2022-09-13 03:36:22 +00:00
|
|
|
import { defineStore } from "pinia";
|
2023-02-18 06:41:01 +00:00
|
|
|
import { LocationsApi } from "~~/lib/api/classes/locations";
|
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-11-02 19:54:43 +00:00
|
|
|
parents: null as LocationOutCount[] | null,
|
|
|
|
Locations: 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-11-02 19:54:43 +00:00
|
|
|
parentLocations(state): LocationOutCount[] {
|
|
|
|
if (state.parents === null) {
|
2023-02-18 06:41:01 +00:00
|
|
|
this.client.locations.getAll({ filterChildren: true }).then(result => {
|
|
|
|
if (result.error) {
|
|
|
|
console.error(result.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:48:22 +00:00
|
|
|
this.parents = result.data;
|
2023-02-18 06:41:01 +00:00
|
|
|
});
|
2022-09-13 03:36:22 +00:00
|
|
|
}
|
2023-02-18 06:41:01 +00:00
|
|
|
return state.parents ?? [];
|
2022-11-02 19:54:43 +00:00
|
|
|
},
|
|
|
|
allLocations(state): LocationOutCount[] {
|
|
|
|
if (state.Locations === null) {
|
2023-02-18 06:41:01 +00:00
|
|
|
this.client.locations.getAll({ filterChildren: false }).then(result => {
|
|
|
|
if (result.error) {
|
|
|
|
console.error(result.error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:48:22 +00:00
|
|
|
this.Locations = result.data;
|
2023-02-18 06:41:01 +00:00
|
|
|
});
|
2022-11-02 19:54:43 +00:00
|
|
|
}
|
2023-02-18 06:41:01 +00:00
|
|
|
return state.Locations ?? [];
|
2022-09-13 03:36:22 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
actions: {
|
2023-02-18 06:41:01 +00:00
|
|
|
async refreshParents(): ReturnType<LocationsApi["getAll"]> {
|
2022-11-02 19:54:43 +00:00
|
|
|
const result = await this.client.locations.getAll({ filterChildren: true });
|
|
|
|
if (result.error) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:48:22 +00:00
|
|
|
this.parents = result.data;
|
2022-11-02 19:54:43 +00:00
|
|
|
return result;
|
|
|
|
},
|
2023-02-18 06:41:01 +00:00
|
|
|
async refreshChildren(): ReturnType<LocationsApi["getAll"]> {
|
2022-11-02 19:54:43 +00:00
|
|
|
const result = await this.client.locations.getAll({ filterChildren: false });
|
2022-09-13 03:36:22 +00:00
|
|
|
if (result.error) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-21 05:48:22 +00:00
|
|
|
this.Locations = result.data;
|
2022-09-13 03:36:22 +00:00
|
|
|
return result;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|