refactor: update api calls 'cachePath' + useInitialQueries

This commit is contained in:
axolotle 2024-08-05 23:18:34 +02:00
parent 13dc8de182
commit b3df5040cc
7 changed files with 19 additions and 31 deletions

View file

@ -72,7 +72,7 @@ export const useInfos = createGlobalState(() => {
// and login prompt will be shown automaticly // and login prompt will be shown automaticly
await getYunoHostVersion() await getYunoHostVersion()
connected.value = true connected.value = true
await api.get({ uri: 'domains', cachePath: 'domainList' }) await api.get({ uri: 'domains', cachePath: 'domains' })
} }
function onLogout(route?: RouteLocationNormalizedLoaded) { function onLogout(route?: RouteLocationNormalizedLoaded) {

View file

@ -3,32 +3,29 @@ import { ref, toValue } from 'vue'
import type { APIQuery } from '@/api/api' import type { APIQuery } from '@/api/api'
import api from '@/api/api' import api from '@/api/api'
import type { Obj } from '@/types/commons'
export function useInitialQueries< export function useInitialQueries<T extends any[] = any[]>(
ResponsesType extends (Obj | string)[] = Obj[],
>(
queries: MaybeRefOrGetter<APIQuery[]> | ComputedRef<APIQuery[]>, queries: MaybeRefOrGetter<APIQuery[]> | ComputedRef<APIQuery[]>,
{ {
onQueriesResponse, onQueriesResponse,
showModal = false, showModal = false,
}: { }: {
onQueriesResponse?: (...responses: ResponsesType) => Promise<void> | void onQueriesResponse?: (...responses: T) => Promise<void> | void
showModal?: boolean showModal?: boolean
} = {}, } = {},
) { ) {
const loading = ref(true) const loading = ref(true)
const responses: Ref<ResponsesType | null> = ref(null) const responses: Ref<T | null> = ref(null)
// FIXME watch `queries` to call on change? // FIXME watch `queries` to call on change?
function call(triggerLoading = true) { function call(triggerLoading = true) {
if (triggerLoading) loading.value = true if (triggerLoading) loading.value = true
return api return api
.fetchAll(toValue(queries), { showModal, initial: true }) .fetchAll<T>(toValue(queries), { showModal, initial: true })
.then(async (responses_) => { .then(async (responses_) => {
responses.value = responses_ as ResponsesType responses.value = responses_
if (onQueriesResponse) { if (onQueriesResponse) {
await onQueriesResponse(...(responses_ as ResponsesType)) await onQueriesResponse(...responses_)
} }
loading.value = false loading.value = false
return responses return responses

View file

@ -19,7 +19,7 @@ function onSubmit(data) {
api api
.post({ .post({
uri: 'domains', uri: 'domains',
cachePath: 'domains', cachePath: `domains.${data.domain}`,
data, data,
humanKey: { key: 'domains.add', name: data.domain }, humanKey: { key: 'domains.add', name: data.domain },
}) })

View file

@ -32,8 +32,7 @@ const { loading, refetch } = useInitialQueries(
{ uri: 'domains', cachePath: 'domains' }, { uri: 'domains', cachePath: 'domains' },
{ {
uri: `domains/${props.name}`, uri: `domains/${props.name}`,
cachePath: 'domains_details', cachePath: `domainDetails.${props.name}`,
cacheParams: { domain: props.name },
}, },
{ uri: `domains/${props.name}/config?full` }, { uri: `domains/${props.name}/config?full` },
], ],
@ -120,8 +119,7 @@ async function deleteDomain() {
api api
.delete({ .delete({
uri: 'domains', uri: 'domains',
cachePath: 'domains', cachePath: `domains.${props.name}`,
cacheParams: { domain: props.name },
data, data,
humanKey: { humanKey: {
key: 'domains.delete', key: 'domains.delete',
@ -140,7 +138,7 @@ async function setAsDefaultDomain() {
api api
.put({ .put({
uri: `domains/${props.name}/main`, uri: `domains/${props.name}/main`,
cachePath: 'main_domain', cachePath: `mainDomain.${props.name}`,
data: {}, data: {},
humanKey: { key: 'domains.set_default', name: props.name }, humanKey: { key: 'domains.set_default', name: props.name },
}) })

View file

@ -120,12 +120,11 @@ async function onPermissionChanged({ option, groupName, action, applyMethod }) {
) )
if (!confirmed) return if (!confirmed) return
} }
// FIXME hacky way to update the store
api api
.put({ .put({
uri: `users/permissions/${permId}/${action}/${groupName}`, uri: `users/permissions/${permId}/${action}/${groupName}`,
cachePath: 'permissions', cachePath: `permissions.${permId}`,
cacheParams: { groupName, action, permId },
humanKey: { key: 'permissions.' + action, perm: option, name: groupName }, humanKey: { key: 'permissions.' + action, perm: option, name: groupName },
}) })
.then(() => applyMethod(option)) .then(() => applyMethod(option))
@ -135,8 +134,7 @@ function onUserChanged({ option, groupName, action, applyMethod }) {
api api
.put({ .put({
uri: `users/groups/${groupName}/${action}/${option}`, uri: `users/groups/${groupName}/${action}/${option}`,
cachePath: 'groups', cachePath: `groups.${groupName}`,
cacheParams: { groupName },
humanKey: { key: 'groups.' + action, user: option, name: groupName }, humanKey: { key: 'groups.' + action, user: option, name: groupName },
}) })
.then(() => applyMethod(option)) .then(() => applyMethod(option))
@ -156,8 +154,7 @@ async function deleteGroup(groupName) {
api api
.delete({ .delete({
uri: `users/groups/${groupName}`, uri: `users/groups/${groupName}`,
cachePath: 'groups', cachePath: `groups.${groupName}`,
cacheParams: { groupName },
humanKey: { key: 'groups.delete', name: groupName }, humanKey: { key: 'groups.delete', name: groupName },
}) })
.then(() => { .then(() => {

View file

@ -32,8 +32,7 @@ const { loading } = useInitialQueries(
[ [
{ {
uri: `users/${props.name}`, uri: `users/${props.name}`,
cachePath: 'users_details', cachePath: `userDetails.${props.name}`,
cacheParams: { username: props.name },
}, },
{ uri: 'domains', cachePath: 'domains' }, { uri: 'domains', cachePath: 'domains' },
], ],
@ -213,8 +212,7 @@ const onUserEdit = onSubmit(async (onError, serverErrors) => {
api api
.put({ .put({
uri: `users/${props.name}`, uri: `users/${props.name}`,
cachePath: 'users_details', cachePath: `userDetails.${props.name}`,
cacheParams: { username: props.name },
data, data,
humanKey: { key: 'users.update', name: props.name }, humanKey: { key: 'users.update', name: props.name },
}) })

View file

@ -12,8 +12,7 @@ const router = useRouter()
const { loading } = useInitialQueries([ const { loading } = useInitialQueries([
{ {
uri: `users/${props.name}`, uri: `users/${props.name}`,
cachePath: 'users_details', cachePath: `userDetails.${props.name}`,
cacheParams: { username: props.name },
}, },
]) ])
@ -26,8 +25,7 @@ function deleteUser() {
api api
.delete({ .delete({
uri: `users/${props.name}`, uri: `users/${props.name}`,
cachePath: 'user_details', cachePath: `userDetails.${props.name}`,
cacheParams: { username: props.name },
data, data,
humanKey: { key: 'users.delete', name: props.name }, humanKey: { key: 'users.delete', name: props.name },
}) })