ui: fix caching for autocomplete values

This commit is contained in:
nick-delirium 2025-02-28 17:39:21 +01:00
parent 1146900dc0
commit 11a2ea48bc
No known key found for this signature in database
GPG key ID: 93ABD695DF5FDBA0
2 changed files with 9 additions and 7 deletions

View file

@ -62,17 +62,18 @@ const FilterAutoComplete = observer(
const [loading, setLoading] = useState(false);
const { filterStore, projectsStore } = useStore();
const _params = processKey(params);
const filterKey = `${_params.type}${_params.key || ''}`;
const filterKey = `${projectsStore.siteId}_${_params.type}${_params.key || ''}`;
const topValues = filterStore.topValues[filterKey] || [];
React.useEffect(() => {
filterStore.resetValues()
setOptions([])
}, [projectsStore.siteId])
const loadTopValues = async () => {
setLoading(true)
await filterStore.fetchTopValues(_params.type, _params.key);
if (projectsStore.siteId) {
await filterStore.fetchTopValues(_params.type, projectsStore.siteId, _params.key);
}
setLoading(false)
};

View file

@ -27,12 +27,13 @@ export default class FilterStore {
this.topValues = {};
}
fetchTopValues = async (key: string, source?: string) => {
if (this.topValues.hasOwnProperty(key)) {
return Promise.resolve(this.topValues[key]);
fetchTopValues = async (key: string, siteId: string, source?: string) => {
const valKey = `${siteId}_${key}${source || ''}`
if (this.topValues[valKey] && this.topValues[valKey].length) {
return Promise.resolve(this.topValues[valKey]);
}
return filterService.fetchTopValues(key, source).then((response: []) => {
this.setTopValues(`${key}${source || ''}`, response);
this.setTopValues(valKey, response);
});
};
}