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 | import { customInstance } from "@services/api";
import { useQuery } from "react-query";
import { stringToEditorState } from "@utils/draft.editor.helpers";
import { useAppDispatch } from "@redux/hooks";
import { setSelectedVariant } from "@redux/slices/checkout";
import { buildMerchantEndpoints } from "@services/api/utils.api";
export const useFindPaymentFormById = (
id?: string,
publicResource?: boolean,
) => {
if (!id) return { data: undefined };
// Validate ID to prevent invalid API calls
const isValidId = Boolean(Number(id)) && !!id && !isNaN(Number(id));
const getPaymentForm = (id: string) => {
const publicUrl = `products/${id}`;
return customInstance({
url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
method: "GET",
});
};
const getPaymentFormVariants = (id: string) => {
const publicUrl = `products/${id}/variants?&sort=displayOrder`;
return customInstance({
url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
method: "GET",
});
};
const { data, error, isLoading } = useQuery(
["public-form-id", id], // Use unified query key for cache sharing
async () => {
const payment_form = await getPaymentForm(id);
const variants = await getPaymentFormVariants(id);
return { payment_form, variants };
},
{
enabled: isValidId,
refetchOnWindowFocus: false,
staleTime: 1000 * 60 * 2, // Fresh for 2 minutes
cacheTime: 1000 * 60 * 5, // Keep in cache for 5 minutes
retry: false,
},
);
const dispatch = useAppDispatch();
if (!data)
return {
error,
isLoading,
redirect: (error as any)?.response?.status === 404 ? true : false,
};
const customAmounts =
data.variants?.data?.map((amount: any) => {
if (
(amount.minPrice && amount.maxPrice) ||
amount.name === "Any Amount"
) {
const minPrice = (amount.minPrice / 100).toFixed(2);
const maxPrice = (amount.maxPrice / 100).toFixed(2);
dispatch(
setSelectedVariant({
type: "payment-form",
min: minPrice,
max: maxPrice,
}),
);
return {
isDefault: true,
id: amount.id,
amount: (amount.price / 100).toFixed(2),
title: amount.name,
description: amount.description,
min_max: {
enabled: !amount.allowCustomPrice,
min: minPrice,
max: maxPrice,
},
...(amount.inventory && { inventory: parseInt(amount.inventory), }),
thumbnail: amount.imageURL ? amount.imageURL : "",
active: amount.isEnabled,
bundle: 1,
display: amount.showAvailableVariants
};
}
return {
id: amount.id,
title: amount.name,
amount: (amount.price / 100).toFixed(2),
description: {
enabled: Boolean(amount.description),
text: stringToEditorState(amount.description),
},
thumbnail: amount.imageURL ? amount.imageURL : "",
...(amount.inventory && { inventory: parseInt(amount.inventory), }),
active: amount.isEnabled,
bundle: 1,
display: amount.showAvailableVariants
};
}) ?? [];
const customPaymentTypes: any = {};
data?.payment_form?.recurringIntervals?.forEach((item: string) => {
if (item === "once") customPaymentTypes["one_time"] = true;
customPaymentTypes[item] = true;
});
["one_time", "monthly", "quarterly", "yearly"].forEach((item) => {
if (!customPaymentTypes[item]) customPaymentTypes[item] = false;
});
customPaymentTypes["default"] =
data?.payment_form?.defaultRecurringIntervalName === "once"
? "one_time"
: data?.payment_form?.defaultRecurringIntervalName;
const customData = {
general: {
title: data?.payment_form?.name || "",
description: data?.payment_form?.description,
browseMore: data?.payment_form?.canBrowseCampaigns || false,
creatorName: data?.payment_form?.merchantName,
creatorImage: data?.payment_form?.merchantImageURL,
creatorDescription: data?.payment_form?.merchantDescription,
featuredImage: {
image: data?.payment_form?.imageURL || "",
active: Boolean(data?.payment_form?.imageURL),
useAsBackground: data?.payment_form?.usesBackgroundImage || false,
},
},
payment: {
payment_types: customPaymentTypes,
customer_pays_credit: {
active: data?.payment_form?.needsTax || false,
optional: data?.payment_form?.allowFeeChoice || false,
},
amountsList: customAmounts || [],
},
};
return {
data: customData,
isLoading: isLoading,
redirect: (error as any)?.response?.status === 404 ? true : false,
};
};
|