Skip to content

Commit

Permalink
fix: 修复查询记录没有更新
Browse files Browse the repository at this point in the history
  • Loading branch information
minosss committed Jun 22, 2023
1 parent e1a1596 commit 2352509
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/pages/detail/use-detail.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import type { QueryItem } from '../../api/types';
import type { Track } from '../../types';
import { useQuery } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useAtom } from 'jotai';
import { useEffect } from 'react';
import { showNotification } from '@mantine/notifications';
import { MessageKind } from '../../types';
import { fetcher } from '../../utils/fetcher';
import { now } from '../../utils/helper';
import { queryAtom } from './store';

export function useDetail() {
const [query, setQuery] = useAtom(queryAtom);
const queryClient = useQueryClient();

const { data, isLoading, error, refetch: refetchDetail } = useQuery({
const { data, isLoading, error, refetch: refetchDetail, isFetched } = useQuery({
queryKey: ['query', query],
queryFn: async () => fetcher<QueryItem>(MessageKind.Query, query),
enabled: query != null,
});

useEffect(() => {
if (isFetched) {
queryClient.invalidateQueries(['history']);
}
}, [isFetched, queryClient]);

function cleanQuery() {
setQuery(null);
}
Expand All @@ -39,7 +47,12 @@ export function useDetail() {
if (isTracking && query?.id) {
await fetcher(MessageKind.DeleteTrack, query.id);
await refetch();
} else if (data != null) {
} else if (data == null) {
showNotification({
color: 'red',
message: '数据错误,无法收藏',
});
} else {
await fetcher(MessageKind.PutTrack, {
...query,
state: data.state,
Expand Down

0 comments on commit 2352509

Please sign in to comment.