import { useMutation, useQuery } from '@tanstack/react-query'; import { getUser, getUser_server, updateOrganizationUser, getMyUserInfo, getMyUserInfo_server, getUserFavorites, getUserFavorites_server, createUserFavorite, deleteUserFavorite, updateUserFavorites, getUserList, getUserList_server } from './requests'; import { useMemoizedFn } from '@/hooks'; import { QueryClient, useQueryClient } from '@tanstack/react-query'; import { queryKeys } from '@/api/query_keys'; import type { UsersFavoritePostPayload, UserFavoriteDeletePayload, UserUpdateFavoritesPayload, UserRequestUserListPayload } from '@/api/request_interfaces/user/interfaces'; export const useGetMyUserInfo = () => { const queryFn = useMemoizedFn(async () => { return getMyUserInfo(); }); return useQuery({ ...queryKeys.userGetUserMyself, queryFn, enabled: false //This is a server only query }); }; export const prefetchGetMyUserInfo = async ( params: Parameters[0], queryClientProp?: QueryClient ) => { const queryClient = queryClientProp || new QueryClient(); const initialData = await getMyUserInfo_server(params); await queryClient.prefetchQuery({ ...queryKeys.userGetUserMyself, queryFn: () => initialData!, initialData }); return { queryClient, initialData }; }; export const useGetUser = (params: Parameters[0]) => { const queryFn = useMemoizedFn(() => getUser(params)); return useQuery({ ...queryKeys.userGetUser(params.userId), queryFn }); }; export const useUpdateUser = () => { const queryClient = useQueryClient(); const mutationFn = useMemoizedFn(async (params: Parameters[0]) => { const options = queryKeys.userGetUser(params.userId); queryClient.setQueryData(options.queryKey, (oldData) => { return { ...oldData!, ...params }; }); const res = await updateOrganizationUser(params); return res; }); return useMutation({ mutationFn: mutationFn }); }; export const prefetchGetUser = async (userId: string, queryClientProp?: QueryClient) => { const queryClient = queryClientProp || new QueryClient(); await queryClient.prefetchQuery({ ...queryKeys.userGetUser(userId), queryFn: () => getUser_server({ userId }) }); return queryClient; }; export const useGetUserFavorites = () => { const queryFn = useMemoizedFn(async () => getUserFavorites()); return useQuery({ ...queryKeys.favoritesGetList, queryFn }); }; export const prefetchGetUserFavorites = async (queryClientProp?: QueryClient) => { const queryClient = queryClientProp || new QueryClient(); await queryClient.prefetchQuery({ ...queryKeys.favoritesGetList, queryFn: () => getUserFavorites_server() }); return queryClient; }; export const useAddUserFavorite = () => { const queryClient = useQueryClient(); return useMutation({ mutationFn: createUserFavorite, onMutate: (params) => { queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => { return [params, ...(prev || [])]; }); }, onSettled: () => { queryClient.invalidateQueries(queryKeys.favoritesGetList); } }); }; export const useDeleteUserFavorite = () => { const queryClient = useQueryClient(); return useMutation({ mutationFn: deleteUserFavorite, onMutate: (params) => { queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => { return prev?.filter((fav) => fav.id !== params.id); }); }, onSettled: () => { queryClient.invalidateQueries(queryKeys.favoritesGetList); } }); }; export const useUpdateUserFavorites = () => { const queryClient = useQueryClient(); return useMutation({ mutationFn: updateUserFavorites, onMutate: (params) => { queryClient.setQueryData(queryKeys.favoritesGetList.queryKey, (prev) => { return prev?.filter((fav, index) => { const id = fav.id || fav.collection_id; const favorite = (prev || []).find((f) => f.id === id || f.collection_id === id)!; return { ...favorite, index }; }); }); } }); }; export const useGetUserList = (params: UserRequestUserListPayload) => { const queryFn = useMemoizedFn(() => getUserList(params)); return useQuery({ ...queryKeys.userGetUserList(params), queryFn }); }; export const prefetchGetUserList = async ( params: UserRequestUserListPayload, queryClientProp?: QueryClient ) => { const queryClient = queryClientProp || new QueryClient(); await queryClient.prefetchQuery({ ...queryKeys.userGetUserList(params), queryFn: () => getUserList_server(params) }); return queryClient; };