2024-08-18 20:39:06 +00:00
|
|
|
import { UseListQueryResult, useCustom, useList, useOne, useSave } from "@/lib/hooks/useDataSource";
|
2024-07-11 18:31:01 +00:00
|
|
|
import {
|
|
|
|
|
IFilterItemDataProviderParam,
|
|
|
|
|
IGetListDataProviderParams,
|
|
|
|
|
} from "@/lib/hooks/useDataSource/DataSource";
|
2024-06-29 19:39:25 +00:00
|
|
|
import { TDataSourceError } from "@/lib/hooks/useDataSource/types";
|
|
|
|
|
import { useDataSource } from "@/lib/hooks/useDataSource/useDataSource";
|
|
|
|
|
import { useQueryKey } from "@/lib/hooks/useQueryKey";
|
|
|
|
|
import {
|
|
|
|
|
ICreateQuote_Request_DTO,
|
|
|
|
|
ICreateQuote_Response_DTO,
|
|
|
|
|
IGetQuote_Response_DTO,
|
2024-07-11 18:31:01 +00:00
|
|
|
IListQuotes_Response_DTO,
|
|
|
|
|
IListResponse_DTO,
|
2024-08-18 20:39:06 +00:00
|
|
|
IReportQuote_Response_DTO,
|
2024-07-09 16:21:12 +00:00
|
|
|
IUpdateQuote_Request_DTO,
|
|
|
|
|
IUpdateQuote_Response_DTO,
|
2024-06-29 19:39:25 +00:00
|
|
|
UniqueID,
|
|
|
|
|
} from "@shared/contexts";
|
2024-08-19 16:13:07 +00:00
|
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
import { useCallback, useMemo } from "react";
|
2024-06-29 19:39:25 +00:00
|
|
|
|
2024-07-11 18:31:01 +00:00
|
|
|
export type UseQuotesListParams = Omit<IGetListDataProviderParams, "filters" | "resource"> & {
|
|
|
|
|
status?: string;
|
|
|
|
|
enabled?: boolean;
|
|
|
|
|
queryOptions?: Record<string, unknown>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type UseQuotesListResponse = UseListQueryResult<
|
|
|
|
|
IListResponse_DTO<IListQuotes_Response_DTO>,
|
|
|
|
|
unknown
|
|
|
|
|
>;
|
|
|
|
|
|
2024-06-29 19:39:25 +00:00
|
|
|
export type UseQuotesGetParamsType = {
|
|
|
|
|
enabled?: boolean;
|
|
|
|
|
queryOptions?: Record<string, unknown>;
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-18 20:39:06 +00:00
|
|
|
export type UseQuotesReportParamsType = {
|
|
|
|
|
enabled?: boolean;
|
|
|
|
|
queryOptions?: Record<string, unknown>;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-11 18:31:01 +00:00
|
|
|
const quoteStatusFilter: Record<string, IFilterItemDataProviderParam> = {
|
|
|
|
|
draft: {
|
|
|
|
|
field: "status",
|
|
|
|
|
operator: "eq",
|
|
|
|
|
value: "draft",
|
|
|
|
|
},
|
|
|
|
|
archived: {
|
|
|
|
|
field: "status",
|
|
|
|
|
operator: "eq",
|
|
|
|
|
value: "archived",
|
|
|
|
|
},
|
|
|
|
|
active: {
|
|
|
|
|
field: "status",
|
|
|
|
|
operator: "eq",
|
|
|
|
|
value: "active",
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-09 16:21:12 +00:00
|
|
|
export const useQuotes = () => {
|
2024-06-29 19:39:25 +00:00
|
|
|
const dataSource = useDataSource();
|
|
|
|
|
const keys = useQueryKey();
|
|
|
|
|
|
|
|
|
|
return {
|
2024-07-11 18:31:01 +00:00
|
|
|
useList: (params: UseQuotesListParams): UseQuotesListResponse => {
|
|
|
|
|
const dataSource = useDataSource();
|
|
|
|
|
const keys = useQueryKey();
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
pagination,
|
|
|
|
|
status = "draft",
|
|
|
|
|
quickSearchTerm = undefined,
|
|
|
|
|
enabled = true,
|
|
|
|
|
queryOptions,
|
|
|
|
|
} = params;
|
|
|
|
|
|
|
|
|
|
return useList({
|
|
|
|
|
queryKey: keys().data().resource("quotes").action("list").params(params).get(),
|
|
|
|
|
queryFn: () => {
|
|
|
|
|
return dataSource.getList({
|
|
|
|
|
resource: "quotes",
|
|
|
|
|
quickSearchTerm,
|
|
|
|
|
filters: status !== "all" ? Array.of(quoteStatusFilter[status]) : undefined,
|
|
|
|
|
pagination,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
enabled,
|
|
|
|
|
queryOptions,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2024-07-09 16:21:12 +00:00
|
|
|
useCreate: () =>
|
2024-07-03 15:15:52 +00:00
|
|
|
useSave<ICreateQuote_Response_DTO, TDataSourceError, ICreateQuote_Request_DTO>({
|
2024-07-12 18:13:17 +00:00
|
|
|
//mutationKey: keys().data().resource("quotes").action("one").id("").params().get(),
|
2024-07-03 15:15:52 +00:00
|
|
|
mutationFn: (data) => {
|
2024-07-11 18:31:01 +00:00
|
|
|
const { date } = data;
|
2024-06-29 19:39:25 +00:00
|
|
|
|
2024-07-11 18:31:01 +00:00
|
|
|
const id = UniqueID.generateNewID().object.toString();
|
|
|
|
|
const status = "draft";
|
2024-06-29 19:39:25 +00:00
|
|
|
|
2024-07-03 15:15:52 +00:00
|
|
|
return dataSource.createOne({
|
|
|
|
|
resource: "quotes",
|
|
|
|
|
data: {
|
|
|
|
|
...data,
|
2024-07-11 18:31:01 +00:00
|
|
|
date: new Date(date).toISOString().slice(0, 10),
|
2024-07-03 15:15:52 +00:00
|
|
|
status,
|
|
|
|
|
id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}),
|
2024-07-09 16:21:12 +00:00
|
|
|
|
|
|
|
|
useUpdate: (id: string) =>
|
|
|
|
|
useSave<IUpdateQuote_Response_DTO, TDataSourceError, IUpdateQuote_Request_DTO>({
|
|
|
|
|
mutationKey: keys().data().resource("quotes").action("one").id(id).params().get(),
|
|
|
|
|
mutationFn: (data) => {
|
2024-07-11 18:31:01 +00:00
|
|
|
const { date } = data;
|
2024-07-11 17:43:08 +00:00
|
|
|
|
2024-07-09 16:21:12 +00:00
|
|
|
return dataSource.updateOne({
|
|
|
|
|
resource: "quotes",
|
|
|
|
|
id,
|
2024-07-11 16:40:46 +00:00
|
|
|
data: {
|
|
|
|
|
...data,
|
2024-07-11 18:31:01 +00:00
|
|
|
date: new Date(date).toISOString().slice(0, 10),
|
2024-07-11 16:40:46 +00:00
|
|
|
},
|
2024-07-09 16:21:12 +00:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}),
|
2024-08-18 20:39:06 +00:00
|
|
|
|
2024-08-19 09:33:42 +00:00
|
|
|
useOne: (id?: string, params?: UseQuotesGetParamsType) =>
|
|
|
|
|
useOne<IGetQuote_Response_DTO>({
|
|
|
|
|
queryKey: keys().data().resource("quotes").action("one").id(id).params().get(),
|
|
|
|
|
queryFn: () =>
|
|
|
|
|
dataSource.getOne({
|
|
|
|
|
resource: "quotes",
|
|
|
|
|
id: String(id),
|
|
|
|
|
}),
|
|
|
|
|
enabled: !!id,
|
|
|
|
|
...params,
|
|
|
|
|
}),
|
|
|
|
|
|
2024-08-19 16:13:07 +00:00
|
|
|
useReport: (id?: string, params?: UseQuotesReportParamsType) => {
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
const queryKey = useMemo(
|
|
|
|
|
() => keys().data().resource("quotes").action("report").id(id).params().get(),
|
|
|
|
|
[id]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...useCustom<ArrayBuffer, TDataSourceError, IReportQuote_Response_DTO>({
|
|
|
|
|
queryKey,
|
|
|
|
|
queryFn: ({ signal }) =>
|
|
|
|
|
dataSource.custom({
|
|
|
|
|
url: `${dataSource.getApiUrl()}/quotes/${id}/report`,
|
|
|
|
|
method: "get",
|
|
|
|
|
responseType: "arraybuffer",
|
|
|
|
|
signal,
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
enabled: !!id,
|
|
|
|
|
select: useCallback((data: ArrayBuffer) => new Uint8Array(data), []),
|
|
|
|
|
...params,
|
|
|
|
|
}),
|
|
|
|
|
cancelQuery: () => queryClient.cancelQueries({ queryKey }),
|
|
|
|
|
};
|
|
|
|
|
},
|
2024-06-29 19:39:25 +00:00
|
|
|
};
|
|
|
|
|
};
|