82 lines
3.5 KiB
TypeScript
82 lines
3.5 KiB
TypeScript
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { toast } from "sonner";
|
|
import { useTranslation } from "react-i18next";
|
|
import { useDebouncedValue, normalizeSearch } from "@/hooks/useDebouncedSearch";
|
|
import { cardPinApi, type CardPinListParams, type CreateCardPinPayload, type UpdateCardPinPayload } from "../api/cardPinApi";
|
|
|
|
export const CARD_PIN_KEYS = {
|
|
all: ["card-pin-orders"] as const,
|
|
lists: () => [...CARD_PIN_KEYS.all, "list"] as const,
|
|
list: (p: CardPinListParams) => [...CARD_PIN_KEYS.lists(), p] as const,
|
|
detail: (id: number) => [...CARD_PIN_KEYS.all, "detail", id] as const,
|
|
};
|
|
|
|
// ─── List ─────────────────────────────────────────────────────────────────────
|
|
|
|
export function useCardPins(params: CardPinListParams) {
|
|
const debouncedSearch = useDebouncedValue(params.search ?? "");
|
|
const queryParams: CardPinListParams = { ...params, search: normalizeSearch(debouncedSearch) };
|
|
|
|
return useQuery({
|
|
queryKey: CARD_PIN_KEYS.list(queryParams),
|
|
queryFn: ({ signal }) => cardPinApi.list(queryParams, signal),
|
|
placeholderData: (prev) => prev,
|
|
});
|
|
}
|
|
|
|
// ─── Detail ───────────────────────────────────────────────────────────────────
|
|
|
|
export function useCardPin(id: number) {
|
|
return useQuery({
|
|
queryKey: CARD_PIN_KEYS.detail(id),
|
|
queryFn: () => cardPinApi.getById(id),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
// ─── Create ───────────────────────────────────────────────────────────────────
|
|
|
|
export function useCreateCardPin() {
|
|
const qc = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation({
|
|
mutationFn: (payload: CreateCardPinPayload) => cardPinApi.create(payload),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() });
|
|
toast.success(t("cardPin.createSuccess", "Kart pin bukja döredildi"));
|
|
},
|
|
});
|
|
}
|
|
|
|
// ─── Update ───────────────────────────────────────────────────────────────────
|
|
|
|
export function useUpdateCardPin(id: number) {
|
|
const qc = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation({
|
|
mutationFn: (payload: UpdateCardPinPayload) => cardPinApi.update(id, payload),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() });
|
|
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.detail(id) });
|
|
toast.success(t("cardPin.updateSuccess", "Kart pin bukja üýtgedildi"));
|
|
},
|
|
});
|
|
}
|
|
|
|
// ─── Delete ───────────────────────────────────────────────────────────────────
|
|
|
|
export function useDeleteCardPin() {
|
|
const qc = useQueryClient();
|
|
const { t } = useTranslation();
|
|
|
|
return useMutation({
|
|
mutationFn: (id: number) => cardPinApi.delete(id),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() });
|
|
toast.success(t("cardPin.deleteSuccess", "Kart pin bukja pozuldy"));
|
|
},
|
|
});
|
|
}
|