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 170 171 172 173 174 175 176 177 178 | 12x 131x 131x 131x 12x 136x 135x 135x 135x 135x 131x 131x 131x 131x 131x 131x 131x 23x 23x 23x 23x 22x 1x 1x 1x 1x 131x 131x 131x 131x 82x 22x 131x 131x | import { useGetCurrentMerchantId } from "@hooks/common";
import { useGetBuilderData } from "@pages/NewAdvancedBuilder/api/builderApi";
import { build } from "@pages/NewAdvancedBuilder/hooks/useGetBuilder";
import { useAppDispatch, useAppSelector } from "@redux/hooks";
import { selectAuth } from "@redux/slices/auth/auth";
import { customInstance } from "@services/api";
import { checkPortals } from "@utils/routing";
import { AxiosError } from "axios";
import { useEffect, useState } from "react";
import { useQuery, UseQueryResult } from "react-query";
import { useParams } from "react-router-dom";
import { TFormStatusEnum } from "../products/types";
import { TPublicForm } from "./types";
import { useCart } from "@sections/PayBuilder/provider/CartContext";
import { setCurrentCampaign } from "@redux/slices/checkout";
import Cookies from "js-cookie";
import { useGetRebrandedFormTypes } from "@sections/PayBuilder/utils";
type R = UseQueryResult<TPublicForm> & {
isAuthenticated: boolean;
notFound: boolean;
formId: string | undefined;
builderData: any;
isADBPaymentFormLoading: boolean;
userCookie?: string;
};
enum ERRORS_ENUM {
NOT_FOUND = 404,
}
const useGetBuilderStandardFormData = ({
enabler,
accID,
name,
description,
publishedStatus,
}: {
enabler: boolean;
accID: number;
name: string;
description: string;
publishedStatus: string;
}) => {
const { id } = useParams();
// const { merchantId } = useGetCurrentMerchantId()
const { isLoading: isADBPaymentFormLoading, data: builderData } =
useGetBuilderData(accID, Number(id), {
enabled:
enabler &&
publishedStatus === TFormStatusEnum.PUBLIC &&
!isNaN(Number(id)),
retry: (_, error) => {
return (error as AxiosError).response?.status !== ERRORS_ENUM.NOT_FOUND;
},
onSuccess(res: any) {
if (res?.snapshot) {
build(
// old snapshots may still use "HTML" in uppercase
res.snapshot?.html ?? res.snapshot?.HTML,
name,
description ?? "",
);
}
},
});
return {
isADBPaymentFormLoading,
builderData,
};
};
// 1- GET /merchant/{merchant_id}/products/{product_id} : within the merchant portal
// 2- GET /products/{product_id}: for public payment form
// During edit of form we need to pass publicFormId to retrieve refund policy data
export const useGetPublicForm = (publicFormIdFromProps?: string): R => {
const isAuthenticated = useAppSelector(selectAuth);
const user = Cookies.get("user");
const { isMerchantPortal } = checkPortals();
const { merchantId, name } = useGetCurrentMerchantId();
const [notFound, setNotFound] = useState<boolean>(false);
const { setDisplayFees } = useCart();
const params = useParams<{ id: string }>(); // by pattern /:id
const idFromPathName = Number(location.pathname.replace("/", ""));
const id =
publicFormIdFromProps ||
(isNaN(idFromPathName) ? params.id : String(idFromPathName));
const isValidId = Boolean(Number(id)) && !!id && !isNaN(Number(id));
const dispatch = useAppDispatch();
const queryData = useQuery({
queryKey: ["public-form-id", id],
queryFn: async () => {
// Safety check: should never execute with invalid ID due to enabled check
Iif (!isValidId) {
throw new Error(`Invalid product ID: ${id}`);
}
setDisplayFees(false);
Iif (isAuthenticated && isMerchantPortal) {
return customInstance({
url: `/merchants/${merchantId}/products/${id}`,
});
} else {
return customInstance({
url: `/products/${id}`,
});
}
},
onSuccess: () => void 0,
// From MDN: Clients that receive a 400 response should expect that repeating the request without modification will fail with the same error.
retry: (_, error) => {
return (error as AxiosError).response?.status !== ERRORS_ENUM.NOT_FOUND;
},
onError: (error) => {
const axiosError = error as AxiosError;
if (axiosError.response?.status === ERRORS_ENUM.NOT_FOUND) {
setNotFound(true);
} else Eif (!axiosError.response) {
// other errors
console.warn(
"Network error while fetching product form:",
axiosError.message,
);
}
},
// Keep Infinity staleTime for this query as it's already configured
// This query is the main product data source and should stay fresh
staleTime: Infinity,
enabled: isValidId,
});
const {
accID,
description,
merchantName,
name: formName,
publishedStatus,
typeName,
} = (queryData?.data || {}) as TPublicForm;
const rebrandedFormTypes = useGetRebrandedFormTypes();
const isNewPayBuilder = rebrandedFormTypes.includes(typeName || "");
useEffect(() => {
if (formName && merchantName) {
dispatch(setCurrentCampaign({ title: `${merchantName} -${formName}` }));
}
}, [formName]);
const { builderData, isADBPaymentFormLoading } =
useGetBuilderStandardFormData({
enabler:
queryData.isSuccess && !isNaN(Number(params.id)) && !isNewPayBuilder,
accID: accID,
description: description ?? "",
name: formName,
publishedStatus,
});
return {
...queryData,
isADBPaymentFormLoading,
isAuthenticated,
userCookie: user,
notFound,
formId: id,
builderData,
};
};
|