felicity-lims/webapp/stores/storage.ts

200 lines
6.5 KiB
TypeScript
Raw Normal View History

2023-02-18 20:59:45 +08:00
import { defineStore } from "pinia";
2023-02-18 20:59:45 +08:00
import { useApiUtil, useTreeStateComposable } from "../composables";
import {
IStorageContainer,
IStorageLocation,
IStorageSection,
IStoreRoom,
} from "../models/storage";
import {
GET_STORAGE_TREE,
GET_ALL_STORAGE_CONTAINERS,
GET_ALL_STORAGE_LOCATIONS,
GET_ALL_STORAGE_SECTIONS,
GET_ALL_STORE_ROOMS,
GET_STORAGE_CONTAINER_BY_UID,
} from "../graphql/storage.queries";
import { GET_SAMPLES_BY_STORAGE_CONTAINER_UID } from "../graphql/analyses.queries";
2023-02-18 20:59:45 +08:00
const { withClientQuery } = useApiUtil();
const { setTree } = useTreeStateComposable();
export const useStorageStore = defineStore("storage", {
state: () => {
2023-02-18 20:59:45 +08:00
return {
tree: [],
fetchingTree: false,
storeRooms: [],
fetchingStoreRooms: false,
storageLocations: [],
fetchingStorageLocations: false,
storageSections: [],
fetchingStorageSections: false,
storageContainers: [],
fetchingStorageContainers: false,
storageContainer: undefined,
fetchingStorageContainer: false,
fetchingStorageContainerSamples: false,
} as {
tree: IStoreRoom[];
fetchingTree: boolean;
storeRooms: IStoreRoom[];
fetchingStoreRooms: boolean;
storageLocations: IStorageLocation[];
fetchingStorageLocations: boolean;
storageSections: IStorageSection[];
fetchingStorageSections: boolean;
storageContainers: IStorageContainer[];
fetchingStorageContainers: boolean;
storageContainer?: IStorageContainer;
fetchingStorageContainer: boolean;
fetchingStorageContainerSamples: boolean;
};
},
2023-02-18 20:59:45 +08:00
getters: {
getStorageTree: (state) => state.tree,
getStoreRooms: (state) => state.storeRooms,
getStorageLocations: (state) => state.storageLocations,
getStorageSection: (state) => state.storageSections,
getStorageContainers: (state) => state.storageContainers,
getStorageContainer: (state) => state.storageContainer,
2023-02-18 20:59:45 +08:00
},
actions: {
// Tree
2023-02-18 20:59:45 +08:00
async fetchStorageTree() {
this.fetchingTree = true;
await withClientQuery(GET_STORAGE_TREE, {}, "storeRoomAll")
2023-02-18 20:59:45 +08:00
.then((tree: IStoreRoom[]) => {
this.fetchingTree = false;
this.tree = tree;
setTree(tree);
})
.catch((err) => (this.fetchingTree = false));
},
// storeRooms
2023-02-18 20:59:45 +08:00
async fetchStoreRooms() {
this.fetchingStoreRooms = true;
await withClientQuery(GET_ALL_STORE_ROOMS, {}, "storeRoomAll")
2023-02-18 20:59:45 +08:00
.then((storeRooms: IStoreRoom[]) => {
this.fetchingStoreRooms = false;
this.storeRooms = storeRooms;
})
.catch((err) => (this.fetchingStoreRooms = false));
},
addStoreRoom(payload): void {
this.storeRooms?.unshift(payload);
},
updateStoreRoom(payload: IStoreRoom): void {
2023-02-18 20:59:45 +08:00
const index = this.storeRooms?.findIndex((item) => item.uid === payload?.uid);
if (index > -1) this.storeRooms[index] = payload;
},
2023-02-18 20:59:45 +08:00
// storageLocations
async fetchStorageLocations(storeRoomUid: number) {
this.fetchingStorageLocations = true;
2023-02-18 20:59:45 +08:00
await withClientQuery(
GET_ALL_STORAGE_LOCATIONS,
{ storeRoomUid },
"storageLocationAll"
)
.then((storageLocations: IStorageLocation[]) => {
this.fetchingStorageLocations = false;
this.storageLocations = storageLocations;
})
.catch((err) => (this.fetchingStorageLocations = false));
},
addStorageLocation(payload): void {
this.storageLocations?.unshift(payload);
},
updateStorageLocation(payload: IStorageLocation): void {
2023-02-18 20:59:45 +08:00
const index = this.storageLocations?.findIndex((item) => item.uid === payload?.uid);
if (index > -1) this.storageLocations[index] = payload;
},
2023-02-18 20:59:45 +08:00
// storageSection
async fetchStorageSections(storageSectionUid: number) {
this.fetchingStorageSections = true;
2023-02-18 20:59:45 +08:00
await withClientQuery(
GET_ALL_STORAGE_SECTIONS,
{ storageSectionUid },
"storageSectionAll"
)
.then((storageSections: IStorageSection[]) => {
this.fetchingStorageSections = false;
this.storageSections = storageSections;
})
.catch((err) => (this.fetchingStorageSections = false));
},
addStorageSection(payload): void {
this.storageSections?.unshift(payload);
},
updateStorageSection(payload: IStorageSection): void {
2023-02-18 20:59:45 +08:00
const index = this.storageSections?.findIndex((item) => item.uid === payload?.uid);
if (index > -1) this.storageSections[index] = payload;
},
// storageContainers
2023-02-18 20:59:45 +08:00
async fetchStorageContainers(storageContainerUid: number) {
this.fetchingStorageContainers = true;
2023-02-18 20:59:45 +08:00
await withClientQuery(
GET_ALL_STORAGE_CONTAINERS,
{ storageContainerUid },
"storageContainerAll"
)
.then((storageContainers: IStorageContainer[]) => {
this.fetchingStorageContainers = false;
this.storageContainers = storageContainers;
})
.catch((err) => (this.fetchingStorageContainers = false));
},
addStorageContainer(payload): void {
this.storageContainers?.unshift(payload);
},
updateStorageContainer(payload: IStorageContainer): void {
2023-02-18 20:59:45 +08:00
const index = this.storageContainers?.findIndex(
(item) => item.uid === payload?.uid
);
if (index > -1) this.storageContainers[index] = payload;
},
2023-02-18 20:59:45 +08:00
async fetchStorageContainer(uid: number) {
if (!uid) return;
this.fetchingStorageContainer = true;
2023-02-18 20:59:45 +08:00
await withClientQuery(
GET_STORAGE_CONTAINER_BY_UID,
{ uid },
"storageContainerByUid",
"network-only"
)
.then(async (payload) => {
this.fetchingStorageContainer = false;
this.storageContainer = payload;
await this.fetchStorageContainerSamples(uid);
})
.catch((err) => (this.fetchingStorageContainer = false));
},
resetStorageContainer(): void {
this.storageContainer = undefined;
},
2023-02-18 20:59:45 +08:00
async fetchStorageContainerSamples(uid: number) {
if (!uid) return;
2023-01-15 14:04:29 +08:00
this.fetchingStorageContainerSamples = true;
2023-02-18 20:59:45 +08:00
await withClientQuery(
GET_SAMPLES_BY_STORAGE_CONTAINER_UID,
{ uid },
"samplesByStorageContainerUid",
"network-only"
)
.then((payload) => {
this.fetchingStorageContainerSamples = false;
this.storageContainer = { ...this.storageContainer, samples: payload };
})
.catch((err) => (this.fetchingStorageContainerSamples = false));
},
2023-02-18 20:59:45 +08:00
},
});