Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | import { useGetCurrentMerchantId } from "@hooks/common";
import {
useCamera,
useGetIdentificationFiles,
useGetUploadedSelfie,
} from "@sections/VerifyAccountHolder_v2/hooks/useCamera";
import { useUploadFiles } from "@hooks/upload-api/uploadHooks";
import { SetStateAction, useEffect, useState } from "react";
import { useGetUploadProgress } from "@redux/slices/uploadProgressSlice";
import { showMessage } from "@common/Toast";
import { QKEY_GET_MERCHANT_BY_ID } from "@constants/queryKeys";
import { useQueryClient } from "react-query";
import { base64ToFile } from "@utils/helpers";
import { isEmpty } from "lodash";
import { deleteDocumentsInLoop } from "@components/Settings/Business/Documents/utils";
import { challengeSlugs } from "@constants/challengeSlugs";
type TUseCameraAction = {
setSelfieUrl: (value: SetStateAction<string | null>) => void;
setCompleted: (completed: boolean) => void;
updateProgressBar: (value: number) => void;
merchant?: any;
selfieUrl: string | null;
homepageReset: () => void;
dbSelfieURL: string | null;
};
const useCameraAction = ({
setSelfieUrl,
setCompleted,
updateProgressBar,
merchant,
selfieUrl,
homepageReset,
dbSelfieURL,
}: TUseCameraAction) => {
const queryClient = useQueryClient();
const { data: files, refetch } = useGetIdentificationFiles();
const { merchantId } = useGetCurrentMerchantId();
const { uploadedSelfie, isLoading: gettingSelfie } = useGetUploadedSelfie();
const { handleUpload } = useUploadFiles();
const {
cameraStatus,
isCameraAvailable,
webcamRef,
setCamera,
destroyCamera,
} = useCamera();
const [selfie, setSelfie] = useState<string | null>(null);
const [isCustomLoading, setIsCustomLoading] = useState<boolean>(false);
const [isButtonDisabled, setIsButtonDisabled] = useState<boolean>(false);
const progress = useGetUploadProgress();
const progressKey = Object.keys(progress)[0];
const { canceled, deleted } = progress[progressKey] || {};
const isCameraOff = selfie === null && cameraStatus?.active === undefined;
const isCameraOn = selfie === null;
const isImageCaptured = selfie && !selfieUrl && !cameraStatus?.active;
const isAllDone = selfieUrl && !isCustomLoading;
// Handling upload cancel and delete
useEffect(() => {
if (canceled) {
cancelUpload();
} else if (deleted) {
deleteUpload();
}
}, [canceled, deleted]);
useEffect(() => {
if (isEmpty(progress) && isCustomLoading) {
cancelUpload();
}
}, [progress]);
const retakePhoto = () => {
setSelfie(null);
};
const cancelUpload = () => {
if (isCustomLoading && isButtonDisabled) {
setIsCustomLoading(false);
setIsButtonDisabled(false);
}
};
const deleteUpload = () => {
if (!selfie) return;
showMessage(
"Info",
"Please capture selfie again", // TODO: add the correct message
true,
"Image is deleted successfully", // TODO: add the correct message
);
setSelfie(null);
setSelfieUrl(null);
setIsCustomLoading(false);
handleRefetch();
queryClient.invalidateQueries([QKEY_GET_MERCHANT_BY_ID, merchantId]);
};
const showPendingMsg = () => {
showMessage(
"Info",
"Please upload again", // TODO: add the correct message
true,
"Image upload is canceled", // TODO: add the correct message
);
setIsButtonDisabled(false);
};
const handleRefetch = () => {
refetch().then((res) => {
const latestSelfieData = res?.data?.data?.reduce(
(latest: any, current: any) => {
if (
current.attTypeName === "account_owner_selfie" &&
current.updatedAt > latest.updatedAt
) {
return current;
}
return latest;
},
{ updatedAt: 0 },
);
const latestSelfieURL = latestSelfieData?.fileURL;
if (!latestSelfieData?.isUploaded) return showPendingMsg();
if (setSelfieUrl) {
setSelfieUrl(latestSelfieURL);
setIsCustomLoading(false);
setIsButtonDisabled(false);
} else {
setCompleted && setCompleted(true);
setIsCustomLoading(false);
setIsButtonDisabled(false);
}
queryClient.invalidateQueries(QKEY_GET_MERCHANT_BY_ID);
});
};
const onSnap = () => {
if (cameraStatus) {
if (webcamRef.current) {
const imageSrc = webcamRef.current.getScreenshot();
setSelfie(imageSrc);
updateProgressBar(100);
destroyCamera({ cameraStatus });
}
} else {
setCamera();
}
};
const onSubmitSelfie = async () => {
if (!selfie) return;
setIsCustomLoading(true);
setIsButtonDisabled(true);
if (cameraStatus) {
destroyCamera({ cameraStatus });
}
const selfieFile = base64ToFile(selfie);
const fileArr = files?.data?.filter(
(doc: any) => doc?.attTypeName === "account_owner_selfie",
);
const hideDeleteMessage = true;
!isEmpty(fileArr) &&
!isEmpty(merchant) &&
(await deleteDocumentsInLoop(
fileArr,
merchant?.accID || merchantId,
hideDeleteMessage,
));
await handleUpload(
{
list: [{ file: selfieFile }],
merchantId: merchant?.accID || merchantId,
resourceID: merchant?.accID || merchantId,
attachmentType: "account_owner_selfie",
label: "",
tag: `merchant upload`,
},
challengeSlugs.PRIMARY_5,
);
handleRefetch();
queryClient.invalidateQueries([QKEY_GET_MERCHANT_BY_ID, merchantId]);
};
const getPrimaryButtonText = () => {
if (isCameraOff) {
return "Open camera";
} else if (isCameraOn) {
return "";
} else if (isImageCaptured) {
return "Submit";
} else if (isAllDone) {
return "Done";
}
};
const handlePrimaryAction = () => {
if (isCameraOff || isCameraOn) {
onSnap();
} else if (isImageCaptured) {
onSubmitSelfie();
} else if (isAllDone) {
setSelfieUrl(null);
homepageReset();
} else {
setSelfieUrl(dbSelfieURL);
}
};
return {
cameraInfo: {
cameraStatus,
webcamRef,
isCameraOff,
isCameraOn,
},
cameraAction: {
retakePhoto,
getPrimaryButtonText,
handlePrimaryAction,
},
selfieStates: {
selfie,
uploadedSelfie,
isCustomLoading,
isAllDone,
gettingSelfie,
isButtonDisabled,
isCameraAvailable,
},
};
};
export default useCameraAction;
|