2022-09-13 03:36:22 +00:00
|
|
|
import { defineStore } from "pinia";
|
2022-09-27 23:52:13 +00:00
|
|
|
import { LocationOutCount } from "~~/lib/api/types/data-contracts";
|
2022-09-13 03:36:22 +00:00
|
|
|
|
|
|
|
export const useLocationStore = defineStore("locations", {
|
|
|
|
state: () => ({
|
2022-09-27 23:52:13 +00:00
|
|
|
allLocations: null as LocationOutCount[] | null,
|
2022-09-13 03:36:22 +00:00
|
|
|
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
|
|
|
|
*/
|
2022-09-27 23:52:13 +00:00
|
|
|
locations(state): LocationOutCount[] {
|
2022-09-13 03:36:22 +00:00
|
|
|
if (state.allLocations === null) {
|
|
|
|
Promise.resolve(this.refresh());
|
|
|
|
}
|
|
|
|
return state.allLocations;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
2022-09-27 23:52:13 +00:00
|
|
|
async refresh(): Promise<LocationOutCount[]> {
|
2022-09-13 03:36:22 +00:00
|
|
|
const result = await this.client.locations.getAll();
|
|
|
|
if (result.error) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.allLocations = result.data.items;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|