mirror of https://github.com/buster-so/buster.git
callback is working!
This commit is contained in:
parent
533eb4fd9e
commit
7d46a35304
|
@ -23,6 +23,7 @@ import type {
|
||||||
InferBusterSocketRequestPayload,
|
InferBusterSocketRequestPayload,
|
||||||
InferBusterSocketResponseData
|
InferBusterSocketResponseData
|
||||||
} from './types';
|
} from './types';
|
||||||
|
import type { BusterChatListItem } from '@/api/asset_interfaces/chat';
|
||||||
|
|
||||||
export function useSocketMutation<
|
export function useSocketMutation<
|
||||||
TRequestRoute extends BusterSocketRequestRoute,
|
TRequestRoute extends BusterSocketRequestRoute,
|
||||||
|
@ -30,12 +31,15 @@ export function useSocketMutation<
|
||||||
TError = unknown,
|
TError = unknown,
|
||||||
TData = InferBusterSocketResponseData<TRoute>,
|
TData = InferBusterSocketResponseData<TRoute>,
|
||||||
TPayload = InferBusterSocketRequestPayload<TRequestRoute>,
|
TPayload = InferBusterSocketRequestPayload<TRequestRoute>,
|
||||||
TQueryKey extends QueryKey = QueryKey
|
TQueryData = unknown
|
||||||
>(
|
>(
|
||||||
socketRequest: BusterSocketRequestConfig<TRequestRoute>,
|
socketRequest: BusterSocketRequestConfig<TRequestRoute>,
|
||||||
socketResponse: BusterSocketResponseConfig<TRoute>,
|
socketResponse: BusterSocketResponseConfig<TRoute>,
|
||||||
options?: UseQueryOptions<TData, TError, TData, TQueryKey>,
|
options?: UseQueryOptions<TQueryData, any, TQueryData, any>,
|
||||||
callback?: (currentData: TData | null, newData: InferBusterSocketResponseData<TRoute>) => TData
|
callback?: (
|
||||||
|
currentData: TQueryData | null,
|
||||||
|
newData: InferBusterSocketResponseData<TRoute>
|
||||||
|
) => TQueryData
|
||||||
) {
|
) {
|
||||||
const busterSocket = useBusterWebSocket();
|
const busterSocket = useBusterWebSocket();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
@ -57,10 +61,10 @@ export function useSocketMutation<
|
||||||
if (options?.queryKey && callback) {
|
if (options?.queryKey && callback) {
|
||||||
const queryKey = options.queryKey;
|
const queryKey = options.queryKey;
|
||||||
const socketData = result as InferBusterSocketResponseData<TRoute>;
|
const socketData = result as InferBusterSocketResponseData<TRoute>;
|
||||||
const currentData = queryClient.getQueryData<TData>(queryKey) ?? null;
|
const currentData = queryClient.getQueryData<TQueryData>(queryKey) ?? null;
|
||||||
const transformedData: TData = callback(currentData, socketData);
|
const transformedData = callback(currentData, socketData);
|
||||||
queryClient.setQueryData<TData>(queryKey, transformedData);
|
queryClient.setQueryData(queryKey, transformedData);
|
||||||
return transformedData;
|
return result as TData;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result as TData;
|
return result as TData;
|
||||||
|
@ -76,13 +80,26 @@ export function useSocketMutation<
|
||||||
|
|
||||||
const ExampleComponent = () => {
|
const ExampleComponent = () => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const options = queryOptionsConfig['/chats/delete:deleteChat']('123');
|
const options = queryOptionsConfig['/chats/list:getChatsList']();
|
||||||
const data = queryClient.getQueryData(options.queryKey);
|
const data = queryClient.getQueryData(options.queryKey);
|
||||||
|
data?.[0].created_by_avatar;
|
||||||
|
|
||||||
const { mutate } = useSocketMutation<'/chats/delete', '/chats/delete:deleteChat'>(
|
const { mutate } = useSocketMutation<
|
||||||
|
'/chats/delete',
|
||||||
|
'/chats/delete:deleteChat',
|
||||||
|
unknown,
|
||||||
|
{ id: string }[],
|
||||||
|
{ id: string }[],
|
||||||
|
BusterChatListItem[]
|
||||||
|
>(
|
||||||
{ route: '/chats/delete' },
|
{ route: '/chats/delete' },
|
||||||
{ route: '/chats/delete:deleteChat' }
|
{ route: '/chats/delete:deleteChat' },
|
||||||
|
options,
|
||||||
|
(currentData, newData) => {
|
||||||
|
currentData?.[0].created_by_avatar; // This should now be properly typed
|
||||||
|
return currentData ?? [];
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
// mutate([{ id: '123' }]);
|
mutate([{ id: '123' }]);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue