All files / src/features/Settings/Team/hooks useEditTeamMember.tsx

0% Statements 0/18
0% Branches 0/4
0% Functions 0/6
0% Lines 0/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 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                                                                                                                                                           
import { MemberRoleName } from "@customTypes/team.member";
import { useMutation, useQueryClient } from "react-query";
import { clearToasts, showMessage } from "@common/Toast/ShowToast";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useGetCurrentMerchantId } from "@hooks/common";
import { editTeamMember, EditMemberTypes } from "@services/api/customer";
import {
  QKEY_LIST_TEAM_MEMBERS,
  QKEY_CHECK_OWNER_MEMBER,
} from "@constants/queryKeys";
 
type Props = {
  accID: string | number;
  roleOnly: boolean;
  handleClose: (data: FormValues) => void;
};
 
type FormValues = {
  firstName: string;
  lastName: string;
  occupation: string;
  employer: string;
  roleName: MemberRoleName;
};
 
export const useEditTeamMember = ({ accID, roleOnly, handleClose }: Props) => {
  const queryClient = useQueryClient();
  const { merchantId } = useGetCurrentMerchantId();
  const { isMobileView } = useCustomThemeV2();
 
  const { mutate, isLoading } = useMutation((formData: FormValues) => {
    const payload: EditMemberTypes = {
      merchantId,
      accID,
      data: {
        firstName: formData.firstName,
        lastName: formData.lastName,
        occupation: formData.occupation,
        employer: formData.employer,
        memberRole: formData.roleName,
        roleOnly,
      },
    };
    return editTeamMember(payload);
  });
 
  const handleEdit = (data: FormValues) => {
    mutate(data, {
      onSuccess: () => {
        showMessage(
          "Success",
          "",
          !isMobileView,
          "Team member updated successfully",
        );
 
        queryClient.invalidateQueries(QKEY_LIST_TEAM_MEMBERS);
        queryClient.invalidateQueries(QKEY_CHECK_OWNER_MEMBER);
      },
      onError: (err: any) => {
        const message =
          err?.response?.data?.message ||
          "Error occurred while updating team member";
 
        showMessage("Error", "", !isMobileView, message);
      },
      onSettled: (_data, error) => {
        if (!error) {
          handleClose(data);
          clearToasts();
        }
      },
    });
  };
 
  return { handleEdit, isLoading };
};