protocol base views
This commit is contained in:
parent
f453bdc7d3
commit
c1e9784b4a
14 changed files with 708 additions and 24 deletions
80
src/stores/admin/protocol.ts
Normal file
80
src/stores/admin/protocol.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import { defineStore } from "pinia";
|
||||
import type { CreateProtocolViewModel, UpdateProtocolViewModel } from "@/viewmodels/admin/protocol.models";
|
||||
import { http } from "@/serverCom";
|
||||
import type { AxiosResponse } from "axios";
|
||||
import type { ProtocolViewModel } from "@/viewmodels/admin/protocol.models";
|
||||
|
||||
export const useProtocolStore = defineStore("protocol", {
|
||||
state: () => {
|
||||
return {
|
||||
protocols: [] as Array<ProtocolViewModel & { tab_pos: number }>,
|
||||
totalCount: 0 as number,
|
||||
loading: "loading" as "loading" | "fetched" | "failed",
|
||||
activeProtocol: null as number | null,
|
||||
activeProtocolObj: null as ProtocolViewModel | null,
|
||||
loadingActive: "loading" as "loading" | "fetched" | "failed",
|
||||
};
|
||||
},
|
||||
actions: {
|
||||
fetchProtocols(offset = 0, count = 25, clear = false) {
|
||||
if (clear) this.protocols = [];
|
||||
this.loading = "loading";
|
||||
http
|
||||
.get(`/admin/protocol?offset=${offset}&count=${count}`)
|
||||
.then((result) => {
|
||||
this.totalCount = result.data.total;
|
||||
result.data.protocols
|
||||
.filter((elem: ProtocolViewModel) => this.protocols.findIndex((m) => m.id == elem.id) == -1)
|
||||
.map((elem: ProtocolViewModel, index: number): ProtocolViewModel & { tab_pos: number } => {
|
||||
return {
|
||||
...elem,
|
||||
tab_pos: index + offset,
|
||||
};
|
||||
})
|
||||
.forEach((elem: ProtocolViewModel & { tab_pos: number }) => {
|
||||
this.protocols.push(elem);
|
||||
});
|
||||
this.loading = "fetched";
|
||||
})
|
||||
.catch((err) => {
|
||||
this.loading = "failed";
|
||||
});
|
||||
},
|
||||
fetchProtocolByActiveId() {
|
||||
this.loadingActive = "loading";
|
||||
http
|
||||
.get(`/admin/protocol/${this.activeProtocol}`)
|
||||
.then((res) => {
|
||||
this.activeProtocolObj = res.data;
|
||||
this.loadingActive = "fetched";
|
||||
})
|
||||
.catch((err) => {
|
||||
this.loadingActive = "failed";
|
||||
});
|
||||
},
|
||||
fetchProtocolById(id: number) {
|
||||
return http.get(`/admin/protocol/${id}`);
|
||||
},
|
||||
async createProtocol(protocol: CreateProtocolViewModel): Promise<AxiosResponse<any, any>> {
|
||||
const result = await http.post(`/admin/protocol`, {
|
||||
title: protocol.title,
|
||||
date: protocol.date,
|
||||
});
|
||||
this.fetchProtocols();
|
||||
return result;
|
||||
},
|
||||
async updateActiveProtocol(protocol: UpdateProtocolViewModel): Promise<AxiosResponse<any, any>> {
|
||||
const result = await http.patch(`/admin/protocol/${protocol.id}`, {
|
||||
title: protocol.title,
|
||||
date: protocol.date,
|
||||
});
|
||||
this.fetchProtocols();
|
||||
return result;
|
||||
},
|
||||
async deleteProtocol(protocol: number): Promise<AxiosResponse<any, any>> {
|
||||
const result = await http.delete(`/admin/protocol/${protocol}`);
|
||||
this.fetchProtocols();
|
||||
return result;
|
||||
},
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue