2024-03-03 13:30:57 +00:00
|
|
|
import { CreatePromocodeBody, PromocodeList } from "@root/model/promocodes";
|
2024-03-03 07:57:12 +00:00
|
|
|
import { enqueueSnackbar } from "notistack";
|
2024-03-03 13:30:57 +00:00
|
|
|
import { useCallback, useRef } from "react";
|
2024-03-03 07:57:12 +00:00
|
|
|
import useSwr, { mutate } from "swr";
|
|
|
|
import { promocodeApi } from "./requests";
|
|
|
|
|
2024-03-03 13:30:57 +00:00
|
|
|
|
|
|
|
export function usePromocodes(page: number, pageSize: 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],
|
|
|
|
});
|
|
|
|
|
|
|
|
promocodesCountRef.current = result.count;
|
|
|
|
return result;
|
|
|
|
},
|
2024-03-03 07:57:12 +00:00
|
|
|
{
|
|
|
|
onError(err) {
|
|
|
|
console.log("Error fetching promocodes", err);
|
|
|
|
enqueueSnackbar(err.message, { variant: "error" });
|
|
|
|
},
|
|
|
|
focusThrottleInterval: 60e3,
|
2024-03-03 13:30:57 +00:00
|
|
|
keepPreviousData: true,
|
2024-03-03 07:57:12 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-03-03 13:30:57 +00:00
|
|
|
const createPromocode = useCallback(async function (body: CreatePromocodeBody) {
|
|
|
|
try {
|
|
|
|
await promocodeApi.createPromocode(body);
|
|
|
|
mutate(["promocodes", page, pageSize]);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error creating promocode", error);
|
|
|
|
if (error instanceof Error) enqueueSnackbar(error.message, { variant: "error" });
|
|
|
|
}
|
|
|
|
}, [page, pageSize]);
|
2024-03-03 07:57:12 +00:00
|
|
|
|
2024-03-03 13:30:57 +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-03 07:57:12 +00:00
|
|
|
|
2024-03-03 13:30:57 +00:00
|
|
|
return {
|
|
|
|
count: displayedData.count - 1,
|
|
|
|
items: displayedData.items.filter((item) => item.id !== id),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
rollbackOnError: true,
|
|
|
|
populateCache(result, currentData) {
|
|
|
|
if (!currentData) return;
|
2024-03-03 07:57:12 +00:00
|
|
|
|
2024-03-03 13:30:57 +00:00
|
|
|
return {
|
|
|
|
count: currentData.count - 1,
|
|
|
|
items: currentData.items.filter((item) => item.id !== id),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.log("Error deleting promocode", error);
|
|
|
|
if (error instanceof Error) enqueueSnackbar(error.message, { variant: "error" });
|
|
|
|
}
|
|
|
|
}, [page, pageSize]);
|
2024-03-03 07:57:12 +00:00
|
|
|
|
2024-03-03 13:30:57 +00:00
|
|
|
return {
|
|
|
|
...swrResponse,
|
|
|
|
createPromocode,
|
|
|
|
deletePromocode,
|
|
|
|
promocodesCount: promocodesCountRef.current,
|
|
|
|
};
|
|
|
|
}
|