All files / src/features/Merchants/MerchantSidePanel/UnderwritingTasks/hooks useUpdateTask.tsx

66.66% Statements 16/24
50% Branches 7/14
80% Functions 4/5
72.72% Lines 16/22

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                                        9x         1x                               9x               120x 120x       120x 120x       1x 1x 1x     1x               120x 1x                           1x 1x                     120x    
import { customInstance } from "@services/api";
import { useMutation, useQueryClient } from "react-query";
import { TChallange } from "../../WithRepository/Challenges/types";
import { UNDERWRITING_CHALLENGE_API_KEYS } from "../../constants";
import { showMessage } from "@common/Toast";
import { NotesType, TaskTypeName } from "../types";
import { UseFormReturn } from "react-hook-form";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { useUpdateUnderwritingTask } from "@features/Merchants/MerchantSidePanel/ApprovalFlowPanel/hooks/useUpdateTaskStatus";
 
interface TUpdateTask {
  merchantID: number;
  challengeID: number;
  data: {
    reason: string;
    status: string;
    type: TaskTypeName;
  };
}
 
export const useUpdateTask = async ({
  merchantID,
  challengeID,
  data,
}: TUpdateTask) => {
  return await customInstance({
    url: `/merchants/${merchantID}/underwriting-challenges/${challengeID}/status`,
    method: "PATCH",
    data,
  });
};
 
type Props = {
  merchantID?: number;
  challengeID?: number;
  status?: string;
  type?: TaskTypeName;
  methods?: UseFormReturn<NotesType, any, undefined>;
  onReasonUpdated?: (newReason: string) => void;
};
 
export const useUpdateTaskData = ({
  merchantID,
  challengeID,
  status,
  type,
  methods,
  onReasonUpdated,
}: Props) => {
  const { isNewApprovalFlowEnabled } = useGetFeatureFlagValues();
  const updateTaskStatus = useUpdateUnderwritingTask({
    isCloseOnSuccess: false,
  });
 
  const queryClient = useQueryClient();
  const updateTask = useMutation({
    mutationFn: useUpdateTask,
    mutationKey: ["update-task"],
    onSuccess: (res: TChallange) => {
      methods?.reset({ reason: res.reason });
      Iif (onReasonUpdated) onReasonUpdated(res.reason);
      queryClient.invalidateQueries(
        UNDERWRITING_CHALLENGE_API_KEYS.GET_CHALLENGES,
      );
      queryClient.invalidateQueries(["item"]);
    },
    onError: (err: any) => {
      showMessage("Error", err.message);
      methods?.reset();
    },
  });
 
  const handleReasonChange = (reason: string) => {
    Iif (isNewApprovalFlowEnabled) {
      if (merchantID)
        updateTaskStatus.mutate({
          merchantID,
          taskID: challengeID,
          data: {
            reason,
          },
        });
 
      if (onReasonUpdated) onReasonUpdated(reason);
      return;
    }
 
    Eif (merchantID && challengeID && status && type)
      return updateTask.mutate({
        merchantID,
        challengeID,
        data: {
          reason: reason,
          status: status,
          type: type,
        },
      });
  };
 
  return { handleReasonChange };
};