diff options
| author | Kevin Hoerr <kjhoerr@noreply.cybr.es> | 2022-08-14 21:35:45 +0000 |
|---|---|---|
| committer | Kevin Hoerr <kjhoerr@noreply.cybr.es> | 2022-08-14 21:35:45 +0000 |
| commit | c04674fa74c2e43535181431aef5d891f8839619 (patch) | |
| tree | 2f7ce3b61590b39254bc22ac96272b7533ed96d2 /src/util | |
| parent | 461b1fa053bcc86d06156574ab59fa7000dbf69e (diff) | |
| download | pantry-c04674fa74c2e43535181431aef5d891f8839619.tar.gz pantry-c04674fa74c2e43535181431aef5d891f8839619.tar.bz2 pantry-c04674fa74c2e43535181431aef5d891f8839619.zip | |
Merge planner code (#3)
Reviewed-on: https://git.submelon.dev/kjhoerr/pantry/pulls/3
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/pantry-item-resource.ts | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/src/util/pantry-item-resource.ts b/src/util/pantry-item-resource.ts new file mode 100644 index 0000000..aedb286 --- /dev/null +++ b/src/util/pantry-item-resource.ts @@ -0,0 +1,212 @@ +/** + * Generated by orval v6.9.6 🍺 + * Do not edit manually. + * pantry API + * OpenAPI spec version: 1.0.0-SNAPSHOT + */ +import { useQuery, useMutation } from "@tanstack/react-query"; +import type { + UseQueryOptions, + UseMutationOptions, + QueryFunction, + MutationFunction, + UseQueryResult, + QueryKey, +} from "@tanstack/react-query"; +import type { PantryItem } from "../model"; +import { useMutator } from "../conf/mutator"; +import type { ErrorType } from "../conf/mutator"; + +export const useGetItemsHook = () => { + const getItems = useMutator<PantryItem[]>(); + + return (signal?: AbortSignal) => { + return getItems({ url: `/items`, method: "get", signal }); + }; +}; + +export const getGetItemsQueryKey = () => [`/items`]; + +export type GetItemsQueryResult = NonNullable< + Awaited<ReturnType<ReturnType<typeof useGetItemsHook>>> +>; +export type GetItemsQueryError = ErrorType<unknown>; + +export const useGetItems = < + TData = Awaited<ReturnType<ReturnType<typeof useGetItemsHook>>>, + TError = ErrorType<unknown> +>(options?: { + query?: UseQueryOptions< + Awaited<ReturnType<ReturnType<typeof useGetItemsHook>>>, + TError, + TData + >; +}): UseQueryResult<TData, TError> & { queryKey: QueryKey } => { + const { query: queryOptions } = options ?? {}; + + const queryKey = queryOptions?.queryKey ?? getGetItemsQueryKey(); + + const getItems = useGetItemsHook(); + + const queryFn: QueryFunction< + Awaited<ReturnType<ReturnType<typeof useGetItemsHook>>> + > = ({ signal }) => getItems(signal); + + const query = useQuery< + Awaited<ReturnType<ReturnType<typeof useGetItemsHook>>>, + TError, + TData + >(queryKey, queryFn, queryOptions) as UseQueryResult<TData, TError> & { + queryKey: QueryKey; + }; + + query.queryKey = queryKey; + + return query; +}; + +export const usePostItemsHook = () => { + const postItems = useMutator<PantryItem>(); + + return (pantryItem: PantryItem) => { + return postItems({ + url: `/items`, + method: "post", + headers: { "Content-Type": "application/json" }, + data: pantryItem, + }); + }; +}; + +export type PostItemsMutationResult = NonNullable< + Awaited<ReturnType<ReturnType<typeof usePostItemsHook>>> +>; +export type PostItemsMutationBody = PantryItem; +export type PostItemsMutationError = ErrorType<unknown>; + +export const usePostItems = < + TError = ErrorType<unknown>, + TContext = unknown +>(options?: { + mutation?: UseMutationOptions< + Awaited<ReturnType<ReturnType<typeof usePostItemsHook>>>, + TError, + { data: PantryItem }, + TContext + >; +}) => { + const { mutation: mutationOptions } = options ?? {}; + + const postItems = usePostItemsHook(); + + const mutationFn: MutationFunction< + Awaited<ReturnType<ReturnType<typeof usePostItemsHook>>>, + { data: PantryItem } + > = (props) => { + const { data } = props ?? {}; + + return postItems(data); + }; + + return useMutation< + Awaited<ReturnType<typeof postItems>>, + TError, + { data: PantryItem }, + TContext + >(mutationFn, mutationOptions); +}; +export const usePutItemsIdHook = () => { + const putItemsId = useMutator<PantryItem>(); + + return (id: number, pantryItem: PantryItem) => { + return putItemsId({ + url: `/items/${id}`, + method: "put", + headers: { "Content-Type": "application/json" }, + data: pantryItem, + }); + }; +}; + +export type PutItemsIdMutationResult = NonNullable< + Awaited<ReturnType<ReturnType<typeof usePutItemsIdHook>>> +>; +export type PutItemsIdMutationBody = PantryItem; +export type PutItemsIdMutationError = ErrorType<unknown>; + +export const usePutItemsId = < + TError = ErrorType<unknown>, + TContext = unknown +>(options?: { + mutation?: UseMutationOptions< + Awaited<ReturnType<ReturnType<typeof usePutItemsIdHook>>>, + TError, + { id: number; data: PantryItem }, + TContext + >; +}) => { + const { mutation: mutationOptions } = options ?? {}; + + const putItemsId = usePutItemsIdHook(); + + const mutationFn: MutationFunction< + Awaited<ReturnType<ReturnType<typeof usePutItemsIdHook>>>, + { id: number; data: PantryItem } + > = (props) => { + const { id, data } = props ?? {}; + + return putItemsId(id, data); + }; + + return useMutation< + Awaited<ReturnType<typeof putItemsId>>, + TError, + { id: number; data: PantryItem }, + TContext + >(mutationFn, mutationOptions); +}; +export const useDeleteItemsIdHook = () => { + const deleteItemsId = useMutator<void>(); + + return (id: number) => { + return deleteItemsId({ url: `/items/${id}`, method: "delete" }); + }; +}; + +export type DeleteItemsIdMutationResult = NonNullable< + Awaited<ReturnType<ReturnType<typeof useDeleteItemsIdHook>>> +>; + +export type DeleteItemsIdMutationError = ErrorType<unknown>; + +export const useDeleteItemsId = < + TError = ErrorType<unknown>, + TContext = unknown +>(options?: { + mutation?: UseMutationOptions< + Awaited<ReturnType<ReturnType<typeof useDeleteItemsIdHook>>>, + TError, + { id: number }, + TContext + >; +}) => { + const { mutation: mutationOptions } = options ?? {}; + + const deleteItemsId = useDeleteItemsIdHook(); + + const mutationFn: MutationFunction< + Awaited<ReturnType<ReturnType<typeof useDeleteItemsIdHook>>>, + { id: number } + > = (props) => { + const { id } = props ?? {}; + + return deleteItemsId(id); + }; + + return useMutation< + Awaited<ReturnType<typeof deleteItemsId>>, + TError, + { id: number }, + TContext + >(mutationFn, mutationOptions); +}; |
