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 179 180 181 182 183 184 185 186 187 188 | 120x 88x 88x 120x 629x 15x 15x | import { showMessage } from "@common/Toast";
import {
QKEY_GET_DONORS_LIST_BY_ID,
QKEY_LIST_PAYMENT_FORMS,
QKEY_LIST_PRODUCTS,
QKEY_SWEEPSTAKE_WINNER,
QKEY_GET_PRODUCT_TYPES,
} from "@constants/queryKeys";
import { useGetCurrentMerchantId } from "@hooks/common";
import { customInstance } from "@services/api";
import {
useInfiniteQuery,
useMutation,
useQuery,
useQueryClient,
} from "react-query";
import { donorModalType } from "../types";
import { parseAmount } from "@utils/index";
import { shortFromNow } from "@utils/helpers";
import { TDonor } from "./provider.type";
export function useDeleteSingleProduct() {
const { merchantId } = useGetCurrentMerchantId();
const queryClient = useQueryClient();
const { mutate } = useMutation(
({
productId,
}: {
onSuccessAction?: () => void;
productId: string | number;
}) => {
return customInstance({
url: `/merchants/${merchantId}/products/${productId}`,
method: "DELETE",
});
},
{
onSuccess(data, variables) {
variables?.onSuccessAction && variables.onSuccessAction();
showMessage("Success", "Product deleted successfully");
queryClient.invalidateQueries(QKEY_LIST_PAYMENT_FORMS);
queryClient.invalidateQueries(QKEY_LIST_PRODUCTS);
queryClient.invalidateQueries({
queryKey: [QKEY_GET_PRODUCT_TYPES, merchantId],
exact: true,
});
},
onError() {
showMessage("Error", "Unable to delete the product");
},
},
);
return { mutate };
}
export function useGetDonorsList({
id,
max = 20,
typeState,
isWinnerSelected,
isEnabled = true,
}: {
id?: string | number;
max?: number;
typeState?: donorModalType;
isWinnerSelected?: boolean;
isEnabled?: boolean;
}) {
const {
data,
fetchNextPage: originalFetchNextPage,
hasNextPage,
isFetching,
isFetchingNextPage,
isError,
isLoading,
} = useInfiniteQuery(
[QKEY_GET_DONORS_LIST_BY_ID, id, typeState, max, isWinnerSelected],
async ({ pageParam = 1 }) => {
const isTopDonorType = typeState === "top_donors";
const sortType = isTopDonorType ? "-sumTransaction" : "-createdAt";
const filters = [];
//for donorsModal => topDonor tab we show only the top donors
if (isTopDonorType) filters.push("(isTopDonor:true)");
//if we have winner we wilter out the winner, bc we got it from useGetSweepstakeWinner
if (isWinnerSelected) filters.push("(isWinner:false)");
const filterParam =
filters.length > 0 ? `filter=%3B${filters.join("%3B")}%3B&` : "";
const response = await customInstance({
url: `/products/${id}/transactions?${filterParam}sort=${sortType}&page=${pageParam}&max=${max}`,
});
const data = response?.data || [];
return {
data: data?.map((donor: any) => {
return {
id: donor?.id,
name: donor?.isCustomerAnonymous
? "Anonymous"
: `${donor?.transactionCustomer?.firstName} ${donor?.transactionCustomer?.lastName}`,
when: shortFromNow(donor?.createdAt),
isTopDonor: donor?.isTopDonor || false,
amount: parseAmount(donor?.sumTransactions / 100),
isSettled: donor?.isSettled,
};
}),
total: response.total,
currentPage: pageParam,
hasNextPage: pageParam * max < response.total, // Determine if more pages exist
};
},
{
getNextPageParam: (lastPage) =>
lastPage.hasNextPage ? lastPage.currentPage + 1 : undefined,
enabled: !!id && isEnabled,
},
);
const fetchNextPage = async () => {
if (hasNextPage && !isFetchingNextPage) {
await originalFetchNextPage();
}
};
return {
donors: (data?.pages.flatMap((page) => page.data) || []) as TDonor[],
total: data?.pages?.[0]?.total ?? 0,
isFetching,
isFetchingNextPage,
isError,
fetchNextPage,
hasNextPage,
isLoading,
};
}
export const useGetSweepstakeWinner = (
id: string | number,
isEnabled?: boolean,
) => {
const { data, isLoading } = useQuery(
[QKEY_SWEEPSTAKE_WINNER, id],
async () => {
const response = await customInstance({
url: `/products/${id}/transactions?filter=%3B(isWinner:true)%3B`,
method: "GET",
});
return (
response?.data?.filter(Boolean)?.map((winner: any) => ({
id: winner?.id,
name: winner?.isCustomerAnonymous
? "Anonymous"
: `${winner?.transactionCustomer?.firstName} ${winner?.transactionCustomer?.lastName}`,
when: shortFromNow(winner?.createdAt),
amount: parseAmount(winner?.sumTransactions / 100),
isSettled: winner?.isSettled,
}))[0] || null
);
},
{
enabled: !!id && isEnabled,
},
);
return {
winner: data as TDonor,
isWinnerSelected: Boolean(data?.amount && data?.name && data?.when),
isLoadingWinner: isLoading,
};
};
export const useGetFundraiserVariants = (id: string) => {
return useQuery(
["fundraiser-variants", id],
async () => {
const data = await customInstance({
url: `products/${id}/variants`,
method: "GET",
});
return data;
},
{
enabled: !!id,
},
);
};
|