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

Replace all window.location.reload() with data refetch. #6892

Merged
merged 8 commits into from
Dec 28, 2023
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();
sriharsh05 marked this conversation as resolved.
Show resolved Hide resolved
} 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();
};
sriharsh05 marked this conversation as resolved.
Show resolved Hide resolved

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
9 changes: 7 additions & 2 deletions src/Components/Common/UpdatableApp.tsx
sriharsh05 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const APP_UPDATED_KEY = "app-updated";
interface UpdatableAppProps {
children: ReactNode;
silentlyAutoUpdate?: boolean;
onUpdateLocalStorage?: (() => void) | undefined;
}

export const checkForUpdate = async () => {
Expand Down Expand Up @@ -46,7 +47,11 @@ export const checkForUpdate = async () => {
}
};

const UpdatableApp = ({ children, silentlyAutoUpdate }: UpdatableAppProps) => {
const UpdatableApp = ({
children,
silentlyAutoUpdate,
onUpdateLocalStorage,
}: UpdatableAppProps) => {
const [newVersion, setNewVersion] = useState<string>();
const [appUpdated, setAppUpdated] = useState(false);

Expand Down Expand Up @@ -77,7 +82,7 @@ const UpdatableApp = ({ children, silentlyAutoUpdate }: UpdatableAppProps) => {
// A second of delay to appreciate the update animation.
const updateLocalStorageAndReload = () => {
localStorage.setItem(APP_UPDATED_KEY, "true");
window.location.reload();
onUpdateLocalStorage && onUpdateLocalStorage();
localStorage.setItem(APP_VERSION_KEY, newVersion);
};

Expand Down
2 changes: 0 additions & 2 deletions src/Components/Facility/CoverImageEditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ const CoverImageEditModal = ({
);
if (response.status === 200) {
Success({ msg: "Cover image updated." });
window.location.reload();
} else {
Notification.Error({
msg: "Something went wrong!",
Expand All @@ -148,7 +147,6 @@ const CoverImageEditModal = ({
const res = await dispatch(deleteFacilityCoverImage(facility.id as any));
if (res.statusCode === 204) {
Success({ msg: "Cover image deleted" });
window.location.reload();
}

onDelete && onDelete();
Expand Down
1 change: 0 additions & 1 deletion src/Components/Facility/DischargeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const DischargeModal = ({
consultationData,
afterSubmit = () => {
onClose();
window.location.reload();
},
discharge_reason = "",
discharge_notes = "",
Expand Down
31 changes: 16 additions & 15 deletions src/Components/Facility/FacilityHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,20 @@ export const FacilityHome = (props: any) => {

useMessageListener((data) => console.log(data));

const { data: facilityData, loading: isLoading } = useQuery(
routes.getPermittedFacility,
{
pathParams: {
id: facilityId,
},
onResponse: ({ res }) => {
if (!res?.ok) {
navigate("/not-found");
}
},
}
);
const {
data: facilityData,
loading: isLoading,
refetch: facilityFetch,
} = useQuery(routes.getPermittedFacility, {
pathParams: {
id: facilityId,
},
onResponse: ({ res }) => {
if (!res?.ok) {
navigate("/not-found");
}
},
});

const handleDeleteClose = () => {
setOpenDeleteDialog(false);
Expand Down Expand Up @@ -139,10 +140,10 @@ export const FacilityHome = (props: any) => {
onSave={() =>
facilityData?.read_cover_image_url
? setImageKey(Date.now())
: window.location.reload()
: facilityFetch()
}
onClose={() => setEditCoverImage(false)}
onDelete={() => window.location.reload()}
onDelete={() => facilityFetch()}
facility={facilityData ?? ({} as FacilityModel)}
/>
{hasCoverImage ? (
Expand Down
6 changes: 5 additions & 1 deletion src/Components/Patient/PatientInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ export default function PatientInfoCard(props: {
/>
<DischargeModal
show={openDischargeDialog}
onClose={() => setOpenDischargeDialog(false)}
onClose={() => {
setOpenDischargeDialog(false);
props.fetchPatientData &&
props.fetchPatientData({ aborted: false });
sriharsh05 marked this conversation as resolved.
Show resolved Hide resolved
}}
sriharsh05 marked this conversation as resolved.
Show resolved Hide resolved
consultationData={consultation}
/>
</>
Expand Down
23 changes: 16 additions & 7 deletions src/Components/Users/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ export default function UserProfile() {
},
});

const { data: skillsView, loading: isSkillsLoading } = useQuery(
routes.userListSkill,
{
pathParams: { username: authUser.username },
}
);
const {
data: skillsView,
refetch: skillsFetch,
loading: isSkillsLoading,
} = useQuery(routes.userListSkill, {
pathParams: { username: authUser.username },
});

const onUpdateLocalStorage = () => {
skillsFetch();
refetchUserData();
};
sriharsh05 marked this conversation as resolved.
Show resolved Hide resolved

const validateForm = () => {
const errors = { ...initError };
Expand Down Expand Up @@ -776,7 +782,10 @@ export default function UserProfile() {
</div>
</div>
{updateStatus.isUpdateAvailable && (
<UpdatableApp silentlyAutoUpdate={false}>
<UpdatableApp
silentlyAutoUpdate={false}
onUpdateLocalStorage={onUpdateLocalStorage}
>
<ButtonV2 disabled={true}>
<div className="flex items-center gap-4">
<CareIcon className="care-l-exclamation text-2xl" />
Expand Down
Loading