homebox/frontend/stores/locations.ts
Hayden bd321af29f
chore: developer cleanup (#300)
* new PR tasks

* add homebox to know words

* formatting

* bump deps

* generate db models

* ts errors

* drop id

* fix accessor

* drop unused time field

* change CI

* add expected error

* add type check

* resolve serveral type errors

* hoise in CI
2023-02-17 21:41:01 -09:00

64 lines
1.9 KiB
TypeScript

import { defineStore } from "pinia";
import { LocationsApi } from "~~/lib/api/classes/locations";
import { LocationOutCount } from "~~/lib/api/types/data-contracts";
export const useLocationStore = defineStore("locations", {
state: () => ({
parents: null as LocationOutCount[] | null,
Locations: null as LocationOutCount[] | null,
client: useUserApi(),
}),
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.items;
});
}
return state.parents ?? [];
},
allLocations(state): LocationOutCount[] {
if (state.Locations === null) {
this.client.locations.getAll({ filterChildren: false }).then(result => {
if (result.error) {
console.error(result.error);
return;
}
this.Locations = result.data.items;
});
}
return state.Locations ?? [];
},
},
actions: {
async refreshParents(): ReturnType<LocationsApi["getAll"]> {
const result = await this.client.locations.getAll({ filterChildren: true });
if (result.error) {
return result;
}
this.parents = result.data.items;
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.items;
return result;
},
},
});