Files
homebox/frontend/stores/locations.ts
Benjamin Wolff 2bdd085289 Item search query parameter modernisation (#1040)
* await labels and locations properly

* update query params with every search

* don't persist default settings in query params

* conceptualize optional parameters

* add run script for development

* lint

* consider typescript

* remove run.sh

* capitalize QueryParamValue

* make query parameter updates predictable

This reverts commit 5c0c48cea5.

* capitalize typename again

---------

Co-authored-by: Benji <benji@DG-SM-7059.local>
Co-authored-by: Benji <benji@mac.home.internal>
Co-authored-by: Benji <benji@dg-sm-7059.home.internal>
2025-10-21 19:40:46 +01:00

76 lines
2.2 KiB
TypeScript

import { defineStore } from "pinia";
import type { LocationsApi } from "~~/lib/api/classes/locations";
import type { LocationOutCount, TreeItem } from "~~/lib/api/types/data-contracts";
export const useLocationStore = defineStore("locations", {
state: () => ({
parents: null as LocationOutCount[] | null,
Locations: null as LocationOutCount[] | null,
client: useUserApi(),
tree: null as TreeItem[] | null,
refreshLocationsPromise: null as Promise<void> | null,
}),
getters: {
/**
* locations represents the locations that are currently in the store. The store is
* synched with the server by intercepting the API calls and updating on the
* response
*/
parentLocations(state): LocationOutCount[] {
if (state.parents === null) {
this.client.locations.getAll({ filterChildren: true }).then(result => {
if (result.error) {
console.error(result.error);
return;
}
this.parents = result.data;
});
}
return state.parents ?? [];
},
allLocations(state): LocationOutCount[] {
return state.Locations ?? [];
},
},
actions: {
async ensureLocationsFetched() {
if (this.Locations !== null) {
return;
}
if (this.refreshLocationsPromise === null) {
this.refreshLocationsPromise = this.refreshChildren().then(() => {});
}
await this.refreshLocationsPromise;
},
async refreshParents(): ReturnType<LocationsApi["getAll"]> {
const result = await this.client.locations.getAll({ filterChildren: true });
if (result.error) {
return result;
}
this.parents = result.data;
return result;
},
async refreshChildren(): ReturnType<LocationsApi["getAll"]> {
const result = await this.client.locations.getAll({ filterChildren: false });
if (result.error) {
return result;
}
this.Locations = result.data;
return result;
},
async refreshTree(): ReturnType<LocationsApi["getTree"]> {
const result = await this.client.locations.getTree();
if (result.error) {
return result;
}
this.tree = result.data;
return result;
},
},
});