refactor: rework async BackupInfo

This commit is contained in:
axolotle 2024-08-13 00:19:59 +02:00
parent ccc65b0b06
commit a0dc769461

View file

@ -7,10 +7,11 @@ import api from '@/api'
import { APIBadRequestError, type APIError } from '@/api/errors' import { APIBadRequestError, type APIError } from '@/api/errors'
import { useAutoModal } from '@/composables/useAutoModal' import { useAutoModal } from '@/composables/useAutoModal'
import { useInfos } from '@/composables/useInfos' import { useInfos } from '@/composables/useInfos'
import { useInitialQueries } from '@/composables/useInitialQueries'
import { isEmptyValue } from '@/helpers/commons' import { isEmptyValue } from '@/helpers/commons'
import { readableDate } from '@/helpers/filters/date' import { readableDate } from '@/helpers/filters/date'
import { humanSize } from '@/helpers/filters/human' import { humanSize } from '@/helpers/filters/human'
import type { BackupInfo } from '@/types/core/api'
import { formatBackupSystem, parseBackupForm } from './backupData'
const props = defineProps<{ const props = defineProps<{
id: string id: string
@ -20,63 +21,33 @@ const props = defineProps<{
const { t } = useI18n() const { t } = useI18n()
const router = useRouter() const router = useRouter()
const modalConfirm = useAutoModal() const modalConfirm = useAutoModal()
const { loading } = useInitialQueries(
[{ uri: `backups/${props.name}?with_details` }],
{ onQueriesResponse },
)
const selected = ref<string[]>([]) const { infos, system, apps } = await api
const error = ref('') .get<BackupInfo>({ uri: `backups/${props.name}?with_details` })
const isValid = ref<boolean | null>(null) .then((backup) => {
const infos = ref() return {
const apps = ref() system: formatBackupSystem(backup.system),
const system = ref() apps: backup.apps,
infos: {
const hasBackupData = computed(() => { id: props.name,
return !isEmptyValue(system.value) || !isEmptyValue(apps.value) created_at: readableDate(backup.created_at),
}) size: humanSize(backup.size),
path: backup.path,
function formatHooks(hooks) { },
const data = {}
Object.entries(hooks).forEach(([hook, { size }]) => {
const groupId = hook.startsWith('conf_')
? 'adminjs_group_configuration'
: hook
if (groupId in data) {
data[groupId].value.push(hook)
data[groupId].description += ', ' + t('hook_' + hook)
data[groupId].size += size
} else {
data[groupId] = {
name: t('hook_' + groupId),
value: [hook],
description: t(groupId === hook ? `hook_${hook}_desc` : 'hook_' + hook),
size,
}
} }
}) })
return data
}
function onQueriesResponse(data: any) { const allKeys = [...Object.keys(apps), ...Object.keys(system)]
infos.value = { const selected = ref(allKeys)
name: props.name, const serverError = ref('')
created_at: data.created_at, const isValid = ref<boolean | null>(null)
size: data.size,
path: data.path,
}
system.value = formatHooks(data.system)
apps.value = data.apps
toggleSelected() const hasBackupData = computed(() => {
} return !isEmptyValue(system) || !isEmptyValue(apps)
})
function toggleSelected(select = true) { function toggleSelected(select = true) {
if (select) { selected.value = select ? allKeys : []
selected.value = [...Object.keys(apps.value), ...Object.keys(system.value)]
} else {
selected.value = []
}
} }
async function restoreBackup() { async function restoreBackup() {
@ -85,19 +56,12 @@ async function restoreBackup() {
) )
if (!confirmed) return if (!confirmed) return
const data = { apps: [], system: [], force: '' } const data = parseBackupForm(selected.value, system)
for (const item of selected.value) {
if (item in system.value) {
data.system = [...data.system, ...system.value[item].value]
} else {
data.apps.push(item)
}
}
api api
.put({ .put({
uri: `backups/${props.name}/restore`, uri: `backups/${props.name}/restore`,
data, // FIXME force?
data: { ...data, force: '' },
humanKey: { key: 'backups.restore', name: props.name }, humanKey: { key: 'backups.restore', name: props.name },
}) })
.then(() => { .then(() => {
@ -105,7 +69,7 @@ async function restoreBackup() {
}) })
.catch((err: APIError) => { .catch((err: APIError) => {
if (!(err instanceof APIBadRequestError)) throw err if (!(err instanceof APIBadRequestError)) throw err
error.value = err.message serverError.value = err.message
isValid.value = false isValid.value = false
}) })
} }
@ -136,7 +100,7 @@ function downloadBackup() {
</script> </script>
<template> <template>
<ViewBase :loading="loading"> <div>
<!-- BACKUP INFO --> <!-- BACKUP INFO -->
<YCard :title="$t('infos')" icon="info-circle" button-unbreak="sm"> <YCard :title="$t('infos')" icon="info-circle" button-unbreak="sm">
<template #header-buttons> <template #header-buttons>
@ -152,19 +116,15 @@ function downloadBackup() {
</template> </template>
<BRow <BRow
v-for="(value, prop) in infos" v-for="(text, prop) in infos"
:key="prop" :key="prop"
no-gutters no-gutters
class="row-line" class="row-line"
> >
<BCol md="3" xl="2"> <BCol md="3" xl="2">
<strong>{{ $t(prop === 'name' ? 'id' : prop) }}</strong> <strong>{{ $t(prop) }}</strong>
</BCol>
<BCol>
<span v-if="prop === 'created_at'">{{ readableDate(value) }}</span>
<span v-else-if="prop === 'size'">{{ humanSize(value) }}</span>
<span v-else>{{ value }}</span>
</BCol> </BCol>
<BCol>{{ text }}</BCol>
</BRow> </BRow>
</YCard> </YCard>
@ -244,7 +204,7 @@ function downloadBackup() {
<BFormInvalidFeedback id="backup-restore-feedback" :state="isValid"> <BFormInvalidFeedback id="backup-restore-feedback" :state="isValid">
<BAlert :modelValue="true" variant="danger" class="mb-0"> <BAlert :modelValue="true" variant="danger" class="mb-0">
{{ error }} {{ serverError }}
</BAlert> </BAlert>
</BFormInvalidFeedback> </BFormInvalidFeedback>
</BFormCheckboxGroup> </BFormCheckboxGroup>
@ -264,10 +224,5 @@ function downloadBackup() {
/> />
</template> </template>
</YCard> </YCard>
</div>
<template #skeleton>
<CardInfoSkeleton :item-count="4" />
<CardListSkeleton />
</template>
</ViewBase>
</template> </template>