All files / src/hooks/upload-api useUploadCustomerFile.ts

63.15% Statements 12/19
25% Branches 2/8
66.66% Functions 2/3
61.11% Lines 11/18

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                4x   85x 85x 85x   85x       85x 1x 1x                 1x   1x                           85x    
import {
  AcceptAllowedImagesTypes,
  useUploadSingleImage,
} from "@hooks/upload-api/uploadHooks";
import { useIsValidFile } from "@hooks/upload-api/useIsValidFile";
import { createImage } from "@services/api/upload/uploadEndpoints";
import { useMutation, useQueryClient } from "react-query";
 
export const useUploadCustomerFile = () => {
  // to be deleted
  const { handleUpload, isLoading } = useUploadSingleImage();
  const { isValidFile } = useIsValidFile();
  const queryClient = useQueryClient();
 
  const createImageMutation = useMutation(({ data }: any) => {
    return createImage(data);
  });
 
  const handlePostImage = async ({ file }: { file: File }) => {
    try {
      Iif (
        !isValidFile({
          file,
          allowedTypes: Object.keys(AcceptAllowedImagesTypes),
        })
      ) {
        return;
      }
 
      const tempUrl = await handleUpload({ file, formType: "customer_avatar" });
 
      Eif (!tempUrl) return "";
 
      const promise = await createImageMutation.mutateAsync({
        data: { label: file?.name || "main_pic", url: tempUrl },
      });
 
      await queryClient.invalidateQueries("get-all-media-items");
      const url: string = promise.URL || "";
      return url;
    } catch (err) {
      return "";
    }
  };
 
  return { handlePostImage, isLoading };
};