started connect with api
This commit is contained in:
1 parent
0ca218505d
commit
6da59f42d9
112 files changed
+7091
-6989
No files matched your search
@@ -1,77 +1,81 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { toast } from 'sonner'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { cardPinApi, type CardPinListParams, type CardPinCreatePayload, type CardPinUpdatePayload } from '../api/cardPinApi'
|
||||
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-pins'] as const,
|
||||
lists: () => [...CARD_PIN_KEYS.all, 'list'] as const,
|
||||
list: (p: CardPinListParams) => [...CARD_PIN_KEYS.lists(), p] as const,
|
||||
detail: (id: string) => [...CARD_PIN_KEYS.all, 'detail', id] as const,
|
||||
}
|
||||
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(params),
|
||||
queryFn: () => cardPinApi.list(params),
|
||||
queryKey: CARD_PIN_KEYS.list(queryParams),
|
||||
queryFn: ({ signal }) => cardPinApi.list(queryParams, signal),
|
||||
placeholderData: (prev) => prev,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Detail ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useCardPin(id: string) {
|
||||
export function useCardPin(id: number) {
|
||||
return useQuery({
|
||||
queryKey: CARD_PIN_KEYS.detail(id),
|
||||
queryFn: () => cardPinApi.getById(id),
|
||||
enabled: !!id,
|
||||
})
|
||||
queryFn: () => cardPinApi.getById(id),
|
||||
enabled: !!id,
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Create ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useCreateCardPin() {
|
||||
const qc = useQueryClient()
|
||||
const { t } = useTranslation()
|
||||
const qc = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: CardPinCreatePayload) => cardPinApi.create(payload),
|
||||
mutationFn: (payload: CreateCardPinPayload) => cardPinApi.create(payload),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() })
|
||||
toast.success(t('cardPin.createSuccess', 'Kart pin bukja döredildi'))
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() });
|
||||
toast.success(t("cardPin.createSuccess", "Kart pin bukja döredildi"));
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Update ───────────────────────────────────────────────────────────────────
|
||||
|
||||
export function useUpdateCardPin(id: string) {
|
||||
const qc = useQueryClient()
|
||||
const { t } = useTranslation()
|
||||
export function useUpdateCardPin(id: number) {
|
||||
const qc = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (payload: CardPinUpdatePayload) => cardPinApi.update(id, payload),
|
||||
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'))
|
||||
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()
|
||||
const qc = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => cardPinApi.delete(id),
|
||||
mutationFn: (id: number) => cardPinApi.delete(id),
|
||||
onSuccess: () => {
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() })
|
||||
toast.success(t('cardPin.deleteSuccess', 'Kart pin bukja pozuldy'))
|
||||
qc.invalidateQueries({ queryKey: CARD_PIN_KEYS.lists() });
|
||||
toast.success(t("cardPin.deleteSuccess", "Kart pin bukja pozuldy"));
|
||||
},
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user