2024-12-18 22:27:44 +01:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import type { CreateQueryViewModel, QueryViewModel, UpdateQueryViewModel } from "@/viewmodels/admin/query.models";
|
|
|
|
import { http } from "@/serverCom";
|
|
|
|
import type { AxiosResponse } from "axios";
|
|
|
|
import { useQueryBuilderStore } from "./queryBuilder";
|
|
|
|
import { useModalStore } from "../modal";
|
|
|
|
import { defineAsyncComponent, markRaw } from "vue";
|
2024-12-19 10:46:45 +01:00
|
|
|
import { useAbilityStore } from "../ability";
|
2024-12-18 22:27:44 +01:00
|
|
|
|
|
|
|
export const useQueryStoreStore = defineStore("queryStore", {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
queries: [] as Array<QueryViewModel>,
|
|
|
|
loading: "loading" as "loading" | "fetched" | "failed",
|
|
|
|
};
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
fetchQueries() {
|
|
|
|
this.loading = "loading";
|
|
|
|
http
|
|
|
|
.get("/admin/querystore")
|
|
|
|
.then((result) => {
|
|
|
|
this.queries = result.data;
|
|
|
|
this.loading = "fetched";
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.loading = "failed";
|
|
|
|
});
|
|
|
|
},
|
|
|
|
fetchQueryById(id: number): Promise<AxiosResponse<any, any>> {
|
|
|
|
return http.get(`/admin/querystore/${id}`);
|
|
|
|
},
|
|
|
|
triggerSave() {
|
|
|
|
const queryBuilderStore = useQueryBuilderStore();
|
|
|
|
const modalStore = useModalStore();
|
2024-12-19 10:46:45 +01:00
|
|
|
const abilityStore = useAbilityStore();
|
|
|
|
if (queryBuilderStore.activeQueryId != undefined && abilityStore.can("update", "settings", "query_store")) {
|
2024-12-18 22:27:44 +01:00
|
|
|
modalStore.openModal(
|
|
|
|
markRaw(
|
2024-12-19 10:46:45 +01:00
|
|
|
defineAsyncComponent(() => import("@/components/admin/settings/queryStore/UpdateQueryStoreModal.vue"))
|
|
|
|
),
|
|
|
|
queryBuilderStore.activeQueryId
|
2024-12-18 22:27:44 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
modalStore.openModal(
|
|
|
|
markRaw(
|
2024-12-19 10:46:45 +01:00
|
|
|
defineAsyncComponent(() => import("@/components/admin/settings/queryStore/CreateQueryStoreModal.vue"))
|
|
|
|
)
|
2024-12-18 22:27:44 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async createQueryStore(query: CreateQueryViewModel): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.post(`/admin/querystore`, {
|
|
|
|
query: query.query,
|
|
|
|
title: query.title,
|
|
|
|
});
|
|
|
|
if (result.status == 200) {
|
|
|
|
const queryBuilderStore = useQueryBuilderStore();
|
|
|
|
queryBuilderStore.activeQueryId = result.data;
|
|
|
|
}
|
|
|
|
this.fetchQueries();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
async updateActiveQueryStore(query: UpdateQueryViewModel): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.patch(`/admin/querystore/${query.id}`, {
|
|
|
|
query: query.query,
|
|
|
|
});
|
|
|
|
this.fetchQueries();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
async deleteQueryStore(queryStore: number): Promise<AxiosResponse<any, any>> {
|
|
|
|
const result = await http.delete(`/admin/querystore/${queryStore}`);
|
|
|
|
this.fetchQueries();
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|