From b3df5040cca54337fb09f9c6b74be4214451accc Mon Sep 17 00:00:00 2001 From: axolotle Date: Mon, 5 Aug 2024 23:18:34 +0200 Subject: [PATCH] refactor: update api calls 'cachePath' + useInitialQueries --- app/src/composables/useInfos.ts | 2 +- app/src/composables/useInitialQueries.ts | 15 ++++++--------- app/src/views/domain/DomainAdd.vue | 2 +- app/src/views/domain/DomainInfo.vue | 8 +++----- app/src/views/group/GroupList.vue | 11 ++++------- app/src/views/user/UserEdit.vue | 6 ++---- app/src/views/user/UserInfo.vue | 6 ++---- 7 files changed, 19 insertions(+), 31 deletions(-) diff --git a/app/src/composables/useInfos.ts b/app/src/composables/useInfos.ts index 46891d9e..eab03324 100644 --- a/app/src/composables/useInfos.ts +++ b/app/src/composables/useInfos.ts @@ -72,7 +72,7 @@ export const useInfos = createGlobalState(() => { // and login prompt will be shown automaticly await getYunoHostVersion() connected.value = true - await api.get({ uri: 'domains', cachePath: 'domainList' }) + await api.get({ uri: 'domains', cachePath: 'domains' }) } function onLogout(route?: RouteLocationNormalizedLoaded) { diff --git a/app/src/composables/useInitialQueries.ts b/app/src/composables/useInitialQueries.ts index d3c21067..1ccea7ba 100644 --- a/app/src/composables/useInitialQueries.ts +++ b/app/src/composables/useInitialQueries.ts @@ -3,32 +3,29 @@ import { ref, toValue } from 'vue' import type { APIQuery } from '@/api/api' import api from '@/api/api' -import type { Obj } from '@/types/commons' -export function useInitialQueries< - ResponsesType extends (Obj | string)[] = Obj[], ->( +export function useInitialQueries( queries: MaybeRefOrGetter | ComputedRef, { onQueriesResponse, showModal = false, }: { - onQueriesResponse?: (...responses: ResponsesType) => Promise | void + onQueriesResponse?: (...responses: T) => Promise | void showModal?: boolean } = {}, ) { const loading = ref(true) - const responses: Ref = ref(null) + const responses: Ref = ref(null) // FIXME watch `queries` to call on change? function call(triggerLoading = true) { if (triggerLoading) loading.value = true return api - .fetchAll(toValue(queries), { showModal, initial: true }) + .fetchAll(toValue(queries), { showModal, initial: true }) .then(async (responses_) => { - responses.value = responses_ as ResponsesType + responses.value = responses_ if (onQueriesResponse) { - await onQueriesResponse(...(responses_ as ResponsesType)) + await onQueriesResponse(...responses_) } loading.value = false return responses diff --git a/app/src/views/domain/DomainAdd.vue b/app/src/views/domain/DomainAdd.vue index 284c806a..4f8ccb22 100644 --- a/app/src/views/domain/DomainAdd.vue +++ b/app/src/views/domain/DomainAdd.vue @@ -19,7 +19,7 @@ function onSubmit(data) { api .post({ uri: 'domains', - cachePath: 'domains', + cachePath: `domains.${data.domain}`, data, humanKey: { key: 'domains.add', name: data.domain }, }) diff --git a/app/src/views/domain/DomainInfo.vue b/app/src/views/domain/DomainInfo.vue index a569678a..4d63fa40 100644 --- a/app/src/views/domain/DomainInfo.vue +++ b/app/src/views/domain/DomainInfo.vue @@ -32,8 +32,7 @@ const { loading, refetch } = useInitialQueries( { uri: 'domains', cachePath: 'domains' }, { uri: `domains/${props.name}`, - cachePath: 'domains_details', - cacheParams: { domain: props.name }, + cachePath: `domainDetails.${props.name}`, }, { uri: `domains/${props.name}/config?full` }, ], @@ -120,8 +119,7 @@ async function deleteDomain() { api .delete({ uri: 'domains', - cachePath: 'domains', - cacheParams: { domain: props.name }, + cachePath: `domains.${props.name}`, data, humanKey: { key: 'domains.delete', @@ -140,7 +138,7 @@ async function setAsDefaultDomain() { api .put({ uri: `domains/${props.name}/main`, - cachePath: 'main_domain', + cachePath: `mainDomain.${props.name}`, data: {}, humanKey: { key: 'domains.set_default', name: props.name }, }) diff --git a/app/src/views/group/GroupList.vue b/app/src/views/group/GroupList.vue index e2668785..01726e2a 100644 --- a/app/src/views/group/GroupList.vue +++ b/app/src/views/group/GroupList.vue @@ -120,12 +120,11 @@ async function onPermissionChanged({ option, groupName, action, applyMethod }) { ) if (!confirmed) return } - // FIXME hacky way to update the store + api .put({ uri: `users/permissions/${permId}/${action}/${groupName}`, - cachePath: 'permissions', - cacheParams: { groupName, action, permId }, + cachePath: `permissions.${permId}`, humanKey: { key: 'permissions.' + action, perm: option, name: groupName }, }) .then(() => applyMethod(option)) @@ -135,8 +134,7 @@ function onUserChanged({ option, groupName, action, applyMethod }) { api .put({ uri: `users/groups/${groupName}/${action}/${option}`, - cachePath: 'groups', - cacheParams: { groupName }, + cachePath: `groups.${groupName}`, humanKey: { key: 'groups.' + action, user: option, name: groupName }, }) .then(() => applyMethod(option)) @@ -156,8 +154,7 @@ async function deleteGroup(groupName) { api .delete({ uri: `users/groups/${groupName}`, - cachePath: 'groups', - cacheParams: { groupName }, + cachePath: `groups.${groupName}`, humanKey: { key: 'groups.delete', name: groupName }, }) .then(() => { diff --git a/app/src/views/user/UserEdit.vue b/app/src/views/user/UserEdit.vue index 38156a0f..b1de97be 100644 --- a/app/src/views/user/UserEdit.vue +++ b/app/src/views/user/UserEdit.vue @@ -32,8 +32,7 @@ const { loading } = useInitialQueries( [ { uri: `users/${props.name}`, - cachePath: 'users_details', - cacheParams: { username: props.name }, + cachePath: `userDetails.${props.name}`, }, { uri: 'domains', cachePath: 'domains' }, ], @@ -213,8 +212,7 @@ const onUserEdit = onSubmit(async (onError, serverErrors) => { api .put({ uri: `users/${props.name}`, - cachePath: 'users_details', - cacheParams: { username: props.name }, + cachePath: `userDetails.${props.name}`, data, humanKey: { key: 'users.update', name: props.name }, }) diff --git a/app/src/views/user/UserInfo.vue b/app/src/views/user/UserInfo.vue index fc8a596f..d0731963 100644 --- a/app/src/views/user/UserInfo.vue +++ b/app/src/views/user/UserInfo.vue @@ -12,8 +12,7 @@ const router = useRouter() const { loading } = useInitialQueries([ { uri: `users/${props.name}`, - cachePath: 'users_details', - cacheParams: { username: props.name }, + cachePath: `userDetails.${props.name}`, }, ]) @@ -26,8 +25,7 @@ function deleteUser() { api .delete({ uri: `users/${props.name}`, - cachePath: 'user_details', - cacheParams: { username: props.name }, + cachePath: `userDetails.${props.name}`, data, humanKey: { key: 'users.delete', name: props.name }, })