forked from mirrors/homebox
fix: conditionally filter parent locations (#133)
This commit is contained in:
parent
fbcbde836a
commit
8e1947d971
18 changed files with 135 additions and 67 deletions
|
@ -41,7 +41,7 @@
|
|||
const toast = useNotifier();
|
||||
|
||||
const locationsStore = useLocationStore();
|
||||
const locations = computed(() => locationsStore.locations);
|
||||
const locations = computed(() => locationsStore.allLocations);
|
||||
|
||||
const labelStore = useLabelStore();
|
||||
const labels = computed(() => labelStore.labels);
|
||||
|
|
|
@ -32,7 +32,8 @@
|
|||
const rmLocationStoreObserver = defineObserver("locationStore", {
|
||||
handler: r => {
|
||||
if (r.status === 201 || r.url.match(reLocation)) {
|
||||
locationStore.refresh();
|
||||
locationStore.refreshChildren();
|
||||
locationStore.refreshParents();
|
||||
}
|
||||
console.debug("locationStore handler called by observer");
|
||||
},
|
||||
|
@ -43,7 +44,8 @@
|
|||
EventTypes.ClearStores,
|
||||
() => {
|
||||
labelStore.refresh();
|
||||
locationStore.refresh();
|
||||
locationStore.refreshChildren();
|
||||
locationStore.refreshParents();
|
||||
},
|
||||
"stores"
|
||||
);
|
||||
|
|
|
@ -2,9 +2,13 @@ import { BaseAPI, route } from "../base";
|
|||
import { LocationOutCount, LocationCreate, LocationOut, LocationUpdate } from "../types/data-contracts";
|
||||
import { Results } from "../types/non-generated";
|
||||
|
||||
export type LocationsQuery = {
|
||||
filterChildren: boolean;
|
||||
};
|
||||
|
||||
export class LocationsApi extends BaseAPI {
|
||||
getAll() {
|
||||
return this.http.get<Results<LocationOutCount>>({ url: route("/locations") });
|
||||
getAll(q: LocationsQuery = { filterChildren: false }) {
|
||||
return this.http.get<Results<LocationOutCount>>({ url: route("/locations", q) });
|
||||
}
|
||||
|
||||
create(body: LocationCreate) {
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
const auth = useAuthStore();
|
||||
|
||||
const locationStore = useLocationStore();
|
||||
const locations = computed(() => locationStore.locations);
|
||||
const locations = computed(() => locationStore.parentLocations);
|
||||
|
||||
const labelsStore = useLabelStore();
|
||||
const labels = computed(() => labelsStore.labels);
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
const itemId = computed<string>(() => route.params.id as string);
|
||||
|
||||
const locationStore = useLocationStore();
|
||||
const locations = computed(() => locationStore.locations);
|
||||
const locations = computed(() => locationStore.allLocations);
|
||||
|
||||
const labelStore = useLabelStore();
|
||||
const labels = computed(() => labelStore.labels);
|
||||
|
|
|
@ -82,7 +82,7 @@
|
|||
});
|
||||
|
||||
const locationsStore = useLocationStore();
|
||||
const locations = computed(() => locationsStore.locations);
|
||||
const locations = computed(() => locationsStore.allLocations);
|
||||
|
||||
const labelStore = useLabelStore();
|
||||
const labels = computed(() => labelStore.labels);
|
||||
|
|
|
@ -117,7 +117,7 @@
|
|||
}
|
||||
|
||||
const locationStore = useLocationStore();
|
||||
const locations = computed(() => locationStore.locations);
|
||||
const locations = computed(() => locationStore.allLocations);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const parent = ref<LocationSummary | any>({});
|
||||
|
|
|
@ -3,7 +3,8 @@ import { LocationOutCount } from "~~/lib/api/types/data-contracts";
|
|||
|
||||
export const useLocationStore = defineStore("locations", {
|
||||
state: () => ({
|
||||
allLocations: null as LocationOutCount[] | null,
|
||||
parents: null as LocationOutCount[] | null,
|
||||
Locations: null as LocationOutCount[] | null,
|
||||
client: useUserApi(),
|
||||
}),
|
||||
getters: {
|
||||
|
@ -12,21 +13,36 @@ export const useLocationStore = defineStore("locations", {
|
|||
* synched with the server by intercepting the API calls and updating on the
|
||||
* response
|
||||
*/
|
||||
locations(state): LocationOutCount[] {
|
||||
if (state.allLocations === null) {
|
||||
Promise.resolve(this.refresh());
|
||||
parentLocations(state): LocationOutCount[] {
|
||||
if (state.parents === null) {
|
||||
Promise.resolve(this.refreshParents());
|
||||
}
|
||||
return state.allLocations;
|
||||
return state.parents;
|
||||
},
|
||||
allLocations(state): LocationOutCount[] {
|
||||
if (state.Locations === null) {
|
||||
Promise.resolve(this.refreshChildren());
|
||||
}
|
||||
return state.Locations;
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
async refresh(): Promise<LocationOutCount[]> {
|
||||
const result = await this.client.locations.getAll();
|
||||
async refreshParents(): Promise<LocationOutCount[]> {
|
||||
const result = await this.client.locations.getAll({ filterChildren: true });
|
||||
if (result.error) {
|
||||
return result;
|
||||
}
|
||||
|
||||
this.allLocations = result.data.items;
|
||||
this.parents = result.data.items;
|
||||
return result;
|
||||
},
|
||||
async refreshChildren(): Promise<LocationOutCount[]> {
|
||||
const result = await this.client.locations.getAll({ filterChildren: false });
|
||||
if (result.error) {
|
||||
return result;
|
||||
}
|
||||
|
||||
this.Locations = result.data.items;
|
||||
return result;
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue