Skip to content

Commit 6805055

Browse files
committed
move fetch logic to useQuery internal key, extract manual exception cases
1 parent d1d8164 commit 6805055

File tree

5 files changed

+54
-31
lines changed

5 files changed

+54
-31
lines changed

src/Serilog.Ui.Web/src/app/components/Search/PagingLeftColumn.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
IconSortAscending,
77
IconSortDescending,
88
} from '@tabler/icons-react';
9-
import useQueryLogs from 'app/hooks/useQueryLogs';
109
import { useSearchForm } from 'app/hooks/useSearchForm';
1110
import { useSerilogUiProps } from 'app/hooks/useSerilogUiProps';
1211
import { memo, useEffect, useMemo } from 'react';
@@ -26,7 +25,6 @@ const sortOnOptions = Object.values(SortPropertyOptions).map((entry) => ({
2625
export const PagingLeftColumn = memo(
2726
({ changePage }: { changePage: (page: number) => void }) => {
2827
const { disabledSortOnKeys } = useSerilogUiProps();
29-
const { refetch } = useQueryLogs();
3028
const { control, watch } = useSearchForm();
3129
const currentDbKey = watch('table');
3230

@@ -58,10 +56,6 @@ export const PagingLeftColumn = memo(
5856
}
5957
}, [disableSortOn, fieldSortOn]);
6058

61-
useEffect(() => {
62-
void refetch();
63-
}, [refetch, fieldEntries.value, fieldSortOn.value, fieldSortBy.value]);
64-
6559
return (
6660
<Box
6761
aria-label="paging-left-column"

src/Serilog.Ui.Web/src/app/components/Search/Search.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import useQueryLogs from 'app/hooks/useQueryLogs';
1414
import { useQueryTableKeys } from 'app/hooks/useQueryTableKeys';
1515
import { useSearchForm } from 'app/hooks/useSearchForm';
1616
import { useSerilogUiProps } from 'app/hooks/useSerilogUiProps';
17-
import { memo, useEffect } from 'react';
17+
import { memo } from 'react';
1818
import { useController, useWatch } from 'react-hook-form';
1919
import classes from 'style/search.module.css';
2020
import { LogLevel } from '../../../types/types';
@@ -28,25 +28,26 @@ const Search = ({ onRefetch }: { onRefetch?: () => void }) => {
2828
const { isError } = useQueryTableKeys(true);
2929
const { isUtc, setIsUtc } = useSerilogUiProps();
3030
const { handleSubmit, reset, setValue } = useSearchForm();
31-
3231
const { refetch } = useQueryLogs();
33-
const currentDbKey = useWatch({ name: 'table' });
3432
const currentPage = useWatch({ name: 'page' });
3533

36-
const clean = async () => {
37-
reset();
38-
await refetch();
39-
};
34+
const onSubmit = async () => {
35+
if (currentPage === 1) {
36+
await refetch();
37+
} else {
38+
setValue('page', 1);
39+
}
4040

41-
const submit = async () => {
42-
setValue('page', 1);
43-
await refetch();
4441
onRefetch?.();
4542
};
4643

47-
useEffect(() => {
48-
void refetch();
49-
}, [currentDbKey, currentPage, refetch]);
44+
const onClear = async () => {
45+
const shouldRefetch = reset();
46+
47+
if (shouldRefetch) {
48+
await refetch();
49+
}
50+
};
5051

5152
return (
5253
<form aria-label="search-logs-form" onSubmit={() => {}}>
@@ -67,13 +68,13 @@ const Search = ({ onRefetch }: { onRefetch?: () => void }) => {
6768
setIsUtc(event.currentTarget.checked);
6869
}}
6970
/>
70-
<Button type="submit" onClick={handleSubmit(submit)} disabled={isError}>
71+
<Button type="submit" onClick={handleSubmit(onSubmit)} disabled={isError}>
7172
Submit
7273
</Button>
7374
<ActionIcon
7475
visibleFrom="lg"
7576
size={28}
76-
onClick={clean}
77+
onClick={onClear}
7778
variant="light"
7879
aria-label="reset filters"
7980
>

src/Serilog.Ui.Web/src/app/components/ShellStructure/FilterButton.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const FilterButton = () => {
1313
const [filterModalOpened, { open, close }] = useDisclosure(false);
1414
const { reset } = useSearchForm();
1515
const { refetch } = useQueryLogs();
16+
const onClear = async () => {
17+
const shouldRefetch = reset();
18+
19+
if (shouldRefetch) {
20+
await refetch();
21+
}
22+
};
1623

1724
useCloseOnResize(close);
1825

@@ -29,10 +36,7 @@ const FilterButton = () => {
2936
<Box className={classes.searchFiltersModalTitle}>
3037
<Text>Search filters</Text>
3138
<ActionIcon
32-
onClick={async () => {
33-
reset();
34-
await refetch();
35-
}}
39+
onClick={onClear}
3640
size={28}
3741
variant="light"
3842
aria-label="reset filters"

src/Serilog.Ui.Web/src/app/hooks/useQueryLogs.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@ import { keepPreviousData, useQuery } from '@tanstack/react-query';
22
import { fetchLogs } from '../queries/logs';
33
import { useAuthProperties } from './useAuthProperties';
44
import { useSearchForm } from './useSearchForm';
5+
import { useWatch } from 'react-hook-form';
56

67
const useQueryLogs = () => {
78
const { fetchInfo, isHeaderReady } = useAuthProperties();
8-
const { getValues, watch } = useSearchForm();
9-
const currentDbKey = watch('table');
9+
const { getValues } = useSearchForm();
1010

11+
const currentDbKey = useWatch({ name: 'table' })
12+
const entriesPerPage = useWatch({ name: 'entriesPerPage' })
13+
const page = useWatch({ name: 'page' })
14+
const sortBy = useWatch({ name: 'sortBy' })
15+
const sortOn = useWatch({ name: 'sortOn' })
16+
17+
console.log(['get-logs', entriesPerPage, page, sortBy, sortOn, currentDbKey])
1118
return useQuery({
12-
enabled: false,
13-
queryKey: ['get-logs'],
19+
enabled: true,
20+
queryKey: ['get-logs', entriesPerPage, page, sortBy, sortOn, currentDbKey],
1421
queryFn: async () => {
1522
if (!isHeaderReady) return null;
16-
23+
1724
return currentDbKey
1825
? await fetchLogs(getValues(), fetchInfo.headers, fetchInfo.routePrefix)
1926
: null;
2027
},
2128
placeholderData: keepPreviousData,
22-
refetchOnMount: true,
29+
refetchOnMount: false,
2330
refetchOnWindowFocus: false,
2431
retry: false,
2532
});

src/Serilog.Ui.Web/src/app/hooks/useSearchForm.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ export const searchFormInitialValues: SearchForm = {
1919
page: 1,
2020
};
2121

22+
// react-query run a refetch when any of these values change,
23+
// as they're part of its query hash-key.
24+
// If on a clear fields no value was changed, we run a manual refetch
25+
// otherwise it won't automatically run
26+
const runManualRefetch = (getValues: () => SearchForm, tableDefault: string) => {
27+
const { table, entriesPerPage, page, sortBy, sortOn } = getValues();
28+
29+
const propertiesToCheck = [table === tableDefault, entriesPerPage === searchFormInitialValues.entriesPerPage,
30+
page === searchFormInitialValues.page, sortBy === searchFormInitialValues.sortBy, sortOn === searchFormInitialValues.sortOn]
31+
32+
return propertiesToCheck.every(isEqualToDefault => isEqualToDefault)
33+
}
34+
2235
export const useSearchForm = () => {
2336
const methods = useForm<SearchForm>({
2437
defaultValues: searchFormInitialValues,
@@ -29,10 +42,14 @@ export const useSearchForm = () => {
2942
const tableKeysDefaultValue = isArrayGuard(data) ? data.at(0)! : '';
3043

3144
const resetForm = () => {
45+
const runRefetch = runManualRefetch(useSearchContext.getValues, tableKeysDefaultValue)
46+
3247
useSearchContext.reset({
3348
...searchFormInitialValues,
3449
table: tableKeysDefaultValue,
3550
});
51+
52+
return runRefetch
3653
};
3754

3855
return { methods, ...useSearchContext, reset: resetForm };

0 commit comments

Comments
 (0)