adminFront/src/api/promocode/swr.ts

137 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-03-03 13:30:57 +00:00
import { useCallback, useRef } from "react";
import useSwr, { mutate } from "swr";
2024-03-26 15:41:22 +00:00
import { enqueueSnackbar } from "notistack";
import { promocodeApi } from "./requests";
2024-05-21 07:41:31 +00:00
import type { CreatePromocodeBody, PromocodeList } from "@root/model/promocodes";
2024-03-03 13:30:57 +00:00
2024-05-21 07:41:31 +00:00
export function usePromocodes(page: number, pageSize: number, promocodeId: string, to: number, from: number) {
const promocodesCountRef = useRef<number>(0);
const swrResponse = useSwr(
["promocodes", page, pageSize],
async (key) => {
const result = await promocodeApi.getPromocodeList({
limit: key[2],
filter: {
active: true,
},
page: key[1],
});
2024-03-26 15:41:22 +00:00
2024-05-21 07:41:31 +00:00
promocodesCountRef.current = result.count;
return result;
},
{
onError(err) {
console.error("Error fetching promocodes", err);
enqueueSnackbar(err.message, { variant: "error" });
},
focusThrottleInterval: 60e3,
keepPreviousData: true,
}
);
2024-03-26 15:41:22 +00:00
2024-05-21 07:41:31 +00:00
const createPromocode = useCallback(
async function (body: CreatePromocodeBody) {
try {
await promocodeApi.createPromocode(body);
mutate(["promocodes", page, pageSize]);
} catch (error) {
console.error("Error creating promocode", error);
if (error instanceof Error) enqueueSnackbar(error.message, { variant: "error" });
}
},
[page, pageSize]
);
2024-03-26 15:41:22 +00:00
2024-05-21 07:41:31 +00:00
const deletePromocode = useCallback(
async function (id: string) {
try {
await mutate<PromocodeList | undefined, void>(
["promocodes", page, pageSize],
promocodeApi.deletePromocode(id),
{
optimisticData(currentData, displayedData) {
if (!displayedData) return;
2024-03-26 15:41:22 +00:00
2024-05-21 07:41:31 +00:00
return {
count: displayedData.count - 1,
items: displayedData.items.filter((item) => item.id !== id),
};
},
rollbackOnError: true,
populateCache(result, currentData) {
if (!currentData) return;
2024-05-21 07:41:31 +00:00
return {
count: currentData.count - 1,
items: currentData.items.filter((item) => item.id !== id),
};
},
}
);
} catch (error) {
console.error("Error deleting promocode", error);
if (error instanceof Error) enqueueSnackbar(error.message, { variant: "error" });
}
},
[page, pageSize]
);
2024-05-21 07:41:31 +00:00
const promocodeStatistics = useSwr(
["promocodeStatistics", promocodeId, from, to],
async ([_, id, from, to]) => {
if (!id) {
return null;
}
2024-04-02 14:56:23 +00:00
2024-05-21 07:41:31 +00:00
const promocodeStatisticsResponse = await promocodeApi.getPromocodeStatistics(id, from, to);
2024-04-02 14:56:23 +00:00
2024-05-21 07:41:31 +00:00
return promocodeStatisticsResponse;
},
{
onError(err) {
console.error("Error fetching promocode statistics", err);
enqueueSnackbar(err.message, { variant: "error" });
},
focusThrottleInterval: 60e3,
keepPreviousData: true,
}
);
2024-05-21 07:41:31 +00:00
const createFastLink = useCallback(
async function (id: string) {
try {
await promocodeApi.createFastlink(id);
mutate(["promocodes", page, pageSize]);
} catch (error) {
console.error("Error creating fast link", error);
if (error instanceof Error) enqueueSnackbar(error.message, { variant: "error" });
}
},
[page, pageSize]
);
2024-04-02 14:56:23 +00:00
2024-05-21 07:41:31 +00:00
return {
...swrResponse,
createPromocode,
deletePromocode,
createFastLink,
promocodeStatistics: promocodeStatistics.data,
promocodesCount: promocodesCountRef.current,
};
2024-03-27 17:31:51 +00:00
}
export function useAllPromocodes() {
2024-05-21 07:41:31 +00:00
const { data } = useSwr("allPromocodes", promocodeApi.getAllPromocodes, {
keepPreviousData: true,
suspense: true,
onError(err) {
console.error("Error fetching all promocodes", err);
enqueueSnackbar(err.message, { variant: "error" });
},
});
2024-03-27 17:31:51 +00:00
2024-05-21 07:41:31 +00:00
return data;
2024-03-26 15:41:22 +00:00
}