2024-06-29 19:39:25 +00:00
|
|
|
import { useOne, useSave } from "@/lib/hooks/useDataSource";
|
|
|
|
|
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-09 16:21:12 +00:00
|
|
|
IUpdateQuote_Request_DTO,
|
|
|
|
|
IUpdateQuote_Response_DTO,
|
2024-06-29 19:39:25 +00:00
|
|
|
UniqueID,
|
|
|
|
|
} from "@shared/contexts";
|
|
|
|
|
|
|
|
|
|
export type UseQuotesGetParamsType = {
|
|
|
|
|
enabled?: boolean;
|
|
|
|
|
queryOptions?: Record<string, unknown>;
|
|
|
|
|
};
|
|
|
|
|
|
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-09 16:21:12 +00:00
|
|
|
useOne: (id?: string, params?: UseQuotesGetParamsType) =>
|
2024-07-03 15:15:52 +00:00
|
|
|
useOne<IGetQuote_Response_DTO>({
|
|
|
|
|
queryKey: keys().data().resource("quotes").action("one").id("").params().get(),
|
|
|
|
|
queryFn: () =>
|
|
|
|
|
dataSource.getOne({
|
|
|
|
|
resource: "quotes",
|
2024-07-09 16:21:12 +00:00
|
|
|
id: String(id),
|
2024-07-03 15:15:52 +00:00
|
|
|
}),
|
2024-07-09 16:21:12 +00:00
|
|
|
enabled: !!id,
|
2024-07-03 15:15:52 +00:00
|
|
|
...params,
|
|
|
|
|
}),
|
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>({
|
|
|
|
|
mutationKey: keys().data().resource("quotes").action("one").id("").params().get(),
|
|
|
|
|
mutationFn: (data) => {
|
|
|
|
|
let { id, status } = data;
|
2024-06-29 19:39:25 +00:00
|
|
|
|
2024-07-03 15:15:52 +00:00
|
|
|
if (!id) {
|
|
|
|
|
id = UniqueID.generateNewID().object.toString();
|
|
|
|
|
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,
|
|
|
|
|
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) => {
|
|
|
|
|
return dataSource.updateOne({
|
|
|
|
|
resource: "quotes",
|
|
|
|
|
id,
|
|
|
|
|
data,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}),
|
2024-06-29 19:39:25 +00:00
|
|
|
};
|
|
|
|
|
};
|