All files / src/shared/SidePanel/components/SidepanelTitle/hooks useChangeProfileImage.tsx

50% Statements 9/18
0% Branches 0/6
25% Functions 1/4
64.28% Lines 9/14

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                21x 464x       464x 464x 464x 464x                         464x   464x   464x                
import {
  useUpdateMedia,
  useUpdateMerchantInfo,
} from "@components/Merchants/MerchantPreview/hooks/useUpdateMerchantInfo";
import { useUploadSingleImage } from "@hooks/upload-api/uploadHooks";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
 
const useChangeProfileImage = (merchantId: number) => {
  const hasUpdatePermissions = useAccessControl({
    resource: RESOURCE_BASE.MERCHANT,
    operation: OPERATIONS.UPDATE,
  });
  const { handleSubmit } = useUpdateMerchantInfo(merchantId);
  const { handleUpload } = useUploadSingleImage();
  const { mutate } = useUpdateMedia({ merchantId });
  const handleChangeStatus: any = async ({ file }: { file: File }) => {
    if (!file) return;
 
    const img = await handleUpload({ file, formType: "customer_avatar" });
    if (img) handleSubmit("avatar", { avatar: img });
 
    if (img)
      mutate({
        label: file?.name,
        url: img,
      });
  };
 
  const onUpload = (file: File) => handleChangeStatus({ file });
 
  const onDelete = () => handleSubmit("avatar", { avatar: null });
 
  return {
    onUpload,
    onDelete,
    disabled: !hasUpdatePermissions,
  };
};
 
export default useChangeProfileImage;