base settings operations
This commit is contained in:
parent
beaf6a5926
commit
9bd663f266
3 changed files with 140 additions and 2 deletions
53
src/stores/admin/management/setting.ts
Normal file
53
src/stores/admin/management/setting.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
import { defineStore } from "pinia";
|
||||
import { http } from "@/serverCom";
|
||||
import { type SettingString, type SettingValueMapping } from "@/types/settingTypes";
|
||||
import type { AxiosResponse } from "axios";
|
||||
|
||||
export const useSettingStore = defineStore("setting", {
|
||||
state: () => {
|
||||
return {
|
||||
settings: {} as { [key in SettingString]: SettingValueMapping[key] },
|
||||
loading: "loading" as "loading" | "fetched" | "failed",
|
||||
};
|
||||
},
|
||||
getters: {
|
||||
readSetting:
|
||||
(state) =>
|
||||
<K extends SettingString>(key: K): SettingValueMapping[K] => {
|
||||
return state.settings[key];
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
fetchSettings() {
|
||||
this.loading = "loading";
|
||||
http
|
||||
.get("/admin/setting")
|
||||
.then((result) => {
|
||||
this.settings = result.data;
|
||||
this.loading = "fetched";
|
||||
})
|
||||
.catch((err) => {
|
||||
this.loading = "failed";
|
||||
});
|
||||
},
|
||||
async getSetting(key: SettingString): Promise<AxiosResponse<any, any>> {
|
||||
return await http.get(`/admin/setting/${key}`).then((res) => {
|
||||
//@ts-expect-error
|
||||
this.settings[key] = res.data;
|
||||
return res;
|
||||
});
|
||||
},
|
||||
async updateSetting<K extends SettingString>(
|
||||
key: K,
|
||||
val: SettingValueMapping[K]
|
||||
): Promise<AxiosResponse<any, any>> {
|
||||
return await http.put("/admin/setting", {
|
||||
setting: key,
|
||||
value: val,
|
||||
});
|
||||
},
|
||||
async resetSetting(key: SettingString): Promise<AxiosResponse<any, any>> {
|
||||
return await http.delete(`/admin/setting/${key}`);
|
||||
},
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue