add tariffs types/hook
This commit is contained in:
parent
9e28031d9b
commit
2b8978567b
39
src/hooks/useTariffs.ts
Normal file
39
src/hooks/useTariffs.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { makeRequest } from "../api";
|
||||||
|
import { Tariff, GetTariffsResponse } from "../model/tariff";
|
||||||
|
|
||||||
|
|
||||||
|
export function useTariffs({ url, tariffsPerPage, apiPage, onNewTariffs, onError }: {
|
||||||
|
url: string;
|
||||||
|
tariffsPerPage: number;
|
||||||
|
apiPage: number;
|
||||||
|
onNewTariffs: (response: Tariff[]) => void;
|
||||||
|
onError: (error: Error) => void;
|
||||||
|
}) {
|
||||||
|
const [fetchState, setFetchState] = useState<"fetching" | "idle" | "all fetched">("idle");
|
||||||
|
|
||||||
|
useEffect(function fetchTickets() {
|
||||||
|
const controller = new AbortController();
|
||||||
|
|
||||||
|
setFetchState("fetching");
|
||||||
|
makeRequest<never, GetTariffsResponse>({
|
||||||
|
url,
|
||||||
|
method: "get",
|
||||||
|
useToken: true,
|
||||||
|
signal: controller.signal,
|
||||||
|
}).then((result) => {
|
||||||
|
// devlog("GetTicketsResponse", result);
|
||||||
|
if (result.tariffs.length > 0) {
|
||||||
|
onNewTariffs(result.tariffs);
|
||||||
|
setFetchState("idle");
|
||||||
|
} else setFetchState("all fetched");
|
||||||
|
}).catch(error => {
|
||||||
|
console.log("Error fetching tariffs", error);
|
||||||
|
onError(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => controller.abort();
|
||||||
|
}, [onError, onNewTariffs, apiPage, tariffsPerPage, url]);
|
||||||
|
|
||||||
|
return fetchState;
|
||||||
|
}
|
19
src/model/privilege.ts
Normal file
19
src/model/privilege.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
export interface Privilege {
|
||||||
|
_id: string;
|
||||||
|
name: string;
|
||||||
|
privilegeId: string;
|
||||||
|
serviceKey: string;
|
||||||
|
description: string;
|
||||||
|
type: "day" | "count";
|
||||||
|
value: PrivilegeValueType;
|
||||||
|
price: number;
|
||||||
|
updatedAt?: string;
|
||||||
|
isDeleted?: boolean;
|
||||||
|
createdAt?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PrivilegeMap = Record<string, Privilege[]>;
|
||||||
|
|
||||||
|
export type PrivilegeValueType = "шаблон" | "день" | "МБ";
|
||||||
|
|
||||||
|
export type PrivilegeWithAmount = Omit<Privilege, "_id"> & { amount: number; };
|
19
src/model/tariff.ts
Normal file
19
src/model/tariff.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { PrivilegeWithAmount } from "./privilege";
|
||||||
|
|
||||||
|
|
||||||
|
export interface GetTariffsResponse {
|
||||||
|
totalPages: number;
|
||||||
|
tariffs: Tariff[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Tariff {
|
||||||
|
_id: string;
|
||||||
|
name: string;
|
||||||
|
price?: number;
|
||||||
|
isCustom: boolean;
|
||||||
|
privilegies: PrivilegeWithAmount[];
|
||||||
|
isDeleted: boolean;
|
||||||
|
createdAt?: string;
|
||||||
|
updatedAt?: string;
|
||||||
|
deletedAt?: string;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user