From cea22ed0efb47fcd31ffb20a247e7f2a9f3d55b9 Mon Sep 17 00:00:00 2001 From: Sockenklaus Date: Sun, 14 Nov 2021 16:24:27 +0100 Subject: [PATCH] generalized settings store --- src/stores/settings.ts | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/stores/settings.ts diff --git a/src/stores/settings.ts b/src/stores/settings.ts new file mode 100644 index 0000000..129ccc9 --- /dev/null +++ b/src/stores/settings.ts @@ -0,0 +1,83 @@ +import { defineStore } from "pinia"; +import type { StateTree } from 'pinia' +import { useEmployees } from "./employees"; +import { useUser } from "./user"; +import axios from '@/axios' +import _clone from "lodash/clone"; +import _difference from "lodash/difference"; +import _pull from "lodash/pull"; +import _has from 'lodash/has' +import _toPairs from 'lodash/toPairs' +import _kebabCase from 'lodash/kebabCase' +import _camelCase from 'lodash/camelCase' +import { useNotifications } from "./notifications"; + + + +export const useSettings = defineStore({ + + id: "settings", + + state: (): { + [index: string]: { + [index: string]: any + } + } => { + return { + settings: { + employeesIndexColumnsSelected: Array(), + } + } + }, + + actions: { + async fetchFromApi(key: string = '') { + const user = useUser() + + try { + const result = await axios.get<{key: string, value: string}[]>('settings/'+key,{ + headers: user.header + }) + + console.log(result) + + result.data.forEach((element) => { + this.settings[_camelCase(element.key)] = JSON.parse(element.value) + }) + } + catch(error){ + useNotifications().add('danger', String(error),-1) + } + }, + + /** + * Persist columnsSelected to API + */ + async persist(key: string = '') { + const user = useUser() + + let settings: Array<{ + key: string, + value: string | undefined + }> = [] + + if(key && _has(this.settings, key)) { + settings = [{key, value: this.settings[key]}] + } + else { + for(const [key, value] of Object.entries(this.settings)){ + settings.push({key: _kebabCase(key), value: JSON.stringify(value)}) + } + } + console.log(settings) + const result = await axios.post('settings', + { + settings + }, + { + headers: user.header + }) + console.log(result) + } + } +}) \ No newline at end of file