Skip to content

Commit

Permalink
Fix QR code error notification (#7726)
Browse files Browse the repository at this point in the history
* fix qr notificaation

* change error notification

* add error notification

* add validation
  • Loading branch information
AshrafMd-1 authored Jun 5, 2024
1 parent dda7cb9 commit 5dfced0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
74 changes: 48 additions & 26 deletions src/Components/Assets/AssetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,63 @@ 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, {
query: { qr_code_id: assetId },
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 (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 !!!",
});
}
} else {
setIsLoading(false);
Notification.Error({
msg: "Invalid QR code scanned !!!",
});
return data?.results[0].id;
}
} catch (err) {
console.log(err);
}
};

const checkValidAssetId = async (assetId: string) => {
const { data: assetData } = await request(routes.getAsset, {
pathParams: { external_id: assetId },
});
try {
if (assetData) {
navigate(
`/facility/${assetData.location_object.facility?.id}/assets/${assetId}`,
);
}
} catch (err) {
console.log(err);
setIsLoading(false);
Notification.Error({
msg: "Invalid QR code scanned !!!",
});
}
};

Expand All @@ -159,8 +182,7 @@ const AssetsList = () => {
<Scanner
onResult={async (text) => {
if (text) {
const assetId = await getAssetIdFromQR(text);
checkValidAssetId(assetId ?? text);
await accessAssetIdFromQR(text);
}
}}
onError={(e) => {
Expand Down
5 changes: 5 additions & 0 deletions src/Redux/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,11 @@ const routes = {
method: "GET",
TRes: Type<PaginatedResponse<AvailabilityRecord>>(),
},
listAssetQR: {
path: "/api/v1/public/asset_qr/{qr_code_id}/",
method: "GET",
TRes: Type<AssetData>(),
},

// Asset transaction endpoints

Expand Down

0 comments on commit 5dfced0

Please sign in to comment.