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 | 1x 1x 1x | import { showMessage } from "@common/Toast";
import { NiceModalHandler } from "@ebay/nice-modal-react";
import { yupResolver } from "@hookform/resolvers/yup";
import { customInstance } from "@services/api";
import { buildMerchantEndpoints } from "@services/api/utils.api";
import * as Yup from "yup";
import { AxiosError } from "axios";
import { useForm } from "react-hook-form";
import { useMutation } from "react-query";
import { useRef } from "react";
import { createEmailValidator } from "@validation/fields";
type FormInputs = {
original: boolean;
other: boolean;
email?: string;
emails: string[];
};
const resendReceiptFormDefaultValues = {
original: false,
other: false,
email: undefined,
emails: [],
};
function createReceiptInstance({
transactionID,
customerID,
data,
}: {
transactionID: string;
customerID?: number;
data: any;
}) {
return customInstance({
url: buildMerchantEndpoints(
`transactions/${transactionID}/receipt`,
customerID,
),
method: "POST",
data,
});
}
const useSendReceipt = ({
transactionID,
customerID,
isChargebackReversal,
}: {
transactionID: string;
customerID?: number;
isChargebackReversal?: boolean;
}) => {
const inputRef = useRef<HTMLInputElement>();
const createReceiptMutation = useMutation((data: any) =>
createReceiptInstance({ transactionID, customerID, data }),
);
const methods = useForm<FormInputs>({
mode: "onChange",
reValidateMode: "onChange",
resolver: yupResolver(schema),
defaultValues: resendReceiptFormDefaultValues,
});
const { watch, setValue, getFieldState, setError, reset } = methods;
const { email, emails, other } = watch();
const handleChange = (name: "original" | "other", checked: boolean) => {
setValue(name, checked);
if (name === "original" && checked) {
setValue("other", false);
}
if (name === "other" && checked) {
setValue("original", false);
}
};
const resetForm = () => {
reset({ ...resendReceiptFormDefaultValues });
};
const handleAddEmails = (
e:
| React.KeyboardEvent<HTMLDivElement>
| React.KeyboardEvent<HTMLInputElement>,
) => {
if (!other) {
setValue("other", true);
}
if (e?.key === "Enter" && email && !getFieldState("email").invalid) {
if (emails.find((em) => em === email)) {
setError("email", {
message: "The same email address is already in use",
});
return;
}
if (inputRef.current) {
inputRef.current.value = "";
}
setValue("emails", [...emails, email]);
setValue("email", undefined);
}
};
const handleDeleteEmail = (val: string) => {
const newList = emails.filter((em) => em !== val);
setValue("emails", newList);
};
const handleSubmit = (
{ recipients }: { recipients: string[] | null },
modal?: NiceModalHandler<Record<string, unknown>>,
) => {
const data = {
recipients,
sendToCustomer: Boolean(isChargebackReversal),
};
createReceiptMutation.mutate(data, {
onError: async (error: unknown) => {
showMessage("Error", "Sending receipt failed, please try again");
},
onSuccess: async (res: any) => {
showMessage("Success", "Receipt sent successfully");
resetForm();
if (modal) modal.hide();
},
});
};
return {
handleSubmit,
handleChange,
handleAddEmails,
handleDeleteEmail,
resetForm,
methods,
inputRef,
isLoading: createReceiptMutation.isLoading,
isError: createReceiptMutation.isError,
};
};
export { useSendReceipt, createReceiptInstance };
const schema = Yup.object().shape({
original: Yup.boolean(),
other: Yup.boolean(),
email: createEmailValidator({
required: false,
invalidMessage: "Invalid email address",
}),
emails: Yup.array(
createEmailValidator({
required: false,
invalidMessage: " ",
}),
),
});
|