mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-03 05:48:47 +00:00
[PUI] Query cache improvements (#7609)
* Remove duplicate call to fetchUserState * Optimize printing fields request - Implement using useQuery hook - Add 500ms cache * Prevent duplicate locale setting * Add cache time to useFilters hook * Prevent useGenerator from fetching initially
This commit is contained in:
parent
2d8f5e9006
commit
7655113851
@ -1,6 +1,7 @@
|
|||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import { notifications } from '@mantine/notifications';
|
import { notifications } from '@mantine/notifications';
|
||||||
import { IconPrinter, IconReport, IconTags } from '@tabler/icons-react';
|
import { IconPrinter, IconReport, IconTags } from '@tabler/icons-react';
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
import { api } from '../../App';
|
import { api } from '../../App';
|
||||||
@ -32,31 +33,28 @@ export function PrintingActions({
|
|||||||
|
|
||||||
const [pluginKey, setPluginKey] = useState<string>('');
|
const [pluginKey, setPluginKey] = useState<string>('');
|
||||||
|
|
||||||
const loadFields = useCallback(() => {
|
// Fetch available printing fields via OPTIONS request
|
||||||
if (!enableLabels) {
|
const printingFields = useQuery({
|
||||||
return;
|
enabled: enableLabels,
|
||||||
}
|
queryKey: ['printingFields', modelType, pluginKey],
|
||||||
|
gcTime: 500,
|
||||||
api
|
queryFn: () =>
|
||||||
.options(apiUrl(ApiEndpoints.label_print), {
|
api
|
||||||
params: {
|
.options(apiUrl(ApiEndpoints.label_print), {
|
||||||
plugin: pluginKey || undefined
|
params: {
|
||||||
}
|
plugin: pluginKey || undefined
|
||||||
})
|
}
|
||||||
.then((response: any) => {
|
})
|
||||||
setExtraFields(extractAvailableFields(response, 'POST') || {});
|
.then((response: any) => {
|
||||||
})
|
return extractAvailableFields(response, 'POST') || {};
|
||||||
.catch(() => {});
|
})
|
||||||
}, [enableLabels, pluginKey]);
|
.catch(() => {
|
||||||
|
return {};
|
||||||
useEffect(() => {
|
})
|
||||||
loadFields();
|
});
|
||||||
}, [loadFields, pluginKey]);
|
|
||||||
|
|
||||||
const [extraFields, setExtraFields] = useState<ApiFormFieldSet>({});
|
|
||||||
|
|
||||||
const labelFields: ApiFormFieldSet = useMemo(() => {
|
const labelFields: ApiFormFieldSet = useMemo(() => {
|
||||||
let fields: ApiFormFieldSet = extraFields;
|
let fields: ApiFormFieldSet = printingFields.data || {};
|
||||||
|
|
||||||
// Override field values
|
// Override field values
|
||||||
fields['template'] = {
|
fields['template'] = {
|
||||||
@ -88,7 +86,7 @@ export function PrintingActions({
|
|||||||
};
|
};
|
||||||
|
|
||||||
return fields;
|
return fields;
|
||||||
}, [extraFields, items, loadFields]);
|
}, [printingFields.data, items]);
|
||||||
|
|
||||||
const labelModal = useCreateApiFormModal({
|
const labelModal = useCreateApiFormModal({
|
||||||
url: apiUrl(ApiEndpoints.label_print),
|
url: apiUrl(ApiEndpoints.label_print),
|
||||||
|
@ -94,6 +94,12 @@ export function LanguageContext({ children }: { children: JSX.Element }) {
|
|||||||
locales.push('en-us');
|
locales.push('en-us');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let new_locales = locales.join(', ');
|
||||||
|
|
||||||
|
if (new_locales == api.defaults.headers.common['Accept-Language']) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Update default Accept-Language headers
|
// Update default Accept-Language headers
|
||||||
api.defaults.headers.common['Accept-Language'] = locales.join(', ');
|
api.defaults.headers.common['Accept-Language'] = locales.join(', ');
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ type UseFilterProps = {
|
|||||||
export function useFilters(props: UseFilterProps) {
|
export function useFilters(props: UseFilterProps) {
|
||||||
const query = useQuery({
|
const query = useQuery({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
gcTime: 500,
|
||||||
queryKey: [props.url, props.method, props.params],
|
queryKey: [props.url, props.method, props.params],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
return await api
|
return await api
|
||||||
|
@ -42,13 +42,15 @@ export function useGenerator(
|
|||||||
...params
|
...params
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
queryGenerator.refetch();
|
||||||
},
|
},
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
// API query handler
|
// API query handler
|
||||||
const queryGenerator = useQuery({
|
const queryGenerator = useQuery({
|
||||||
enabled: true,
|
enabled: false,
|
||||||
queryKey: ['generator', key, endpoint, debouncedQuery],
|
queryKey: ['generator', key, endpoint, debouncedQuery],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
return api.post(apiUrl(endpoint), debouncedQuery).then((response) => {
|
return api.post(apiUrl(endpoint), debouncedQuery).then((response) => {
|
||||||
|
@ -11,7 +11,7 @@ export default function Logged_In() {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
checkLoginState(navigate, location?.state?.redirectFrom);
|
checkLoginState(navigate, location?.state?.redirectFrom);
|
||||||
}, []);
|
}, [navigate]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -135,7 +135,6 @@ export function fetchGlobalStates() {
|
|||||||
setApiDefaults();
|
setApiDefaults();
|
||||||
|
|
||||||
useServerApiState.getState().fetchServerApiState();
|
useServerApiState.getState().fetchServerApiState();
|
||||||
useUserState.getState().fetchUserState();
|
|
||||||
useUserSettingsState.getState().fetchSettings();
|
useUserSettingsState.getState().fetchSettings();
|
||||||
useGlobalSettingsState.getState().fetchSettings();
|
useGlobalSettingsState.getState().fetchSettings();
|
||||||
useGlobalStatusState.getState().fetchStatus();
|
useGlobalStatusState.getState().fetchStatus();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user