favorite providers update

This commit is contained in:
Nate Kelley 2025-03-10 22:36:25 -06:00
parent 11ddf973cf
commit bbd4430a9d
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
9 changed files with 193 additions and 140 deletions

View File

@ -4,11 +4,24 @@ import {
getUser_server,
updateOrganizationUser,
getMyUserInfo,
getMyUserInfo_server
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 () => {
@ -71,3 +84,88 @@ export const prefetchGetUser = async (userId: string, queryClientProp?: QueryCli
});
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;
};

View File

@ -1,5 +1,16 @@
import { BASE_URL } from '@/api/buster_rest/config';
import type { OrganizationUser, BusterUserResponse } from '@/api/asset_interfaces/users';
import type {
OrganizationUser,
BusterUserResponse,
BusterUserFavorite,
BusterUserListItem
} from '@/api/asset_interfaces/users';
import type {
UsersFavoritePostPayload,
UserFavoriteDeletePayload,
UserUpdateFavoritesPayload,
UserRequestUserListPayload
} from '@/api/request_interfaces/user/interfaces';
import { mainApi } from '../instances';
import { serverFetch } from '../../createServerInstance';
@ -76,3 +87,60 @@ export const inviteUser = async ({
team_ids
});
};
export const getUserFavorites = async () => {
return mainApi.get<BusterUserFavorite[]>(`/users/favorites`).then((response) => response.data);
};
export const getUserFavorites_server = async () => {
return serverFetch<BusterUserFavorite[]>(`/users/favorites`);
};
export const createUserFavorite = async (payload: UsersFavoritePostPayload) => {
return mainApi
.post<BusterUserFavorite[]>(`/users/favorites`, payload)
.then((response) => response.data);
};
export const createUserFavorite_server = async (payload: UsersFavoritePostPayload) => {
return serverFetch<BusterUserFavorite[]>(`/users/favorites`, {
method: 'POST',
body: JSON.stringify(payload)
});
};
export const deleteUserFavorite = async (payload: UserFavoriteDeletePayload) => {
return mainApi
.delete<BusterUserFavorite[]>(`/users/favorites`, { data: payload })
.then((response) => response.data);
};
export const deleteUserFavorite_server = async (payload: UserFavoriteDeletePayload) => {
return serverFetch<BusterUserFavorite[]>(`/users/favorites`, {
method: 'DELETE',
body: JSON.stringify(payload)
});
};
export const updateUserFavorites = async (payload: UserUpdateFavoritesPayload) => {
return mainApi
.put<BusterUserFavorite[]>(`/users/favorites`, payload)
.then((response) => response.data);
};
export const updateUserFavorites_server = async (payload: UserUpdateFavoritesPayload) => {
return serverFetch<BusterUserFavorite[]>(`/users/favorites`, {
method: 'PUT',
body: JSON.stringify(payload)
});
};
export const getUserList = async (payload: UserRequestUserListPayload) => {
return mainApi
.get<BusterUserListItem[]>(`/users/list`, { params: payload })
.then((response) => response.data);
};
export const getUserList_server = async (payload: UserRequestUserListPayload) => {
return serverFetch<BusterUserListItem[]>(`/users/list`, { params: payload });
};

View File

@ -1,8 +1,7 @@
import type { UserEmits, UserResponses, UserResponsesTypes } from './user';
import type { ChatEmits, ChatResponseTypes, ChatsResponses } from './chats';
export type BusterSocketRequest = UserEmits | ChatEmits;
export type BusterSocketRequest = ChatEmits;
export type BusterSocketResponse = UserResponsesTypes | ChatResponseTypes;
export type BusterSocketResponse = ChatResponseTypes;
export type BusterSocketResponseRoute = keyof typeof UserResponses | keyof typeof ChatsResponses;
export type BusterSocketResponseRoute = keyof typeof ChatsResponses;

View File

@ -1,2 +0,0 @@
export * from './userRequests';
export * from './userResponses';

View File

@ -1,36 +0,0 @@
import type { BusterSocketRequestBase } from '../base_interfaces';
import type {
UsersFavoritePostPayload,
UserFavoriteDeletePayload,
UserUpdateFavoritesPayload,
UserRequestUserListPayload
} from '@/api/request_interfaces/user/interfaces';
export type UsersFavoritePost = BusterSocketRequestBase<
'/users/favorites/post',
UsersFavoritePostPayload
>;
export type UsersFavoriteList = BusterSocketRequestBase<'/users/favorites/list', {}>;
export type UserFavoriteDelete = BusterSocketRequestBase<
'/users/favorites/delete',
UserFavoriteDeletePayload
>;
export type UserUpdateFavorites = BusterSocketRequestBase<
'/users/favorites/update',
UserUpdateFavoritesPayload
>;
export type UserRequestUserList = BusterSocketRequestBase<
'/users/list',
UserRequestUserListPayload
>;
export type UserEmits =
| UsersFavoritePost
| UsersFavoriteList
| UserFavoriteDelete
| UserUpdateFavorites
| UserRequestUserList;

View File

@ -1,59 +0,0 @@
import type {
BusterUserFavorite,
BusterUserListItem,
BusterUserPalette,
BusterUserResponse
} from '@/api/asset_interfaces/users';
export enum UserResponses {
'/users/favorites/list:listFavorites' = '/users/favorites/list:listFavorites',
'/users/favorites/post:createFavorite' = '/users/favorites/post:createFavorite',
'/users/get:getUser' = '/users/get:getUser',
'/users/favorites/update:updateFavorite' = '/users/favorites/update:updateFavorite',
'/users/list:listUsersTeams' = '/users/list:listUsersTeams',
'/users/list:listUsers' = '/users/list:listUsers'
}
export type UserColorsPost_createUserColorPalette = {
route: '/users/colors/post:createUserColorPalette';
callback: (d: BusterUserPalette[]) => void;
onError?: (d: unknown) => void;
};
export type UserFavoriteList_listFavorites = {
route: '/users/favorites/list:listFavorites';
callback: (d: BusterUserFavorite[]) => void;
onError?: (d: unknown) => void;
};
export type UserFavoritesPost_createFavorite = {
route: '/users/favorites/post:createFavorite';
callback: (d: BusterUserFavorite[]) => void;
onError?: (d: unknown) => void;
};
export type UserFavoritesUpdate_updateFavorite = {
route: '/users/favorites/update:updateFavorite';
callback: (d: BusterUserFavorite[]) => void;
onError?: (d: unknown) => void;
};
export type UserGet_getUser = {
route: '/users/get:getUser';
callback: (d: BusterUserResponse) => void;
onError?: (d: unknown) => void;
};
export type UserList_getUserList = {
route: '/users/list:listUsers';
callback: (d: BusterUserListItem[]) => void;
onError?: (d: unknown) => void;
};
export type UserResponsesTypes =
| UserColorsPost_createUserColorPalette
| UserFavoriteList_listFavorites
| UserFavoritesPost_createFavorite
| UserGet_getUser
| UserFavoritesUpdate_updateFavorite
| UserList_getUserList;

View File

@ -7,11 +7,14 @@ import type {
BusterUserPermissionGroup,
BusterUserResponse,
BusterUserTeamListItem,
OrganizationUser
OrganizationUser,
BusterUserListItem
} from '@/api/asset_interfaces/users';
import type { UserRequestUserListPayload } from '@/api/request_interfaces/user/interfaces';
const favoritesGetList = queryOptions<BusterUserFavorite[]>({
queryKey: ['users', 'favorites', 'list'] as const
queryKey: ['users', 'favorites', 'list'] as const,
staleTime: 1000 * 60 * 60 // 1 hour
});
const userGetUserMyself = queryOptions<BusterUserResponse>({
@ -48,6 +51,11 @@ const userGetUserDatasetGroups = (userId: string) =>
queryKey: ['users', userId, 'datasetGroups'] as const
});
const userGetUserList = (params: UserRequestUserListPayload) =>
queryOptions<BusterUserListItem[]>({
queryKey: ['users', 'list', params] as const
});
export const userQueryKeys = {
favoritesGetList,
userGetUserMyself,
@ -56,5 +64,6 @@ export const userQueryKeys = {
userGetUserTeams,
userGetUserAttributes,
userGetUserDatasets,
userGetUserDatasetGroups
userGetUserDatasetGroups,
userGetUserList
};

View File

@ -1,46 +1,24 @@
import { useMemoizedFn } from '@/hooks';
import { useSocketQueryEmitOn, useSocketQueryMutation } from '@/api/buster_socket_query';
import { queryKeys } from '@/api/query_keys';
import type { BusterUserFavorite } from '@/api/asset_interfaces/users';
import isEmpty from 'lodash/isEmpty';
import {
useAddUserFavorite,
useDeleteUserFavorite,
useGetUserFavorites
} from '@/api/buster_rest/users';
const DEFAULT_FAVORITES: BusterUserFavorite[] = [];
export const useFavoriteProvider = () => {
const { data: userFavorites, refetch: refreshFavoritesList } = useSocketQueryEmitOn({
emitEvent: { route: '/users/favorites/list', payload: {} },
responseEvent: '/users/favorites/list:listFavorites',
options: queryKeys.favoritesGetList
});
const { data: userFavorites, refetch: refreshFavoritesList } = useGetUserFavorites();
const { mutate: addItemToFavorite } = useSocketQueryMutation({
emitEvent: '/users/favorites/post',
responseEvent: '/users/favorites/post:createFavorite',
options: queryKeys.favoritesGetList,
preCallback: (prev, mutationParams) => [mutationParams, ...(prev || [])]
});
const { mutate: addItemToFavorite } = useAddUserFavorite();
const { mutate: removeItemFromFavorite } = useSocketQueryMutation({
emitEvent: '/users/favorites/delete',
responseEvent: '/users/favorites/post:createFavorite',
options: queryKeys.favoritesGetList,
preCallback: (prev, mutationParams) => prev?.filter((f) => f.id !== mutationParams.id) || []
});
const { mutate: updateFavorites } = useSocketQueryMutation({
emitEvent: '/users/favorites/update',
responseEvent: '/users/favorites/update:updateFavorite',
options: queryKeys.favoritesGetList,
preCallback: (prev, mutationParams) => {
return mutationParams.favorites.map((id, index) => {
const favorite = (prev || []).find((f) => f.id === id || f.collection_id === id)!;
return { ...favorite, index };
});
}
});
const { mutate: removeItemFromFavorite } = useDeleteUserFavorite();
const bulkEditFavorites = useMemoizedFn(async (favorites: string[]) => {
return updateFavorites({ favorites });
// return updateFavorites({ favorites });
alert('TODO - feature not implemented yet');
});
return {

View File

@ -1,5 +1,4 @@
import { isDev } from '@/config';
import { UserResponses } from '@/api/buster_socket/user';
import type {
BusterSocketResponseBase,
BusterSocketResponseMessage
@ -26,7 +25,6 @@ export const createBusterResponse = (
const isKnownMessageRoute = (parsedMessage: BusterSocketResponseMessage) => {
const allResponses = {
...UserResponses,
...ChatsResponses
};
const event = parsedMessage?.event;