Skip to content

Commit

Permalink
replaced window.location.reload with appropriate refetch in assetImpo…
Browse files Browse the repository at this point in the history
…rtModal
  • Loading branch information
sriharsh05 committed Dec 21, 2023
1 parent 351b51b commit 153f971
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/Components/Assets/AssetImportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ interface Props {
open: boolean;
onClose: (() => void) | undefined;
facility: FacilityModel;
onUpdate?: (() => void) | undefined;
}

const AssetImportModal = ({ open, onClose, facility }: Props) => {
const AssetImportModal = ({ open, onClose, facility, onUpdate }: Props) => {
const [isImporting, setIsImporting] = useState(false);
const [selectedFile, setSelectedFile] = useState<any>();
const [preview, setPreview] =
Expand Down Expand Up @@ -170,7 +171,7 @@ const AssetImportModal = ({ open, onClose, facility }: Props) => {
Notification.Success({ msg: "Assets imported successfully" });
await sleep(1000);
setIsImporting(false);
window.location.reload();
onUpdate && onUpdate();
} else {
Notification.Error({ msg: "Error importing some assets" });
await sleep(1000);
Expand Down
49 changes: 31 additions & 18 deletions src/Components/Assets/AssetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const AssetsList = () => {
qParams.warranty_amc_end_of_validity_after || "",
};

const { loading } = useQuery(routes.listAssets, {
const { refetch: assetsFetch, loading } = useQuery(routes.listAssets, {
query: params,
onResponse: ({ res, data }) => {
if (res?.status === 200 && data) {
Expand All @@ -79,16 +79,19 @@ const AssetsList = () => {
},
});

const { data: facilityObject } = useQuery(routes.getAnyFacility, {
pathParams: { id: qParams.facility },
onResponse: ({ res, data }) => {
if (res?.status === 200 && data) {
setFacility(data);
setSelectedFacility(data);
}
},
prefetch: !!qParams.facility,
});
const { data: facilityObject, refetch: facilityFetch } = useQuery(
routes.getAnyFacility,
{
pathParams: { id: qParams.facility },
onResponse: ({ res, data }) => {
if (res?.status === 200 && data) {
setFacility(data);
setSelectedFacility(data);
}
},
prefetch: !!qParams.facility,
}
);

useEffect(() => {
setAssetType(qParams.asset_type);
Expand All @@ -102,13 +105,16 @@ const AssetsList = () => {
setAssetClass(qParams.asset_class);
}, [qParams.asset_class]);

const { data: locationObject } = useQuery(routes.getFacilityAssetLocation, {
pathParams: {
facility_external_id: String(qParams.facility),
external_id: String(qParams.location),
},
prefetch: !!(qParams.facility && qParams.location),
});
const { data: locationObject, refetch: locationFetch } = useQuery(
routes.getFacilityAssetLocation,
{
pathParams: {
facility_external_id: String(qParams.facility),
external_id: String(qParams.location),
},
prefetch: !!(qParams.facility && qParams.location),
}
);

const getAssetIdFromQR = async (assetUrl: string) => {
try {
Expand All @@ -128,6 +134,12 @@ const AssetsList = () => {
}
};

const handleUpdate = () => {
assetsFetch();
facilityFetch();
locationFetch();
};

const checkValidAssetId = async (assetId: string) => {
const { data: assetData } = await request(routes.getAsset, {
pathParams: { id: assetId },
Expand Down Expand Up @@ -436,6 +448,7 @@ const AssetsList = () => {
return f;
});
}}
onUpdate={handleUpdate}
facility={facility}
/>
)}
Expand Down

0 comments on commit 153f971

Please sign in to comment.