mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +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 { notifications } from '@mantine/notifications';
|
||||
import { IconPrinter, IconReport, IconTags } from '@tabler/icons-react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
|
||||
import { api } from '../../App';
|
||||
@ -32,31 +33,28 @@ export function PrintingActions({
|
||||
|
||||
const [pluginKey, setPluginKey] = useState<string>('');
|
||||
|
||||
const loadFields = useCallback(() => {
|
||||
if (!enableLabels) {
|
||||
return;
|
||||
}
|
||||
|
||||
api
|
||||
.options(apiUrl(ApiEndpoints.label_print), {
|
||||
params: {
|
||||
plugin: pluginKey || undefined
|
||||
}
|
||||
})
|
||||
.then((response: any) => {
|
||||
setExtraFields(extractAvailableFields(response, 'POST') || {});
|
||||
})
|
||||
.catch(() => {});
|
||||
}, [enableLabels, pluginKey]);
|
||||
|
||||
useEffect(() => {
|
||||
loadFields();
|
||||
}, [loadFields, pluginKey]);
|
||||
|
||||
const [extraFields, setExtraFields] = useState<ApiFormFieldSet>({});
|
||||
// Fetch available printing fields via OPTIONS request
|
||||
const printingFields = useQuery({
|
||||
enabled: enableLabels,
|
||||
queryKey: ['printingFields', modelType, pluginKey],
|
||||
gcTime: 500,
|
||||
queryFn: () =>
|
||||
api
|
||||
.options(apiUrl(ApiEndpoints.label_print), {
|
||||
params: {
|
||||
plugin: pluginKey || undefined
|
||||
}
|
||||
})
|
||||
.then((response: any) => {
|
||||
return extractAvailableFields(response, 'POST') || {};
|
||||
})
|
||||
.catch(() => {
|
||||
return {};
|
||||
})
|
||||
});
|
||||
|
||||
const labelFields: ApiFormFieldSet = useMemo(() => {
|
||||
let fields: ApiFormFieldSet = extraFields;
|
||||
let fields: ApiFormFieldSet = printingFields.data || {};
|
||||
|
||||
// Override field values
|
||||
fields['template'] = {
|
||||
@ -88,7 +86,7 @@ export function PrintingActions({
|
||||
};
|
||||
|
||||
return fields;
|
||||
}, [extraFields, items, loadFields]);
|
||||
}, [printingFields.data, items]);
|
||||
|
||||
const labelModal = useCreateApiFormModal({
|
||||
url: apiUrl(ApiEndpoints.label_print),
|
||||
|
@ -94,6 +94,12 @@ export function LanguageContext({ children }: { children: JSX.Element }) {
|
||||
locales.push('en-us');
|
||||
}
|
||||
|
||||
let new_locales = locales.join(', ');
|
||||
|
||||
if (new_locales == api.defaults.headers.common['Accept-Language']) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Update default Accept-Language headers
|
||||
api.defaults.headers.common['Accept-Language'] = locales.join(', ');
|
||||
|
||||
|
@ -22,6 +22,7 @@ type UseFilterProps = {
|
||||
export function useFilters(props: UseFilterProps) {
|
||||
const query = useQuery({
|
||||
enabled: true,
|
||||
gcTime: 500,
|
||||
queryKey: [props.url, props.method, props.params],
|
||||
queryFn: async () => {
|
||||
return await api
|
||||
|
@ -42,13 +42,15 @@ export function useGenerator(
|
||||
...params
|
||||
}));
|
||||
}
|
||||
|
||||
queryGenerator.refetch();
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
// API query handler
|
||||
const queryGenerator = useQuery({
|
||||
enabled: true,
|
||||
enabled: false,
|
||||
queryKey: ['generator', key, endpoint, debouncedQuery],
|
||||
queryFn: async () => {
|
||||
return api.post(apiUrl(endpoint), debouncedQuery).then((response) => {
|
||||
|
@ -11,7 +11,7 @@ export default function Logged_In() {
|
||||
|
||||
useEffect(() => {
|
||||
checkLoginState(navigate, location?.state?.redirectFrom);
|
||||
}, []);
|
||||
}, [navigate]);
|
||||
|
||||
return (
|
||||
<>
|
||||
|
@ -135,7 +135,6 @@ export function fetchGlobalStates() {
|
||||
setApiDefaults();
|
||||
|
||||
useServerApiState.getState().fetchServerApiState();
|
||||
useUserState.getState().fetchUserState();
|
||||
useUserSettingsState.getState().fetchSettings();
|
||||
useGlobalSettingsState.getState().fetchSettings();
|
||||
useGlobalStatusState.getState().fetchStatus();
|
||||
|
Loading…
x
Reference in New Issue
Block a user