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 | 4x | import { customInstance } from "@services/api";
import { useQuery } from "react-query";
import { stringToEditorState } from "@utils/draft.editor.helpers";
import { buildMerchantEndpoints } from "@services/api/utils.api";
export const useFindMembershipById = (
id?: string,
publicResource?: boolean,
) => {
if (!id) return { data: undefined };
const getMembership = (id: string) => {
const publicUrl = `products/${id}?filter=typeName:"membership"`;
return customInstance({
url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
method: "GET",
});
};
const getMembershipVariants = (id: string) => {
const publicUrl = `products/${id}/variants?&sort=displayOrder`;
return customInstance({
url: publicResource ? publicUrl : buildMerchantEndpoints(publicUrl),
method: "GET",
});
};
const { data, error, isLoading } = useQuery(
"find-membership-by-id",
async () => {
const membership = await getMembership(id);
const variants = await getMembershipVariants(id);
return { membership, variants };
},
{ cacheTime: 0 },
);
if (!data)
return {
error,
isLoading,
redirect: (error as any)?.response?.status === 404 ? true : false,
};
const allIntervals = ["once", "monthly", "quarterly", "yearly"];
function capitalizeFirstLetter(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}
const getRecurringIntervals = (subscription: any) => {
return allIntervals.map((t) => {
if (subscription.recurringIntervals?.includes(t)) {
return {
title: t === "once" ? "One-Time" : capitalizeFirstLetter(t),
checked: true,
isDefault: subscription.defaultRecurringIntervalName === t,
};
}
return {
title: t === "once" ? "One-Time" : capitalizeFirstLetter(t),
checked: false,
isDefault: false,
};
});
};
const customSubscriptionsList = data.variants?.data.map(
(subscription: any) => {
return {
id: subscription.id,
name: subscription.name,
amount: (subscription.price / 100).toFixed(2),
description: {
enabled: Boolean(subscription.description),
text: stringToEditorState(subscription.description),
},
thumbnail: subscription.imageURL ? subscription.imageURL : "",
active: subscription.isEnabled,
display: subscription.showAvailableVariants,
in_stock: subscription.inventory?.toString(),
paymentTypes: getRecurringIntervals(subscription),
bundle: 1,
};
},
);
const customData = {
general: {
title: data?.membership?.name || "",
description: data?.membership?.description || "",
browseMore: data?.membership?.canBrowseCampaigns || false,
creatorName: data?.membership?.merchantName,
creatorImage: data?.membership?.merchantImageURL,
creatorDescription: data?.membership?.merchantDescription,
featuredImage: {
image: data?.membership?.imageURL || "",
active: Boolean(data?.membership?.imageURL),
useAsBackground: data?.membership?.usesBackgroundImage || false,
},
},
subscriptions: {
customer_pays_credit: {
active: data?.membership?.needsTax || false,
optional: data?.membership?.allowFeeChoice || false,
max_subscriptions:
data?.membership?.maxPurchaseQuantity?.toString() || "5",
},
list: customSubscriptionsList,
},
accID: data?.membership?.accID,
};
return {
data: customData,
isLoading: isLoading,
redirect: (error as any)?.response?.status === 404 ? true : false,
};
};
|