Presupuestador_web/client/src/app/quotes/hooks/useQuotes.tsx

276 lines
7.5 KiB
TypeScript
Raw Normal View History

2024-08-21 11:49:52 +00:00
import { useDownloader } from "@/lib/hooks";
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";
2024-08-21 11:49:52 +00:00
import { useCallback, useMemo, useState } 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();
2024-08-20 21:40:37 +00:00
const actions = {
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-21 11:49:52 +00:00
useReport2: (id?: string, params?: UseQuotesReportParamsType) => {
2024-08-19 16:13:07 +00:00
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,
2024-08-20 21:40:37 +00:00
select: useCallback(
(data: ArrayBuffer) => ({
original: data,
data: new Uint8Array(data),
}),
[]
),
2024-08-19 16:13:07 +00:00
...params,
}),
cancelQuery: () => queryClient.cancelQueries({ queryKey }),
};
},
2024-08-20 21:40:37 +00:00
getQuotePDFDownloadURL: (id: string) => `${dataSource.getApiUrl()}/quotes/${id}/report`,
getQuotePDFFilename: (quote: IListQuotes_Response_DTO | IGetQuote_Response_DTO) =>
2024-08-21 11:16:56 +00:00
`filename-quote.pdf`,
2024-08-20 21:40:37 +00:00
2024-08-21 11:16:56 +00:00
/*useDownload2: (id?: string, params?: UseQuotesReportParamsType) => {
2024-08-20 21:40:37 +00:00
const queryKey = useMemo(
() => keys().data().resource("quotes").action("report").id(id).params().get(),
[id]
);
const { data, error, refetch, isFetching, isError } = useCustom<
IDownloadPDFDataProviderResponse,
TDataSourceError
>({
queryKey,
queryFn: () =>
dataSource.downloadPDF({
url: `${dataSource.getApiUrl()}/quotes/${id}/report`,
}),
enabled: false,
refetchInterval: false,
...params,
});
const download = () => {
console.log("pido");
refetch().then((result) => {
console.log("termino");
if (result.isSuccess) {
const blob = data!.filedata;
const link = document.createElement("a");
const url = window.URL.createObjectURL(blob);
link.href = url;
link.setAttribute("download", data!.filename); // Nombre del archivo
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(url);
}
});
};
return { download, error, isFetching, isError };
2024-08-21 11:16:56 +00:00
},*/
2024-08-20 21:40:37 +00:00
2024-08-21 11:49:52 +00:00
useReport: () => {
const auth = dataSource.getApiAuthorization();
const [reportBlob, setReportBlob] = useState<Blob>();
const downloader = useDownloader({
headers: {
Authorization: auth,
},
customHandleDownload: (data: Blob) => {
const blobData = [data];
const blob = new Blob(blobData, {
type: "application/pdf",
});
setReportBlob(blob);
return true;
},
});
const download = (id: string) => {
const url = actions.getQuotePDFDownloadURL(id);
downloader.download(url, "");
return reportBlob;
};
return {
...downloader,
download,
};
},
2024-08-20 21:40:37 +00:00
useDownloader: () => {
const auth = dataSource.getApiAuthorization();
const downloader = useDownloader({
headers: {
Authorization: auth,
},
});
const download = (id: string, filename?: string) => {
2024-08-21 11:16:56 +00:00
const url = actions.getQuotePDFDownloadURL(id);
return downloader.download(url, filename ?? "ssaas");
2024-08-20 21:40:37 +00:00
};
return {
...downloader,
download,
};
},
2024-06-29 19:39:25 +00:00
};
2024-08-20 21:40:37 +00:00
return actions;
2024-06-29 19:39:25 +00:00
};