Skip to content

Commit

Permalink
move invalidate into refresh component
Browse files Browse the repository at this point in the history
  • Loading branch information
hardingjam committed Feb 19, 2025
1 parent d306015 commit 960590d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
26 changes: 1 addition & 25 deletions packages/ui-components/src/__tests__/TanstackAppTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,4 @@ test('shows refresh icon', async () => {
resolve();

await waitFor(() => expect(screen.getByTestId('refreshButton')).toBeInTheDocument());
});

test('refetches data when refresh button is clicked', async () => {
const mockRefetch = vi.fn();
const mockQuery = writable({
status: 'success',
fetchStatus: 'idle',
refetch: mockRefetch
});

render(TanstackAppTableTest, {
query: mockQuery as unknown as CreateInfiniteQueryResult<
InfiniteData<unknown[], unknown>,
Error
>,
emptyMessage: 'No rows',
title: 'Test Table',
head: 'Test head'
});

const refreshButton = screen.getByTestId('refreshButton');
await userEvent.click(refreshButton);

expect(mockRefetch).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
export let query: CreateInfiniteQueryResult<InfiniteData<T[], unknown>, Error>;
export let emptyMessage: string = 'None found';
export let rowHoverable = true;
export let queryKey: string;
</script>

<div data-testid="title" class="flex h-16 w-full items-center justify-end">
Expand All @@ -21,9 +22,7 @@
class="ml-2 h-8 w-5 cursor-pointer text-gray-400 dark:text-gray-400"
data-testid="refreshButton"
spin={$query.isLoading || $query.isFetching}
on:click={() => {
$query.refetch();
}}
{queryKey}
/>
</div>
{#if $query.data?.pages[0].length === 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
Tooltip
} from 'flowbite-svelte';
import { BugOutline, PauseSolid, PlaySolid } from 'flowbite-svelte-icons';
import { useQueryClient } from '@tanstack/svelte-query';
const queryClient = useQueryClient();
export let id: string;
export let order: OrderSubgraph;
Expand All @@ -37,7 +39,11 @@
let enabled = true;
const refreshQuotes = () => {
$orderQuoteQuery.refetch();
queryClient.invalidateQueries({
queryKey: [QKEY_ORDER_QUOTE + id, id],
refetchType: 'all',
exact: false
});
};
$: orderQuoteQuery = createQuery<BatchOrderQuotesResponse[]>({
Expand Down Expand Up @@ -70,8 +76,8 @@
{/if}
<span></span>
<Refresh
queryKey={id}
class="h-8 w-5 cursor-pointer text-gray-400 dark:text-gray-400"
on:click={refreshQuotes}
spin={$orderQuoteQuery.isLoading || $orderQuoteQuery.isFetching}
/>
<PauseSolid
Expand Down
12 changes: 12 additions & 0 deletions packages/ui-components/src/lib/components/icon/Refresh.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
import { getContext } from 'svelte';
import type { AriaRole } from 'svelte/elements';
import { twMerge } from 'tailwind-merge';
import { useQueryClient } from '@tanstack/svelte-query';
export let queryKey: string;
const queryClient = useQueryClient();
const invalidateQueries = () => {
queryClient.invalidateQueries({
queryKey: [queryKey],
refetchType: 'all',
exact: false
});
};
const ctx: { size: 'xs' | 'sm' | 'md' | 'lg' | 'xl'; role: AriaRole | undefined } =
getContext('iconCtx') ?? {};
Expand All @@ -19,6 +30,7 @@
</script>

<svg
on:click={invalidateQueries}
data-testid="refresh-button"
xmlns="http://www.w3.org/2000/svg"
fill="none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@
});
</script>

<TanstackAppTable query={orderTradesQuery} emptyMessage="No trades found" rowHoverable={false}>
<TanstackAppTable
query={orderTradesQuery}
emptyMessage="No trades found"
rowHoverable={false}
queryKey={id}
>
<svelte:fragment slot="info">
{#if tradesCount !== undefined}
<div class="px-2">{tradesCount} Trades</div>
Expand Down

0 comments on commit 960590d

Please sign in to comment.