generalized settings store

This commit is contained in:
Sockenklaus
2021-11-14 16:24:27 +01:00
parent dea3e41cd5
commit cea22ed0ef

83
src/stores/settings.ts Normal file
View File

@@ -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<string>(),
}
}
},
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)
}
}
})