pl-fe: move status history to tanstack query
Signed-off-by: mkljczk <git@mkljczk.pl>
This commit is contained in:
25
packages/pl-fe/src/api/hooks/statuses/use-status-history.ts
Normal file
25
packages/pl-fe/src/api/hooks/statuses/use-status-history.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { StatusEdit } from 'pl-api';
|
||||
|
||||
import { importEntities } from 'pl-fe/actions/importer';
|
||||
import { useAppDispatch } from 'pl-fe/hooks/use-app-dispatch';
|
||||
import { useClient } from 'pl-fe/hooks/use-client';
|
||||
|
||||
const minifyStatusEdit = ({ account, ...statusEdit }: StatusEdit) => ({
|
||||
account_id: account.id,
|
||||
...statusEdit,
|
||||
});
|
||||
|
||||
const useStatusHistory = (statusId: string) => {
|
||||
const client = useClient();
|
||||
const dispatch = useAppDispatch();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['statuses', 'history', statusId],
|
||||
queryFn: () => client.statuses.getStatusHistory(statusId)
|
||||
.then(history => (dispatch(importEntities({ accounts: history.map(({ account }) => account) })), history))
|
||||
.then(history => history.map(minifyStatusEdit)),
|
||||
});
|
||||
};
|
||||
|
||||
export { useStatusHistory };
|
||||
Reference in New Issue
Block a user