All files / src/features/Merchants/MerchantSidePanel/hooks useUpdateAssignee.tsx

33.33% Statements 5/15
0% Branches 0/15
20% Functions 1/5
35.71% Lines 5/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 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                        50x       416x   416x               416x                                                                                 416x    
import { useMutation } from "react-query";
import { useUpdateMerchantsLists } from "../../../../components/Merchants/MerchantPreview/hooks/useGetTeamMembers";
import { customInstance } from "@services/api";
import { showMessage } from "@common/Toast";
//we should use this hook for updating assignee in rebranded table and panel for onboarding merchants
//later we should enable this endpoint for all updates
 
type TOptions = { updateMerchantPreview?: boolean };
type AssigneePayload = {
  underwriterAssigneeID: number;
};
 
export const useUpdateAssigneeV2 = (
  merchantID?: number,
  options?: TOptions,
) => {
  const { updateData } = useUpdateMerchantsLists();
 
  const updateAssignee = useMutation((data: AssigneePayload) => {
    return customInstance({
      url: `/merchants/${merchantID}/underwriting-assignee`,
      method: "PATCH",
      data,
    });
  });
 
  const handleChangeAssignee = (
    oldAssignee = 0,
    newAssignee: number,
    onSuccess?: () => void,
  ) => {
    const successCharMsg =
      oldAssignee && newAssignee
        ? "changed"
        : newAssignee
        ? "added"
        : "removed";
    updateAssignee.mutate(
      { underwriterAssigneeID: newAssignee },
      {
        onError: (error: unknown) => {
          const axiosError = error as any;
          const errorMessage = axiosError?.response?.data?.message;
          showMessage("Error", errorMessage || "Unable to change assignee");
        },
        onSuccess: (data) => {
          if (onSuccess) onSuccess();
          showMessage("Success", `Assignee has been ${successCharMsg}`);
 
          merchantID &&
            updateData(
              merchantID,
              {
                underwriterID: data?.underwriterID,
                underwriterName:
                  data?.underwriterName || data?.underwriterEmail,
                underwriterImageURL: data?.underwriterImageURL,
              },
              {
                ...options,
              },
            );
        },
      },
    );
  };
 
  return { handleChangeAssignee };
};