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}`);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
80
src/types/settingTypes.ts
Normal file
80
src/types/settingTypes.ts
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
export type SettingTopic = "club" | "app" | "session" | "mail" | "backup" | "security";
|
||||||
|
export type SettingString =
|
||||||
|
| "club.icon"
|
||||||
|
| "club.logo"
|
||||||
|
| "club.name"
|
||||||
|
| "club.imprint"
|
||||||
|
| "club.privacy"
|
||||||
|
| "club.website"
|
||||||
|
| "app.custom_login_message"
|
||||||
|
| "app.show_link_to_calendar"
|
||||||
|
| "session.jwt_expiration"
|
||||||
|
| "session.refresh_expiration"
|
||||||
|
| "session.pwa_refresh_expiration"
|
||||||
|
| "mail.email"
|
||||||
|
| "mail.username"
|
||||||
|
| "mail.password"
|
||||||
|
| "mail.host"
|
||||||
|
| "mail.port"
|
||||||
|
| "mail.secure"
|
||||||
|
| "backup.interval"
|
||||||
|
| "backup.copies";
|
||||||
|
|
||||||
|
export type SettingTypeAtom = "longstring" | "string" | "ms" | "number" | "boolean" | "url" | "email";
|
||||||
|
export type SettingType = SettingTypeAtom | `${SettingTypeAtom}/crypt` | `${SettingTypeAtom}/rand`;
|
||||||
|
|
||||||
|
export type SettingValueMapping = {
|
||||||
|
"club.icon": string;
|
||||||
|
"club.logo": string;
|
||||||
|
"club.name": string;
|
||||||
|
"club.imprint": string;
|
||||||
|
"club.privacy": string;
|
||||||
|
"club.website": string;
|
||||||
|
"app.custom_login_message": string;
|
||||||
|
"app.show_link_to_calendar": boolean;
|
||||||
|
"session.jwt_expiration": string;
|
||||||
|
"session.refresh_expiration": string;
|
||||||
|
"session.pwa_refresh_expiration": string;
|
||||||
|
"mail.email": string;
|
||||||
|
"mail.username": string;
|
||||||
|
"mail.password": string;
|
||||||
|
"mail.host": string;
|
||||||
|
"mail.port": number;
|
||||||
|
"mail.secure": boolean;
|
||||||
|
"backup.interval": number;
|
||||||
|
"backup.copies": number;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Typsicherer Zugriff auf Settings
|
||||||
|
export type SettingDefinition<T extends SettingType | SettingTypeAtom[]> = {
|
||||||
|
type: T;
|
||||||
|
default?: string | number | boolean;
|
||||||
|
optional?: boolean;
|
||||||
|
min?: T extends "number" | `number/crypt` | `number/rand` ? number : never;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type SettingsSchema = {
|
||||||
|
[key in SettingString]: SettingDefinition<SettingType | SettingTypeAtom[]>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const settingsType: SettingsSchema = {
|
||||||
|
"club.icon": { type: "string", optional: true },
|
||||||
|
"club.logo": { type: "string", optional: true },
|
||||||
|
"club.name": { type: "string", default: "FF Admin" },
|
||||||
|
"club.imprint": { type: "url", optional: true },
|
||||||
|
"club.privacy": { type: "url", optional: true },
|
||||||
|
"club.website": { type: "url", optional: true },
|
||||||
|
"app.custom_login_message": { type: "string", optional: true },
|
||||||
|
"app.show_link_to_calendar": { type: "boolean", default: true },
|
||||||
|
"session.jwt_expiration": { type: "ms", default: "15m" },
|
||||||
|
"session.refresh_expiration": { type: "ms", default: "1d" },
|
||||||
|
"session.pwa_refresh_expiration": { type: "ms", default: "5d" },
|
||||||
|
"mail.email": { type: "email", optional: false },
|
||||||
|
"mail.username": { type: "string", optional: false },
|
||||||
|
"mail.password": { type: "string/crypt", optional: false },
|
||||||
|
"mail.host": { type: "url", optional: false },
|
||||||
|
"mail.port": { type: "number", default: 587 },
|
||||||
|
"mail.secure": { type: "boolean", default: false },
|
||||||
|
"backup.interval": { type: "number", default: 1, min: 1 },
|
||||||
|
"backup.copies": { type: "number", default: 7, min: 1 },
|
||||||
|
};
|
|
@ -18,6 +18,7 @@ import { defineComponent } from "vue";
|
||||||
import { mapState, mapActions } from "pinia";
|
import { mapState, mapActions } from "pinia";
|
||||||
import MainTemplate from "@/templates/Main.vue";
|
import MainTemplate from "@/templates/Main.vue";
|
||||||
import { useAbilityStore } from "@/stores/ability";
|
import { useAbilityStore } from "@/stores/ability";
|
||||||
|
import { useSettingStore } from "@/stores/admin/management/setting";
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
@ -25,7 +26,11 @@ export default defineComponent({
|
||||||
computed: {
|
computed: {
|
||||||
...mapState(useAbilityStore, ["can"]),
|
...mapState(useAbilityStore, ["can"]),
|
||||||
},
|
},
|
||||||
mounted() {},
|
mounted() {
|
||||||
methods: {},
|
this.fetchSettings();
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
...mapActions(useSettingStore, ["fetchSettings"]),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Reference in a new issue