2023-02-18 20:59:45 +08:00
|
|
|
import { defineStore } from "pinia";
|
2023-01-02 00:04:52 +08:00
|
|
|
|
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-01-02 00:04:52 +08:00
|
|
|
|
2023-02-18 20:59:45 +08:00
|
|
|
const { withClientQuery } = useApiUtil();
|
|
|
|
const { setTree } = useTreeStateComposable();
|
|
|
|
|
|
|
|
export const useStorageStore = defineStore("storage", {
|
2023-01-02 00:04:52 +08:00
|
|
|
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-01-02 00:04:52 +08:00
|
|
|
},
|
2023-02-18 20:59:45 +08:00
|
|
|
getters: {
|
2023-01-02 20:33:50 +08:00
|
|
|
getStorageTree: (state) => state.tree,
|
2023-01-02 00:04:52 +08:00
|
|
|
getStoreRooms: (state) => state.storeRooms,
|
|
|
|
getStorageLocations: (state) => state.storageLocations,
|
|
|
|
getStorageSection: (state) => state.storageSections,
|
|
|
|
getStorageContainers: (state) => state.storageContainers,
|
2023-01-03 02:12:00 +08:00
|
|
|
getStorageContainer: (state) => state.storageContainer,
|
2023-02-18 20:59:45 +08:00
|
|
|
},
|
2023-01-02 00:04:52 +08:00
|
|
|
actions: {
|
2023-01-02 20:33:50 +08:00
|
|
|
// Tree
|
2023-02-18 20:59:45 +08:00
|
|
|
async fetchStorageTree() {
|
2023-01-02 20:33:50 +08:00
|
|
|
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));
|
2023-01-02 20:33:50 +08:00
|
|
|
},
|
|
|
|
|
2023-01-02 00:04:52 +08:00
|
|
|
// storeRooms
|
2023-02-18 20:59:45 +08:00
|
|
|
async fetchStoreRooms() {
|
2023-01-02 00:04:52 +08:00
|
|
|
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));
|
2023-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
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-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
|
2023-02-18 20:59:45 +08:00
|
|
|
// storageLocations
|
|
|
|
async fetchStorageLocations(storeRoomUid: number) {
|
2023-01-02 00:04:52 +08:00
|
|
|
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));
|
2023-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
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-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
|
2023-02-18 20:59:45 +08:00
|
|
|
// storageSection
|
|
|
|
async fetchStorageSections(storageSectionUid: number) {
|
2023-01-02 00:04:52 +08:00
|
|
|
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));
|
2023-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
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;
|
2023-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// storageContainers
|
2023-02-18 20:59:45 +08:00
|
|
|
async fetchStorageContainers(storageContainerUid: number) {
|
2023-01-02 00:04:52 +08:00
|
|
|
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));
|
2023-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
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-01-02 00:04:52 +08:00
|
|
|
},
|
|
|
|
|
2023-02-18 20:59:45 +08:00
|
|
|
async fetchStorageContainer(uid: number) {
|
|
|
|
if (!uid) return;
|
2023-01-03 02:12:00 +08:00
|
|
|
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));
|
2023-01-03 02:12:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
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-01-02 00:04:52 +08:00
|
|
|
},
|
2023-02-18 20:59:45 +08:00
|
|
|
},
|
|
|
|
});
|