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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | 46x 131x 46x 6137x 6137x 6137x 6137x 6137x 126x 126x 711x 711x 576x 576x 711x 576x 6137x 46x 576x 576x 576x 46x 2x 46x 46x 108x 5x 2x 2x 2x 2x 46x 6x 46x 18x 46x 136x | import { TAccounts } from "@customTypes/accounts.types";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { selectUser, updatePartialUser } from "@redux/slices/auth/auth";
import { isDefined } from "@utils/helpers";
import Cookies from "js-cookie";
import { safeParse } from "@utils/index";
import {
UseMutationOptions,
UseQueryOptions,
useMutation,
useQuery,
} from "react-query";
import { customInstance } from "../index";
import * as Sentry from "@sentry/react";
import { QKEY_LIST_ACCOUNTS } from "@constants/queryKeys";
export const getUser = () => {
return customInstance({
url: `/me`,
method: "GET",
});
};
export const useGetUser = (props?: any) => {
const { merchantId } = useGetCurrentMerchantId();
const dispatch = useAppDispatch();
const { email, globalName } = useAppSelector(selectUser);
const { data: merchantData } = useGetMerchantById();
const { data, ...rest } = useQuery(
["user", merchantId],
async () => {
const data = await getUser();
return data;
},
{
...props,
enabled: isDefined(merchantId),
refetchOnWindowFocus: false,
onSuccess: (res) => {
props?.onSuccess?.(res);
if (res?.email && res.email !== email) {
dispatch(updatePartialUser({ email: res.email }));
updateUserCookie({ email: res.email });
}
if (
!isDefined(globalName.lastName) ||
(res?.firstName && res.firstName !== globalName?.firstName) ||
(res?.lastName && res.lastName !== globalName.lastName)
) {
dispatch(
updatePartialUser({
globalName: {
lastName: res?.lastName,
firstName: res?.firstName,
phoneNumber: res?.phoneNumber,
},
}),
);
}
},
},
);
return {
data,
merchantData,
...rest,
};
};
const updateUserCookie = (fieldToUpdate: Record<string, any>) => {
const user = Cookies.get("user");
const parsedUser = user ? safeParse(user) : null;
Eif (!parsedUser) return;
Cookies.set("user", JSON.stringify({ ...parsedUser, ...fieldToUpdate }), {
expires: 30,
});
};
export const getAccounts = () => {
return customInstance({
url: `/accounts?filter=&sort=name&max=500`, //TODO: refactor the flow built on this endpoint and use pagination
method: "GET",
});
};
type QueryData = {
total: number;
data: TAccounts[];
};
export const useGetAccounts = (
options: Omit<
UseQueryOptions<any, any, any, any>,
"queryKey" | "queryFn"
> = {},
) => {
return useQuery<QueryData>(
"accounts",
async () => {
const data = await getAccounts();
return data;
},
options,
);
};
export const useGetAccountsWithUser = (
options: Omit<
UseQueryOptions<any, any, any, any>,
"queryKey" | "queryFn"
> = {},
) => {
return useQuery<{ user: any; accounts: QueryData }>(
QKEY_LIST_ACCOUNTS,
async () => {
const user = await getUser();
Eif (user?.email) {
Sentry.setUser({ email: user.email });
}
const accounts = await getAccounts();
return { user, accounts };
},
{ ...options },
);
};
export const validateAccount = async ({
token,
isFromCheckout = false,
}: {
token: string;
isFromCheckout?: boolean;
}) => {
return customInstance({
url: `/email-verifications/${token}`,
method: "POST",
data: {
isFromCheckout,
},
});
};
export const useValidateAccount = () => {
return useMutation(validateAccount, { mutationKey: "post-validate-account" });
};
export const usePutAccount = (
options: Omit<UseMutationOptions<any, any, any, any>, "mutationFn"> = {},
) => {
return useMutation({
mutationKey: ["putAccount"],
mutationFn: (id: string | number) =>
customInstance({
url: `/accounts/${id}`,
method: "PUT",
}),
...options,
});
};
|