From 29d812a1055e052f6e11c0692b56d659903fc0f1 Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Sat, 27 Apr 2024 23:12:46 +0530 Subject: [PATCH 1/4] fix qr notificaation --- src/Components/Assets/AssetsList.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Components/Assets/AssetsList.tsx b/src/Components/Assets/AssetsList.tsx index 796e532a41e..48609c78983 100644 --- a/src/Components/Assets/AssetsList.tsx +++ b/src/Components/Assets/AssetsList.tsx @@ -116,7 +116,14 @@ const AssetsList = () => { const { data } = await request(routes.listAssets, { query: { qr_code_id: assetId }, }); - return data?.results[0].id; + if (data?.results.length === 0) { + setIsLoading(false); + Notification.Error({ + msg: "You are not authorized to view this asset", + }); + } else { + return data?.results[0].id; + } } } catch (err) { console.log(err); From 7c1032a057b0ca81c2d71c0decc8c0e596caba1c Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Sun, 28 Apr 2024 13:02:47 +0530 Subject: [PATCH 2/4] change error notification --- src/Components/Assets/AssetsList.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Assets/AssetsList.tsx b/src/Components/Assets/AssetsList.tsx index 48609c78983..3d087dbc97e 100644 --- a/src/Components/Assets/AssetsList.tsx +++ b/src/Components/Assets/AssetsList.tsx @@ -119,7 +119,7 @@ const AssetsList = () => { if (data?.results.length === 0) { setIsLoading(false); Notification.Error({ - msg: "You are not authorized to view this asset", + msg: "Asset not found !!!", }); } else { return data?.results[0].id; From 9521e63aded66247026295d34dfa99d7cc870de1 Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Wed, 1 May 2024 15:45:07 +0530 Subject: [PATCH 3/4] add error notification --- src/Components/Assets/AssetsList.tsx | 32 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Components/Assets/AssetsList.tsx b/src/Components/Assets/AssetsList.tsx index 3d087dbc97e..03bedc9cb2d 100644 --- a/src/Components/Assets/AssetsList.tsx +++ b/src/Components/Assets/AssetsList.tsx @@ -114,15 +114,10 @@ const AssetsList = () => { const assetId = params.asset || params.assetQR; if (assetId) { const { data } = await request(routes.listAssets, { - query: { qr_code_id: assetId }, + query: { qr_code_id: assetId, limit: 1 }, }); - if (data?.results.length === 0) { - setIsLoading(false); - Notification.Error({ - msg: "Asset not found !!!", - }); - } else { - return data?.results[0].id; + if (data?.results.length === 1) { + return data.results[0].id; } } } catch (err) { @@ -131,10 +126,19 @@ const AssetsList = () => { }; const checkValidAssetId = async (assetId: string) => { - const { data: assetData } = await request(routes.getAsset, { - pathParams: { external_id: assetId }, - }); try { + const { data: assetData } = await request(routes.getAsset, { + pathParams: { external_id: assetId }, + onResponse: ({ res }) => { + if (res?.status !== 200) { + setIsLoading(false); + Notification.Error({ + msg: "Invalid QR code scanned !!!", + }); + return; + } + }, + }); if (assetData) { navigate( `/facility/${assetData.location_object.facility?.id}/assets/${assetId}`, @@ -142,10 +146,6 @@ const AssetsList = () => { } } catch (err) { console.log(err); - setIsLoading(false); - Notification.Error({ - msg: "Invalid QR code scanned !!!", - }); } }; @@ -167,7 +167,7 @@ const AssetsList = () => { onResult={async (text) => { if (text) { const assetId = await getAssetIdFromQR(text); - checkValidAssetId(assetId ?? text); + await checkValidAssetId(assetId ?? text); } }} onError={(e) => { From 75693c4a9ee6dfe3d6b376425100cfe9c47ab32c Mon Sep 17 00:00:00 2001 From: Ashraf Mohammed Date: Tue, 14 May 2024 00:21:36 +0530 Subject: [PATCH 4/4] add validation --- src/Components/Assets/AssetsList.tsx | 77 +++++++++++++++++----------- src/Redux/api.tsx | 5 ++ 2 files changed, 51 insertions(+), 31 deletions(-) diff --git a/src/Components/Assets/AssetsList.tsx b/src/Components/Assets/AssetsList.tsx index 03bedc9cb2d..f6f83e7030f 100644 --- a/src/Components/Assets/AssetsList.tsx +++ b/src/Components/Assets/AssetsList.tsx @@ -105,44 +105,60 @@ const AssetsList = () => { prefetch: !!(qParams.facility && qParams.location), }); - const getAssetIdFromQR = async (assetUrl: string) => { + function isValidURL(url: string) { + try { + new URL(url); + return true; + } catch (_) { + return false; + } + } + + const accessAssetIdFromQR = async (assetURL: string) => { try { setIsLoading(true); setIsScannerActive(false); - const params = parseQueryParams(assetUrl); + if (!isValidURL(assetURL)) { + setIsLoading(false); + Notification.Error({ + msg: "Invalid QR code scanned !!!", + }); + return; + } + const params = parseQueryParams(assetURL); // QR Maybe searchParams "asset" or "assetQR" + // If no params found, then use assetText const assetId = params.asset || params.assetQR; + if (assetId) { - const { data } = await request(routes.listAssets, { + const { data } = await request(routes.listAssetQR, { + pathParams: { qr_code_id: assetId }, + }); + if (!data) { + setIsLoading(false); + Notification.Error({ + msg: "Invalid QR code scanned !!!", + }); + return; + } + const { data: assetData } = await request(routes.listAssets, { query: { qr_code_id: assetId, limit: 1 }, }); - if (data?.results.length === 1) { - return data.results[0].id; + if (assetData?.results.length === 1) { + navigate( + `/facility/${assetData.results[0].location_object.facility?.id}/assets/${assetData.results[0].id}`, + ); + } else { + setIsLoading(false); + Notification.Error({ + msg: "Asset not found !!!", + }); } - } - } catch (err) { - console.log(err); - } - }; - - const checkValidAssetId = async (assetId: string) => { - try { - const { data: assetData } = await request(routes.getAsset, { - pathParams: { external_id: assetId }, - onResponse: ({ res }) => { - if (res?.status !== 200) { - setIsLoading(false); - Notification.Error({ - msg: "Invalid QR code scanned !!!", - }); - return; - } - }, - }); - if (assetData) { - navigate( - `/facility/${assetData.location_object.facility?.id}/assets/${assetId}`, - ); + } else { + setIsLoading(false); + Notification.Error({ + msg: "Invalid QR code scanned !!!", + }); } } catch (err) { console.log(err); @@ -166,8 +182,7 @@ const AssetsList = () => { { if (text) { - const assetId = await getAssetIdFromQR(text); - await checkValidAssetId(assetId ?? text); + await accessAssetIdFromQR(text); } }} onError={(e) => { diff --git a/src/Redux/api.tsx b/src/Redux/api.tsx index 82ae52f26a4..13cdf20f5dd 100644 --- a/src/Redux/api.tsx +++ b/src/Redux/api.tsx @@ -1268,6 +1268,11 @@ const routes = { method: "GET", TRes: Type>(), }, + listAssetQR: { + path: "/api/v1/public/asset_qr/{qr_code_id}/", + method: "GET", + TRes: Type(), + }, // Asset transaction endpoints