Skip to content

Commit

Permalink
Added an option to upload cover image as a step in facility registrat…
Browse files Browse the repository at this point in the history
…ion process (ohcnetwork#7297)
  • Loading branch information
Utkarsh-Anandani committed Dec 22, 2024
1 parent ae8ac6f commit ff501c8
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 5 deletions.
138 changes: 138 additions & 0 deletions src/components/Facility/CoverImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import careConfig from "@careConfig";
import { navigate } from "raviger";
import { SetStateAction } from "react";
import { useTranslation } from "react-i18next";

import useAppHistory from "@/hooks/useAppHistory";

import { LocalStorageKeys } from "@/common/constants";

import * as Notification from "@/Utils/Notifications";
import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import uploadFile from "@/Utils/request/uploadFile";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { sleep } from "@/Utils/utils";

import AvatarEditModal from "../Common/AvatarEditModal";
import AvatarEditable from "../Common/AvatarEditable";
import { Cancel, Submit } from "../Common/ButtonV2";

type uploadCoverImageType = React.Dispatch<SetStateAction<boolean>>;
type setCurrentStepType = React.Dispatch<SetStateAction<number>>;

interface CoverImageProps {
uploadCoverImage: boolean;
setUploadCoverImage: uploadCoverImageType;
createdFacilityId: string;
currentStep: number;
setCurrentStep: setCurrentStepType;
}

export const CoverImage = ({
uploadCoverImage,
setUploadCoverImage,
createdFacilityId,
currentStep,
setCurrentStep,
}: CoverImageProps) => {
const { t } = useTranslation();
const { goBack } = useAppHistory();

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

const handleCoverImageUpload = async (file: File, onError: () => void) => {
const formData = new FormData();
formData.append("cover_image", file);
const url = `${careConfig.apiUrl}/api/v1/facility/${createdFacilityId}/cover_image/`;

uploadFile(
url,
formData,
"POST",
{
Authorization:
"Bearer " + localStorage.getItem(LocalStorageKeys.accessToken),
},
async (xhr: XMLHttpRequest) => {
if (xhr.status === 200) {
await sleep(1000);
facilityFetch();
Notification.Success({ msg: "Cover image updated." });
setUploadCoverImage(false);
} else {
onError();
}
},
null,
() => {
onError();
},
);
};

const handleCoverImageDelete = async (onError: () => void) => {
const { res } = await request(routes.deleteFacilityCoverImage, {
pathParams: { id: createdFacilityId },
});
if (res?.ok) {
Notification.Success({ msg: "Cover image deleted" });
facilityFetch();
setUploadCoverImage(false);
} else {
onError();
}
};

return (
<div className="w-full h-full my-auto flex justify-center items-center">
<AvatarEditModal
title={t("edit_cover_photo")}
open={uploadCoverImage}
imageUrl={facilityData?.read_cover_image_url}
handleUpload={handleCoverImageUpload}
handleDelete={handleCoverImageDelete}
onClose={() => setUploadCoverImage(false)}
/>
<div className="flex flex-col md:flex-row gap-5 relative">
<AvatarEditable
id="facility-coverimage"
imageUrl={facilityData?.read_cover_image_url}
name={facilityData?.name ? facilityData.name : ""}
editable={true}
onClick={() => setUploadCoverImage(true)}
className="md:mr-2 lg:mr-6 lg:h-80 lg:w-80"
/>
<div className="w-full h-full">
<h3>Hover the existing cover image to upload a new cover image</h3>
<h5>Guidelines for cover image selection</h5>
<ul className="list-disc pl-6 text-sm text-color-body-dark">
<li>Max size for image uploaded should be 1mb</li>
<li>Allowed formats are jpg,png,jpeg</li>
<li>Recommended aspect ratio for the image is 1:1</li>
</ul>
<div className="mt-20 flex flex-col-reverse justify-end gap-3 sm:flex-row md:absolute md:right-0 md:bottom-5">
<Cancel onClick={() => goBack()} />
<Submit
type="button"
onClick={() => {
setCurrentStep(currentStep + 1);
}}
label={"Update Cover Image"}
/>
</div>
</div>
</div>
</div>
);
};
54 changes: 49 additions & 5 deletions src/components/Facility/FacilityCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import {
parsePhoneNumber,
} from "@/Utils/utils";

import { CoverImage } from "./CoverImage";

interface FacilityProps {
facilityId?: string;
}
Expand Down Expand Up @@ -162,6 +164,9 @@ export const FacilityCreate = (props: FacilityProps) => {
const [stateId, setStateId] = useState<number>();
const [districtId, setDistrictId] = useState<number>();
const [localBodyId, setLocalBodyId] = useState<number>();
const [_facilityName, setFacilityName] = useState("");
const [uploadCoverImage, setUploadCoverImage] = useState(false);
const [_coverImageURL, setCoverImageURL] = useState<string>();
const { goBack } = useAppHistory();
const headerText = !facilityId ? "Create Facility" : "Update Facility";
const buttonText = !facilityId ? "Save Facility" : "Update Facility";
Expand Down Expand Up @@ -213,7 +218,7 @@ export const FacilityCreate = (props: FacilityProps) => {
},
{
id: 2,
name: "Bed Capacity",
name: "Cover Image",
onClick: () => {
setCurrentStep(2);
},
Expand All @@ -227,12 +232,26 @@ export const FacilityCreate = (props: FacilityProps) => {
},
{
id: 3,
name: "Staff Capacity",
name: "Bed Capacity",
onClick: () => {
setCurrentStep(3);
},
status:
currentStep === 3
? "current"
: currentStep > 3
? "complete"
: "upcoming",
disabled: createdFacilityId == "",
},
{
id: 4,
name: "Staff Capacity",
onClick: () => {
setCurrentStep(4);
},
disabled: createdFacilityId == "",
status: currentStep === 3 ? "current" : "upcoming",
status: currentStep === 4 ? "current" : "upcoming",
},
];
};
Expand All @@ -259,6 +278,9 @@ export const FacilityCreate = (props: FacilityProps) => {
const formData = {
facility_type: data.facility_type ? data.facility_type : "",
name: data.name ? data.name : "",
read_cover_image_url: data.read_cover_image_url
? data.read_cover_image_url
: "",
state: data.state ? data.state : 0,
district: data.district ? data.district : 0,
local_body: data.local_body ? data.local_body : 0,
Expand Down Expand Up @@ -289,6 +311,7 @@ export const FacilityCreate = (props: FacilityProps) => {
setStateId(data.state);
setDistrictId(data.district);
setLocalBodyId(data.local_body);
setCoverImageURL(data.read_cover_image_url);
} else {
navigate(`/facility/${facilityId}`);
}
Expand Down Expand Up @@ -510,6 +533,7 @@ export const FacilityCreate = (props: FacilityProps) => {

if (res?.ok && requestData) {
const id = requestData.id;
setFacilityName(requestData.name ? requestData.name : "");
dispatch({ type: "set_form", form: initForm });
if (!facilityId) {
Notification.Success({
Expand Down Expand Up @@ -650,7 +674,7 @@ export const FacilityCreate = (props: FacilityProps) => {
};

switch (currentStep) {
case 3:
case 4:
return (
<Page
title={headerText}
Expand Down Expand Up @@ -687,7 +711,7 @@ export const FacilityCreate = (props: FacilityProps) => {
</div>
</Page>
);
case 2:
case 3:
return (
<Page
title={headerText}
Expand Down Expand Up @@ -724,6 +748,26 @@ export const FacilityCreate = (props: FacilityProps) => {
</div>
</Page>
);
case 2:
return (
<Page
title={headerText}
crumbsReplacements={{
[facilityId || "????"]: { name: state.form.name },
}}
>
<Steps steps={getSteps()} />
<div className="mt-3">
<CoverImage
uploadCoverImage={uploadCoverImage}
setUploadCoverImage={setUploadCoverImage}
createdFacilityId={createdFacilityId}
currentStep={currentStep}
setCurrentStep={setCurrentStep}
/>
</div>
</Page>
);
case 1:
default:
return (
Expand Down

0 comments on commit ff501c8

Please sign in to comment.