Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QR code error notification #7726

Merged
merged 11 commits into from
Jun 5, 2024
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
Loading