From 317bf7fbb67c828223615c60856572100ba3f645 Mon Sep 17 00:00:00 2001 From: axolotle Date: Thu, 11 Jul 2024 15:23:39 +0200 Subject: [PATCH] refactor: use useForm + rework mapStoreGetSet of ToolWebadmin --- app/src/store/utils.ts | 45 ++++++++++++++ app/src/views/tool/ToolWebadmin.vue | 93 +++++++++++++---------------- 2 files changed, 87 insertions(+), 51 deletions(-) diff --git a/app/src/store/utils.ts b/app/src/store/utils.ts index 41e30447..448f9c65 100644 --- a/app/src/store/utils.ts +++ b/app/src/store/utils.ts @@ -1,6 +1,13 @@ +import type { WritableComputedRef } from 'vue' import { computed } from 'vue' import { useStore } from 'vuex' +import type { + AnyWritableComponents, + FormField, + FormFieldDict, +} from '@/types/form' + export function useStoreGetters() { const store = useStore() return Object.fromEntries( @@ -10,3 +17,41 @@ export function useStoreGetters() { ]), ) } + +/** + * Dynamicly generate computed properties from store with get/set and automatic commit/dispatch + */ +export function useMapStoreGetSet({ + commit = [], + dispatch = [], +}: { + commit: Extract[] + dispatch: Extract[] +}) { + const store = useStore() + type Types = { + [k in keyof FFD]: FFD[k] extends + | FormField + | undefined + ? MV + : any + } + return [...commit, ...dispatch].reduce( + (obj, prop) => { + obj[prop] = computed({ + get() { + return store.getters[prop] + }, + set(value) { + const isCommit = commit.includes(prop) + const key = (isCommit ? 'SET_' : 'UPDATE_') + prop.toUpperCase() + store[isCommit ? 'commit' : 'dispatch'](key, value) + }, + }) + return obj + }, + {} as { [k in keyof FFD]: WritableComputedRef }, + ) as { + [k in keyof FFD]: WritableComputedRef + } +} diff --git a/app/src/views/tool/ToolWebadmin.vue b/app/src/views/tool/ToolWebadmin.vue index cd2fd079..9223cee4 100644 --- a/app/src/views/tool/ToolWebadmin.vue +++ b/app/src/views/tool/ToolWebadmin.vue @@ -1,93 +1,84 @@