mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
Finalize Typescript restructure
This commit is contained in:
@@ -0,0 +1,270 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, reactive, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { required, helpers } from '@vuelidate/validators'
|
||||
import useVuelidate from '@vuelidate/core'
|
||||
import { useModalStore } from '@v2/stores/modal.store'
|
||||
import { useNotificationStore } from '@v2/stores/notification.store'
|
||||
import {
|
||||
backupService,
|
||||
type CreateBackupPayload,
|
||||
} from '@v2/api/services/backup.service'
|
||||
import { diskService, type Disk } from '@v2/api/services/disk.service'
|
||||
import {
|
||||
getErrorTranslationKey,
|
||||
handleApiError,
|
||||
} from '@v2/utils/error-handling'
|
||||
|
||||
type BackupOption = CreateBackupPayload['option']
|
||||
|
||||
interface DiskOption extends Disk {
|
||||
display_name: string
|
||||
}
|
||||
|
||||
interface BackupTypeOption {
|
||||
id: BackupOption
|
||||
label: string
|
||||
}
|
||||
|
||||
interface BackupModalData {
|
||||
disks?: DiskOption[]
|
||||
selectedDiskId?: number | null
|
||||
}
|
||||
|
||||
interface BackupForm {
|
||||
option: BackupOption | ''
|
||||
selectedDiskId: number | null
|
||||
}
|
||||
|
||||
const modalStore = useModalStore()
|
||||
const notificationStore = useNotificationStore()
|
||||
const { t } = useI18n()
|
||||
|
||||
const isSaving = ref(false)
|
||||
const isFetchingInitialData = ref(false)
|
||||
const disks = ref<DiskOption[]>([])
|
||||
|
||||
const form = reactive<BackupForm>({
|
||||
option: 'full',
|
||||
selectedDiskId: null,
|
||||
})
|
||||
|
||||
const backupTypeOptions: BackupTypeOption[] = [
|
||||
{
|
||||
id: 'full',
|
||||
label: 'full',
|
||||
},
|
||||
{
|
||||
id: 'only-db',
|
||||
label: 'only-db',
|
||||
},
|
||||
{
|
||||
id: 'only-files',
|
||||
label: 'only-files',
|
||||
},
|
||||
]
|
||||
|
||||
const modalActive = computed<boolean>(() => {
|
||||
return modalStore.active && modalStore.componentName === 'AdminBackupModal'
|
||||
})
|
||||
|
||||
const rules = computed(() => ({
|
||||
option: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
selectedDiskId: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
}))
|
||||
|
||||
const v$ = useVuelidate(rules, form)
|
||||
|
||||
async function setInitialData(): Promise<void> {
|
||||
resetForm()
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
try {
|
||||
const modalData = isBackupModalData(modalStore.data) ? modalStore.data : null
|
||||
|
||||
if (modalData?.disks?.length) {
|
||||
disks.value = modalData.disks
|
||||
form.selectedDiskId =
|
||||
modalData.selectedDiskId ??
|
||||
modalData.disks.find((disk) => disk.set_as_default)?.id ??
|
||||
modalData.disks[0]?.id ??
|
||||
null
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const response = await diskService.list({ limit: 'all' })
|
||||
|
||||
disks.value = response.data.map((disk) => ({
|
||||
...disk,
|
||||
display_name: `${disk.name} - [${disk.driver}]`,
|
||||
}))
|
||||
|
||||
const selectedDiskId =
|
||||
modalStore.data &&
|
||||
typeof modalStore.data === 'object' &&
|
||||
'id' in (modalStore.data as Record<string, unknown>)
|
||||
? Number((modalStore.data as Record<string, unknown>).id)
|
||||
: null
|
||||
|
||||
form.selectedDiskId =
|
||||
disks.value.find((disk) => disk.id === selectedDiskId)?.id ??
|
||||
disks.value.find((disk) => disk.set_as_default)?.id ??
|
||||
disks.value[0]?.id ??
|
||||
null
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function createBackup(): Promise<void> {
|
||||
v$.value.$touch()
|
||||
|
||||
if (v$.value.$invalid || !form.selectedDiskId) {
|
||||
return
|
||||
}
|
||||
|
||||
isSaving.value = true
|
||||
|
||||
try {
|
||||
const response = await backupService.create({
|
||||
option: form.option as BackupOption,
|
||||
file_disk_id: form.selectedDiskId,
|
||||
})
|
||||
|
||||
if (response.success) {
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.backup.created_message'),
|
||||
})
|
||||
modalStore.refreshData?.()
|
||||
closeModal()
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function showApiError(error: unknown): void {
|
||||
const normalizedError = handleApiError(error)
|
||||
const translationKey = getErrorTranslationKey(normalizedError.message)
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: translationKey ? t(translationKey) : normalizedError.message,
|
||||
})
|
||||
}
|
||||
|
||||
function resetForm(): void {
|
||||
form.option = 'full'
|
||||
form.selectedDiskId = null
|
||||
v$.value.$reset()
|
||||
}
|
||||
|
||||
function closeModal(): void {
|
||||
modalStore.closeModal()
|
||||
|
||||
setTimeout(() => {
|
||||
resetForm()
|
||||
disks.value = []
|
||||
}, 300)
|
||||
}
|
||||
|
||||
function isBackupModalData(value: unknown): value is BackupModalData {
|
||||
return Boolean(value && typeof value === 'object')
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseModal :show="modalActive" @close="closeModal" @open="setInitialData">
|
||||
<template #header>
|
||||
<div class="flex justify-between w-full">
|
||||
{{ modalStore.title }}
|
||||
<BaseIcon
|
||||
name="XMarkIcon"
|
||||
class="w-6 h-6 text-muted cursor-pointer"
|
||||
@click="closeModal"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<form @submit.prevent="createBackup">
|
||||
<div class="p-4 md:p-6">
|
||||
<BaseInputGrid layout="one-column">
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.backup.select_backup_type')"
|
||||
:error="v$.option.$error && v$.option.$errors[0]?.$message"
|
||||
required
|
||||
>
|
||||
<BaseSelectInput
|
||||
v-model="form.option"
|
||||
:options="backupTypeOptions"
|
||||
:placeholder="$t('settings.backup.select_backup_type')"
|
||||
value-prop="id"
|
||||
@update:model-value="v$.option.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.disk.select_disk')"
|
||||
:error="
|
||||
v$.selectedDiskId.$error && v$.selectedDiskId.$errors[0]?.$message
|
||||
"
|
||||
required
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="form.selectedDiskId"
|
||||
:options="disks"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:can-deselect="false"
|
||||
:invalid="v$.selectedDiskId.$error"
|
||||
label="display_name"
|
||||
track-by="id"
|
||||
value-prop="id"
|
||||
searchable
|
||||
:placeholder="$t('settings.disk.select_disk')"
|
||||
@update:model-value="v$.selectedDiskId.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="z-0 flex justify-end p-4 border-t border-line-default border-solid"
|
||||
>
|
||||
<BaseButton
|
||||
type="button"
|
||||
variant="primary-outline"
|
||||
class="mr-3"
|
||||
@click="closeModal"
|
||||
>
|
||||
{{ $t('general.cancel') }}
|
||||
</BaseButton>
|
||||
|
||||
<BaseButton
|
||||
:loading="isSaving"
|
||||
:disabled="isSaving"
|
||||
variant="primary"
|
||||
type="submit"
|
||||
>
|
||||
<template #left="slotProps">
|
||||
<BaseIcon
|
||||
v-if="!isSaving"
|
||||
name="ArrowDownOnSquareIcon"
|
||||
:class="slotProps.class"
|
||||
/>
|
||||
</template>
|
||||
{{ $t('general.create') }}
|
||||
</BaseButton>
|
||||
</div>
|
||||
</form>
|
||||
</BaseModal>
|
||||
</template>
|
||||
@@ -0,0 +1,565 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, reactive, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { required, helpers } from '@vuelidate/validators'
|
||||
import useVuelidate from '@vuelidate/core'
|
||||
import { useModalStore } from '@v2/stores/modal.store'
|
||||
import { useNotificationStore } from '@v2/stores/notification.store'
|
||||
import {
|
||||
diskService,
|
||||
type CreateDiskPayload,
|
||||
type Disk,
|
||||
type DiskDriverValue,
|
||||
} from '@v2/api/services/disk.service'
|
||||
import {
|
||||
getErrorTranslationKey,
|
||||
handleApiError,
|
||||
} from '@v2/utils/error-handling'
|
||||
|
||||
interface DiskField {
|
||||
key: string
|
||||
labelKey: string
|
||||
placeholder?: string
|
||||
}
|
||||
|
||||
interface DiskDriverOption {
|
||||
name: string
|
||||
value: DiskDriverValue
|
||||
}
|
||||
|
||||
interface DiskForm {
|
||||
name: string
|
||||
driver: DiskDriverValue
|
||||
set_as_default: boolean
|
||||
credentials: Record<string, string>
|
||||
}
|
||||
|
||||
const DRIVER_FIELDS: Record<DiskDriverValue, DiskField[]> = {
|
||||
local: [
|
||||
{
|
||||
key: 'root',
|
||||
labelKey: 'settings.disk.local_root',
|
||||
placeholder: 'Ex. /user/root/',
|
||||
},
|
||||
],
|
||||
s3: [
|
||||
{
|
||||
key: 'root',
|
||||
labelKey: 'settings.disk.aws_root',
|
||||
placeholder: 'Ex. /user/root/',
|
||||
},
|
||||
{
|
||||
key: 'key',
|
||||
labelKey: 'settings.disk.aws_key',
|
||||
placeholder: 'Ex. KEIS4S39SERSDS',
|
||||
},
|
||||
{
|
||||
key: 'secret',
|
||||
labelKey: 'settings.disk.aws_secret',
|
||||
placeholder: 'Ex. ********',
|
||||
},
|
||||
{
|
||||
key: 'region',
|
||||
labelKey: 'settings.disk.aws_region',
|
||||
placeholder: 'Ex. us-west',
|
||||
},
|
||||
{
|
||||
key: 'bucket',
|
||||
labelKey: 'settings.disk.aws_bucket',
|
||||
placeholder: 'Ex. AppName',
|
||||
},
|
||||
],
|
||||
s3compat: [
|
||||
{
|
||||
key: 'endpoint',
|
||||
labelKey: 'settings.disk.s3_endpoint',
|
||||
placeholder: 'Ex. https://s3.example.com',
|
||||
},
|
||||
{
|
||||
key: 'key',
|
||||
labelKey: 'settings.disk.s3_key',
|
||||
placeholder: 'Ex. KEIS4S39SERSDS',
|
||||
},
|
||||
{
|
||||
key: 'secret',
|
||||
labelKey: 'settings.disk.s3_secret',
|
||||
placeholder: 'Ex. ********',
|
||||
},
|
||||
{
|
||||
key: 'region',
|
||||
labelKey: 'settings.disk.s3_region',
|
||||
placeholder: 'Ex. us-west',
|
||||
},
|
||||
{
|
||||
key: 'bucket',
|
||||
labelKey: 'settings.disk.s3_bucket',
|
||||
placeholder: 'Ex. AppName',
|
||||
},
|
||||
{
|
||||
key: 'root',
|
||||
labelKey: 'settings.disk.s3_root',
|
||||
placeholder: 'Ex. /user/root/',
|
||||
},
|
||||
],
|
||||
doSpaces: [
|
||||
{
|
||||
key: 'key',
|
||||
labelKey: 'settings.disk.do_spaces_key',
|
||||
placeholder: 'Ex. KEIS4S39SERSDS',
|
||||
},
|
||||
{
|
||||
key: 'secret',
|
||||
labelKey: 'settings.disk.do_spaces_secret',
|
||||
placeholder: 'Ex. ********',
|
||||
},
|
||||
{
|
||||
key: 'region',
|
||||
labelKey: 'settings.disk.do_spaces_region',
|
||||
placeholder: 'Ex. nyc3',
|
||||
},
|
||||
{
|
||||
key: 'bucket',
|
||||
labelKey: 'settings.disk.do_spaces_bucket',
|
||||
placeholder: 'Ex. AppName',
|
||||
},
|
||||
{
|
||||
key: 'endpoint',
|
||||
labelKey: 'settings.disk.do_spaces_endpoint',
|
||||
placeholder: 'Ex. https://nyc3.digitaloceanspaces.com',
|
||||
},
|
||||
{
|
||||
key: 'root',
|
||||
labelKey: 'settings.disk.do_spaces_root',
|
||||
placeholder: 'Ex. /user/root/',
|
||||
},
|
||||
],
|
||||
dropbox: [
|
||||
{
|
||||
key: 'token',
|
||||
labelKey: 'settings.disk.dropbox_token',
|
||||
},
|
||||
{
|
||||
key: 'key',
|
||||
labelKey: 'settings.disk.dropbox_key',
|
||||
placeholder: 'Ex. KEIS4S39SERSDS',
|
||||
},
|
||||
{
|
||||
key: 'secret',
|
||||
labelKey: 'settings.disk.dropbox_secret',
|
||||
placeholder: 'Ex. ********',
|
||||
},
|
||||
{
|
||||
key: 'app',
|
||||
labelKey: 'settings.disk.dropbox_app',
|
||||
},
|
||||
{
|
||||
key: 'root',
|
||||
labelKey: 'settings.disk.dropbox_root',
|
||||
placeholder: 'Ex. /user/root/',
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
const modalStore = useModalStore()
|
||||
const notificationStore = useNotificationStore()
|
||||
const { t } = useI18n()
|
||||
|
||||
const isSaving = ref(false)
|
||||
const isFetchingInitialData = ref(false)
|
||||
const driverOptions = ref<DiskDriverOption[]>([])
|
||||
const currentDisk = ref<Disk | null>(null)
|
||||
|
||||
const form = reactive<DiskForm>({
|
||||
name: '',
|
||||
driver: 'local',
|
||||
set_as_default: false,
|
||||
credentials: {},
|
||||
})
|
||||
|
||||
const modalActive = computed<boolean>(() => {
|
||||
return modalStore.active && modalStore.componentName === 'AdminFileDiskModal'
|
||||
})
|
||||
|
||||
const currentFields = computed<DiskField[]>(() => {
|
||||
return DRIVER_FIELDS[form.driver] ?? []
|
||||
})
|
||||
|
||||
const defaultSwitchDisabled = computed<boolean>(() => {
|
||||
return Boolean(currentDisk.value?.set_as_default)
|
||||
})
|
||||
|
||||
const rules = computed(() => {
|
||||
const credentialRules = currentFields.value.reduce<
|
||||
Record<string, { required: ReturnType<typeof helpers.withMessage> }>
|
||||
>((ruleSet, field) => {
|
||||
ruleSet[field.key] = {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
}
|
||||
|
||||
return ruleSet
|
||||
}, {})
|
||||
|
||||
return {
|
||||
name: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
driver: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
credentials: credentialRules,
|
||||
}
|
||||
})
|
||||
|
||||
const v$ = useVuelidate(rules, form)
|
||||
|
||||
async function setInitialData(): Promise<void> {
|
||||
resetForm()
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
try {
|
||||
const response = await diskService.getDrivers()
|
||||
driverOptions.value = response.drivers
|
||||
|
||||
if (isDisk(modalStore.data)) {
|
||||
currentDisk.value = modalStore.data
|
||||
form.name = currentDisk.value.name
|
||||
form.driver = currentDisk.value.driver
|
||||
form.set_as_default = currentDisk.value.set_as_default
|
||||
form.credentials = normalizeDiskCredentials(
|
||||
currentDisk.value.credentials,
|
||||
currentDisk.value.driver
|
||||
)
|
||||
} else {
|
||||
currentDisk.value = null
|
||||
form.driver = resolveInitialDriver(response.drivers, response.default)
|
||||
form.credentials = await loadDriverCredentials(form.driver)
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDriverChange(value: DiskDriverValue): Promise<void> {
|
||||
v$.value.driver.$touch()
|
||||
form.driver = value
|
||||
|
||||
const existingName = form.name
|
||||
const existingDefaultState = form.set_as_default
|
||||
|
||||
form.credentials = await loadDriverCredentials(value)
|
||||
form.name = existingName
|
||||
form.set_as_default = existingDefaultState
|
||||
}
|
||||
|
||||
async function saveDisk(): Promise<void> {
|
||||
v$.value.$touch()
|
||||
|
||||
if (v$.value.$invalid) {
|
||||
return
|
||||
}
|
||||
|
||||
isSaving.value = true
|
||||
|
||||
try {
|
||||
const payload: CreateDiskPayload = {
|
||||
name: form.name.trim(),
|
||||
driver: form.driver,
|
||||
credentials: { ...form.credentials },
|
||||
set_as_default: form.set_as_default,
|
||||
}
|
||||
|
||||
if (currentDisk.value) {
|
||||
await diskService.update(currentDisk.value.id, payload)
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.disk.success_update'),
|
||||
})
|
||||
} else {
|
||||
await diskService.create(payload)
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.disk.success_create'),
|
||||
})
|
||||
}
|
||||
|
||||
modalStore.refreshData?.()
|
||||
closeModal()
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadDriverCredentials(
|
||||
driver: DiskDriverValue
|
||||
): Promise<Record<string, string>> {
|
||||
if (currentDisk.value && currentDisk.value.driver === driver) {
|
||||
return normalizeDiskCredentials(currentDisk.value.credentials, driver)
|
||||
}
|
||||
|
||||
const defaults = await diskService.get(driver)
|
||||
return normalizeDiskCredentials(defaults, driver)
|
||||
}
|
||||
|
||||
function resolveInitialDriver(
|
||||
drivers: DiskDriverOption[],
|
||||
defaultDriver: string
|
||||
): DiskDriverValue {
|
||||
const matchedDriver = drivers.find((driver) => driver.value === defaultDriver)
|
||||
return matchedDriver?.value ?? drivers[0]?.value ?? 'local'
|
||||
}
|
||||
|
||||
function normalizeDiskCredentials(
|
||||
credentials: Disk['credentials'] | Record<string, string>,
|
||||
driver: DiskDriverValue
|
||||
): Record<string, string> {
|
||||
const emptyCredentials = createEmptyCredentials(driver)
|
||||
|
||||
if (!credentials) {
|
||||
return emptyCredentials
|
||||
}
|
||||
|
||||
if (typeof credentials === 'string') {
|
||||
try {
|
||||
const parsedCredentials = JSON.parse(credentials) as unknown
|
||||
|
||||
if (typeof parsedCredentials === 'string') {
|
||||
return {
|
||||
...emptyCredentials,
|
||||
root: parsedCredentials,
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedCredentials && typeof parsedCredentials === 'object') {
|
||||
return {
|
||||
...emptyCredentials,
|
||||
...stringifyRecord(parsedCredentials as Record<string, unknown>),
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
return {
|
||||
...emptyCredentials,
|
||||
root: credentials,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...emptyCredentials,
|
||||
...stringifyRecord(credentials as Record<string, unknown>),
|
||||
}
|
||||
}
|
||||
|
||||
function createEmptyCredentials(driver: DiskDriverValue): Record<string, string> {
|
||||
return currentFieldsFor(driver).reduce<Record<string, string>>(
|
||||
(credentialSet, field) => {
|
||||
credentialSet[field.key] = ''
|
||||
return credentialSet
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
function currentFieldsFor(driver: DiskDriverValue): DiskField[] {
|
||||
return DRIVER_FIELDS[driver] ?? []
|
||||
}
|
||||
|
||||
function stringifyRecord(
|
||||
value: Record<string, unknown>
|
||||
): Record<string, string> {
|
||||
return Object.entries(value).reduce<Record<string, string>>(
|
||||
(record, [key, entry]) => {
|
||||
record[key] = entry == null ? '' : String(entry)
|
||||
return record
|
||||
},
|
||||
{}
|
||||
)
|
||||
}
|
||||
|
||||
function touchCredential(key: string): void {
|
||||
const credentialField = (
|
||||
v$.value.credentials as Record<
|
||||
string,
|
||||
{ $touch: () => void }
|
||||
>
|
||||
)[key]
|
||||
|
||||
credentialField?.$touch()
|
||||
}
|
||||
|
||||
function credentialError(key: string): string {
|
||||
const credentialField = (
|
||||
v$.value.credentials as Record<
|
||||
string,
|
||||
{ $error: boolean; $errors: Array<{ $message: string }> }
|
||||
>
|
||||
)[key]
|
||||
|
||||
if (!credentialField?.$error) {
|
||||
return ''
|
||||
}
|
||||
|
||||
return credentialField.$errors[0]?.$message ?? ''
|
||||
}
|
||||
|
||||
function isCredentialInvalid(key: string): boolean {
|
||||
const credentialField = (
|
||||
v$.value.credentials as Record<string, { $error: boolean }>
|
||||
)[key]
|
||||
|
||||
return Boolean(credentialField?.$error)
|
||||
}
|
||||
|
||||
function showApiError(error: unknown): void {
|
||||
const normalizedError = handleApiError(error)
|
||||
const translationKey = getErrorTranslationKey(normalizedError.message)
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: translationKey ? t(translationKey) : normalizedError.message,
|
||||
})
|
||||
}
|
||||
|
||||
function resetForm(): void {
|
||||
form.name = ''
|
||||
form.driver = 'local'
|
||||
form.set_as_default = false
|
||||
form.credentials = {}
|
||||
currentDisk.value = null
|
||||
v$.value.$reset()
|
||||
}
|
||||
|
||||
function closeModal(): void {
|
||||
modalStore.closeModal()
|
||||
|
||||
setTimeout(() => {
|
||||
resetForm()
|
||||
driverOptions.value = []
|
||||
}, 300)
|
||||
}
|
||||
|
||||
function isDisk(value: unknown): value is Disk {
|
||||
return Boolean(
|
||||
value &&
|
||||
typeof value === 'object' &&
|
||||
'id' in value &&
|
||||
'driver' in value &&
|
||||
'name' in value
|
||||
)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseModal :show="modalActive" @close="closeModal" @open="setInitialData">
|
||||
<template #header>
|
||||
<div class="flex justify-between w-full">
|
||||
{{ modalStore.title }}
|
||||
<BaseIcon
|
||||
name="XMarkIcon"
|
||||
class="w-6 h-6 text-muted cursor-pointer"
|
||||
@click="closeModal"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<form @submit.prevent="saveDisk">
|
||||
<div class="p-4 md:p-6">
|
||||
<BaseInputGrid>
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.disk.name')"
|
||||
:error="v$.name.$error && v$.name.$errors[0]?.$message"
|
||||
required
|
||||
>
|
||||
<BaseInput
|
||||
v-model.trim="form.name"
|
||||
:invalid="v$.name.$error"
|
||||
@input="v$.name.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.disk.driver')"
|
||||
:error="v$.driver.$error && v$.driver.$errors[0]?.$message"
|
||||
required
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="form.driver"
|
||||
:options="driverOptions"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:can-deselect="false"
|
||||
:invalid="v$.driver.$error"
|
||||
label="name"
|
||||
track-by="value"
|
||||
value-prop="value"
|
||||
searchable
|
||||
@update:model-value="handleDriverChange"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
v-for="field in currentFields"
|
||||
:key="field.key"
|
||||
:label="$t(field.labelKey)"
|
||||
:error="credentialError(field.key)"
|
||||
required
|
||||
>
|
||||
<BaseInput
|
||||
v-model.trim="form.credentials[field.key]"
|
||||
:invalid="isCredentialInvalid(field.key)"
|
||||
:placeholder="field.placeholder"
|
||||
@input="touchCredential(field.key)"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
|
||||
<div class="mt-6 flex items-center">
|
||||
<div class="relative flex items-center w-12">
|
||||
<BaseSwitch
|
||||
v-model="form.set_as_default"
|
||||
:disabled="defaultSwitchDisabled"
|
||||
class="flex"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="ml-4">
|
||||
<p class="mb-1 text-base leading-snug text-heading">
|
||||
{{ $t('settings.disk.is_default') }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="z-0 flex justify-end p-4 border-t border-line-default border-solid"
|
||||
>
|
||||
<BaseButton
|
||||
type="button"
|
||||
variant="primary-outline"
|
||||
class="mr-3"
|
||||
@click="closeModal"
|
||||
>
|
||||
{{ $t('general.cancel') }}
|
||||
</BaseButton>
|
||||
|
||||
<BaseButton
|
||||
:loading="isSaving"
|
||||
:disabled="isSaving"
|
||||
variant="primary"
|
||||
type="submit"
|
||||
>
|
||||
<template #left="slotProps">
|
||||
<BaseIcon
|
||||
v-if="!isSaving"
|
||||
name="ArrowDownOnSquareIcon"
|
||||
:class="slotProps.class"
|
||||
/>
|
||||
</template>
|
||||
{{ currentDisk ? $t('general.update') : $t('general.save') }}
|
||||
</BaseButton>
|
||||
</div>
|
||||
</form>
|
||||
</BaseModal>
|
||||
</template>
|
||||
@@ -0,0 +1,105 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { required, helpers } from '@vuelidate/validators'
|
||||
import useVuelidate from '@vuelidate/core'
|
||||
import type { PdfDriver } from '@v2/api/services/pdf.service'
|
||||
|
||||
interface DomPdfForm {
|
||||
pdf_driver: string
|
||||
}
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
configData?: Record<string, unknown>
|
||||
isSaving?: boolean
|
||||
isFetchingInitialData?: boolean
|
||||
drivers?: PdfDriver[]
|
||||
}>(),
|
||||
{
|
||||
configData: () => ({}),
|
||||
isSaving: false,
|
||||
isFetchingInitialData: false,
|
||||
drivers: () => [],
|
||||
}
|
||||
)
|
||||
|
||||
const emit = defineEmits<{
|
||||
'submit-data': [config: DomPdfForm]
|
||||
'on-change-driver': [driver: string]
|
||||
}>()
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
const form = reactive<DomPdfForm>({
|
||||
pdf_driver: 'dompdf',
|
||||
})
|
||||
|
||||
const rules = computed(() => ({
|
||||
pdf_driver: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
}))
|
||||
|
||||
const v$ = useVuelidate(rules, form)
|
||||
|
||||
onMounted(() => {
|
||||
if (typeof props.configData.pdf_driver === 'string') {
|
||||
form.pdf_driver = props.configData.pdf_driver
|
||||
}
|
||||
})
|
||||
|
||||
function onChangeDriver(): void {
|
||||
v$.value.pdf_driver.$touch()
|
||||
emit('on-change-driver', form.pdf_driver)
|
||||
}
|
||||
|
||||
function saveConfig(): void {
|
||||
v$.value.$touch()
|
||||
if (v$.value.$invalid) {
|
||||
return
|
||||
}
|
||||
|
||||
emit('submit-data', { ...form })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form @submit.prevent="saveConfig">
|
||||
<BaseInputGrid>
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.pdf.driver')"
|
||||
:error="v$.pdf_driver.$error && v$.pdf_driver.$errors[0]?.$message"
|
||||
required
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="form.pdf_driver"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:options="drivers"
|
||||
:can-deselect="false"
|
||||
:invalid="v$.pdf_driver.$error"
|
||||
@update:model-value="onChangeDriver"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
|
||||
<div class="flex my-10">
|
||||
<BaseButton
|
||||
:disabled="isSaving"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:loading="isSaving"
|
||||
type="submit"
|
||||
variant="primary"
|
||||
>
|
||||
<template #left="slotProps">
|
||||
<BaseIcon
|
||||
v-if="!isSaving"
|
||||
name="ArrowDownOnSquareIcon"
|
||||
:class="slotProps.class"
|
||||
/>
|
||||
</template>
|
||||
{{ $t('general.save') }}
|
||||
</BaseButton>
|
||||
</div>
|
||||
</form>
|
||||
</template>
|
||||
@@ -0,0 +1,180 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, reactive } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { required, helpers } from '@vuelidate/validators'
|
||||
import useVuelidate from '@vuelidate/core'
|
||||
import type { PdfDriver } from '@v2/api/services/pdf.service'
|
||||
|
||||
interface GotenbergForm {
|
||||
pdf_driver: string
|
||||
gotenberg_host: string
|
||||
gotenberg_papersize: string
|
||||
}
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
configData?: Record<string, unknown>
|
||||
isSaving?: boolean
|
||||
isFetchingInitialData?: boolean
|
||||
drivers?: PdfDriver[]
|
||||
}>(),
|
||||
{
|
||||
configData: () => ({}),
|
||||
isSaving: false,
|
||||
isFetchingInitialData: false,
|
||||
drivers: () => [],
|
||||
}
|
||||
)
|
||||
|
||||
const emit = defineEmits<{
|
||||
'submit-data': [config: GotenbergForm]
|
||||
'on-change-driver': [driver: string]
|
||||
}>()
|
||||
|
||||
const { t } = useI18n()
|
||||
|
||||
const form = reactive<GotenbergForm>({
|
||||
pdf_driver: 'gotenberg',
|
||||
gotenberg_host: '',
|
||||
gotenberg_papersize: '210mm 297mm',
|
||||
})
|
||||
|
||||
function isValidServiceUrl(value: string): boolean {
|
||||
if (!helpers.req(value)) {
|
||||
return true
|
||||
}
|
||||
|
||||
try {
|
||||
const parsedUrl = new URL(value)
|
||||
|
||||
return (
|
||||
(parsedUrl.protocol === 'http:' || parsedUrl.protocol === 'https:') &&
|
||||
parsedUrl.hostname.length > 0
|
||||
)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const rules = computed(() => ({
|
||||
pdf_driver: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
gotenberg_host: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
validServiceUrl: helpers.withMessage(
|
||||
t('validation.invalid_url'),
|
||||
isValidServiceUrl
|
||||
),
|
||||
},
|
||||
gotenberg_papersize: {
|
||||
required: helpers.withMessage(t('validation.required'), required),
|
||||
},
|
||||
}))
|
||||
|
||||
const v$ = useVuelidate(rules, form)
|
||||
|
||||
onMounted(() => {
|
||||
if (typeof props.configData.pdf_driver === 'string') {
|
||||
form.pdf_driver = props.configData.pdf_driver
|
||||
}
|
||||
|
||||
if (typeof props.configData.gotenberg_host === 'string') {
|
||||
form.gotenberg_host = props.configData.gotenberg_host
|
||||
}
|
||||
|
||||
if (typeof props.configData.gotenberg_papersize === 'string') {
|
||||
form.gotenberg_papersize = props.configData.gotenberg_papersize
|
||||
}
|
||||
})
|
||||
|
||||
function onChangeDriver(): void {
|
||||
v$.value.pdf_driver.$touch()
|
||||
emit('on-change-driver', form.pdf_driver)
|
||||
}
|
||||
|
||||
function saveConfig(): void {
|
||||
v$.value.$touch()
|
||||
if (v$.value.$invalid) {
|
||||
return
|
||||
}
|
||||
|
||||
emit('submit-data', { ...form })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form @submit.prevent="saveConfig">
|
||||
<BaseInputGrid>
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.pdf.driver')"
|
||||
:error="v$.pdf_driver.$error && v$.pdf_driver.$errors[0]?.$message"
|
||||
required
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="form.pdf_driver"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:options="drivers"
|
||||
:can-deselect="false"
|
||||
:invalid="v$.pdf_driver.$error"
|
||||
@update:model-value="onChangeDriver"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.pdf.gotenberg_host')"
|
||||
:error="
|
||||
v$.gotenberg_host.$error && v$.gotenberg_host.$errors[0]?.$message
|
||||
"
|
||||
required
|
||||
>
|
||||
<BaseInput
|
||||
v-model.trim="form.gotenberg_host"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:invalid="v$.gotenberg_host.$error"
|
||||
type="text"
|
||||
name="gotenberg_host"
|
||||
@input="v$.gotenberg_host.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.pdf.papersize')"
|
||||
:help-text="$t('settings.pdf.papersize_hint')"
|
||||
:error="
|
||||
v$.gotenberg_papersize.$error &&
|
||||
v$.gotenberg_papersize.$errors[0]?.$message
|
||||
"
|
||||
required
|
||||
>
|
||||
<BaseInput
|
||||
v-model.trim="form.gotenberg_papersize"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:invalid="v$.gotenberg_papersize.$error"
|
||||
type="text"
|
||||
name="gotenberg_papersize"
|
||||
@input="v$.gotenberg_papersize.$touch()"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</BaseInputGrid>
|
||||
|
||||
<div class="flex my-10">
|
||||
<BaseButton
|
||||
:disabled="isSaving"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:loading="isSaving"
|
||||
type="submit"
|
||||
variant="primary"
|
||||
>
|
||||
<template #left="slotProps">
|
||||
<BaseIcon
|
||||
v-if="!isSaving"
|
||||
name="ArrowDownOnSquareIcon"
|
||||
:class="slotProps.class"
|
||||
/>
|
||||
</template>
|
||||
{{ $t('general.save') }}
|
||||
</BaseButton>
|
||||
</div>
|
||||
</form>
|
||||
</template>
|
||||
@@ -7,6 +7,11 @@ const AdminCompanyEditView = () => import('./views/AdminCompanyEditView.vue')
|
||||
const AdminUsersView = () => import('./views/AdminUsersView.vue')
|
||||
const AdminUserEditView = () => import('./views/AdminUserEditView.vue')
|
||||
const AdminSettingsView = () => import('./views/AdminSettingsView.vue')
|
||||
const AdminMailConfigView = () => import('./views/settings/AdminMailConfigView.vue')
|
||||
const AdminPdfGenerationView = () => import('./views/settings/AdminPdfGenerationView.vue')
|
||||
const AdminBackupView = () => import('./views/settings/AdminBackupView.vue')
|
||||
const AdminFileDiskView = () => import('./views/settings/AdminFileDiskView.vue')
|
||||
const AdminUpdateAppView = () => import('./views/settings/AdminUpdateAppView.vue')
|
||||
|
||||
export const adminRoutes: RouteRecordRaw[] = [
|
||||
{
|
||||
@@ -15,6 +20,7 @@ export const adminRoutes: RouteRecordRaw[] = [
|
||||
meta: {
|
||||
requiresAuth: true,
|
||||
isSuperAdmin: true,
|
||||
usesAdminBootstrap: true,
|
||||
},
|
||||
children: [
|
||||
{
|
||||
@@ -65,14 +71,17 @@ export const adminRoutes: RouteRecordRaw[] = [
|
||||
isSuperAdmin: true,
|
||||
},
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
redirect: 'mail-configuration',
|
||||
},
|
||||
{
|
||||
path: 'mail-configuration',
|
||||
name: 'admin.settings.mail',
|
||||
meta: {
|
||||
isSuperAdmin: true,
|
||||
},
|
||||
// Loaded by settings sub-routes
|
||||
component: { template: '<router-view />' },
|
||||
component: AdminMailConfigView,
|
||||
},
|
||||
{
|
||||
path: 'pdf-generation',
|
||||
@@ -80,7 +89,7 @@ export const adminRoutes: RouteRecordRaw[] = [
|
||||
meta: {
|
||||
isSuperAdmin: true,
|
||||
},
|
||||
component: { template: '<router-view />' },
|
||||
component: AdminPdfGenerationView,
|
||||
},
|
||||
{
|
||||
path: 'backup',
|
||||
@@ -88,7 +97,7 @@ export const adminRoutes: RouteRecordRaw[] = [
|
||||
meta: {
|
||||
isSuperAdmin: true,
|
||||
},
|
||||
component: { template: '<router-view />' },
|
||||
component: AdminBackupView,
|
||||
},
|
||||
{
|
||||
path: 'file-disk',
|
||||
@@ -96,7 +105,7 @@ export const adminRoutes: RouteRecordRaw[] = [
|
||||
meta: {
|
||||
isSuperAdmin: true,
|
||||
},
|
||||
component: { template: '<router-view />' },
|
||||
component: AdminFileDiskView,
|
||||
},
|
||||
{
|
||||
path: 'update-app',
|
||||
@@ -104,7 +113,7 @@ export const adminRoutes: RouteRecordRaw[] = [
|
||||
meta: {
|
||||
isSuperAdmin: true,
|
||||
},
|
||||
component: { template: '<router-view />' },
|
||||
component: AdminUpdateAppView,
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useModalStore } from '@v2/stores/modal.store'
|
||||
import { useDialogStore } from '@v2/stores/dialog.store'
|
||||
import { useNotificationStore } from '@v2/stores/notification.store'
|
||||
import { backupService, type Backup } from '@v2/api/services/backup.service'
|
||||
import { diskService, type Disk } from '@v2/api/services/disk.service'
|
||||
import {
|
||||
getErrorTranslationKey,
|
||||
handleApiError,
|
||||
} from '@v2/utils/error-handling'
|
||||
import AdminBackupModal from '@v2/features/admin/components/settings/AdminBackupModal.vue'
|
||||
|
||||
interface TableColumn {
|
||||
key: string
|
||||
label?: string
|
||||
thClass?: string
|
||||
tdClass?: string
|
||||
sortable?: boolean
|
||||
}
|
||||
|
||||
interface DiskOption extends Disk {
|
||||
display_name: string
|
||||
}
|
||||
|
||||
interface FetchParams {
|
||||
page: number
|
||||
filter: Record<string, unknown>
|
||||
sort: { fieldName: string; order: string }
|
||||
}
|
||||
|
||||
interface FetchResult {
|
||||
data: Backup[]
|
||||
pagination: {
|
||||
totalPages: number
|
||||
currentPage: number
|
||||
totalCount: number
|
||||
limit: number
|
||||
}
|
||||
}
|
||||
|
||||
const modalStore = useModalStore()
|
||||
const dialogStore = useDialogStore()
|
||||
const notificationStore = useNotificationStore()
|
||||
const { t } = useI18n()
|
||||
|
||||
const table = ref<{ refresh: () => void } | null>(null)
|
||||
const disks = ref<DiskOption[]>([])
|
||||
const selectedDisk = ref<DiskOption | null>(null)
|
||||
const isFetchingInitialData = ref(false)
|
||||
const backupError = ref('')
|
||||
|
||||
const backupColumns = computed<TableColumn[]>(() => [
|
||||
{
|
||||
key: 'path',
|
||||
label: t('settings.backup.path'),
|
||||
thClass: 'extra',
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'created_at',
|
||||
label: t('settings.backup.created_at'),
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'size',
|
||||
label: t('settings.backup.size'),
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'actions',
|
||||
label: '',
|
||||
tdClass: 'text-right text-sm font-medium',
|
||||
sortable: false,
|
||||
},
|
||||
])
|
||||
|
||||
watch(
|
||||
selectedDisk,
|
||||
(newDisk, oldDisk) => {
|
||||
if (newDisk?.id && oldDisk?.id && newDisk.id !== oldDisk.id) {
|
||||
refreshTable()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
loadDisks()
|
||||
|
||||
async function loadDisks(): Promise<void> {
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
try {
|
||||
const response = await diskService.list({ limit: 'all' })
|
||||
|
||||
disks.value = response.data.map((disk) => ({
|
||||
...disk,
|
||||
display_name: `${disk.name} - [${disk.driver}]`,
|
||||
}))
|
||||
|
||||
selectedDisk.value =
|
||||
disks.value.find((disk) => disk.set_as_default) ?? disks.value[0] ?? null
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchData({ page }: FetchParams): Promise<FetchResult> {
|
||||
if (!selectedDisk.value) {
|
||||
return emptyResult(page)
|
||||
}
|
||||
|
||||
backupError.value = ''
|
||||
|
||||
try {
|
||||
const response = await backupService.list({
|
||||
disk: selectedDisk.value.driver,
|
||||
file_disk_id: selectedDisk.value.id,
|
||||
})
|
||||
|
||||
if (response.error) {
|
||||
backupError.value = t('settings.backup.invalid_disk_credentials')
|
||||
|
||||
return emptyResult(page)
|
||||
}
|
||||
|
||||
return {
|
||||
data: response.backups,
|
||||
pagination: {
|
||||
totalPages: 1,
|
||||
currentPage: 1,
|
||||
totalCount: response.backups.length,
|
||||
limit: response.backups.length || 1,
|
||||
},
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
return emptyResult(page)
|
||||
}
|
||||
}
|
||||
|
||||
async function removeBackup(backup: Backup): Promise<void> {
|
||||
if (!selectedDisk.value) {
|
||||
return
|
||||
}
|
||||
|
||||
const confirmed = await dialogStore.openDialog({
|
||||
title: t('general.are_you_sure'),
|
||||
message: t('settings.backup.backup_confirm_delete'),
|
||||
yesLabel: t('general.ok'),
|
||||
noLabel: t('general.cancel'),
|
||||
variant: 'danger',
|
||||
hideNoButton: false,
|
||||
size: 'lg',
|
||||
})
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await backupService.delete({
|
||||
disk: selectedDisk.value.driver,
|
||||
file_disk_id: selectedDisk.value.id,
|
||||
path: backup.path,
|
||||
})
|
||||
|
||||
if (response.success) {
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.backup.deleted_message'),
|
||||
})
|
||||
refreshTable()
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadBackup(backup: Backup): Promise<void> {
|
||||
if (!selectedDisk.value) {
|
||||
return
|
||||
}
|
||||
|
||||
isFetchingInitialData.value = true
|
||||
let objectUrl = ''
|
||||
|
||||
try {
|
||||
const blob = await backupService.download({
|
||||
disk: selectedDisk.value.driver,
|
||||
file_disk_id: selectedDisk.value.id,
|
||||
path: backup.path,
|
||||
})
|
||||
|
||||
objectUrl = window.URL.createObjectURL(blob)
|
||||
|
||||
const downloadLink = document.createElement('a')
|
||||
downloadLink.href = objectUrl
|
||||
downloadLink.setAttribute(
|
||||
'download',
|
||||
backup.path.split('/').pop() ?? 'backup.zip'
|
||||
)
|
||||
document.body.appendChild(downloadLink)
|
||||
downloadLink.click()
|
||||
document.body.removeChild(downloadLink)
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
} finally {
|
||||
if (objectUrl) {
|
||||
window.URL.revokeObjectURL(objectUrl)
|
||||
}
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function openCreateBackupModal(): void {
|
||||
modalStore.openModal({
|
||||
title: t('settings.backup.create_backup'),
|
||||
componentName: 'AdminBackupModal',
|
||||
size: 'sm',
|
||||
data: {
|
||||
disks: disks.value,
|
||||
selectedDiskId: selectedDisk.value?.id ?? null,
|
||||
},
|
||||
refreshData: table.value?.refresh,
|
||||
})
|
||||
}
|
||||
|
||||
function refreshTable(): void {
|
||||
table.value?.refresh()
|
||||
}
|
||||
|
||||
function emptyResult(page: number): FetchResult {
|
||||
return {
|
||||
data: [],
|
||||
pagination: {
|
||||
totalPages: 1,
|
||||
currentPage: page,
|
||||
totalCount: 0,
|
||||
limit: 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function showApiError(error: unknown): void {
|
||||
const normalizedError = handleApiError(error)
|
||||
const translationKey = getErrorTranslationKey(normalizedError.message)
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: translationKey ? t(translationKey) : normalizedError.message,
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AdminBackupModal />
|
||||
|
||||
<BaseSettingCard
|
||||
:title="$t('settings.backup.title', 1)"
|
||||
:description="$t('settings.backup.description')"
|
||||
>
|
||||
<template #action>
|
||||
<BaseButton variant="primary-outline" @click="openCreateBackupModal">
|
||||
<template #left="slotProps">
|
||||
<BaseIcon :class="slotProps.class" name="PlusIcon" />
|
||||
</template>
|
||||
{{ $t('settings.backup.new_backup') }}
|
||||
</BaseButton>
|
||||
</template>
|
||||
|
||||
<div class="grid my-14 md:grid-cols-3">
|
||||
<BaseInputGroup
|
||||
:label="$t('settings.disk.select_disk')"
|
||||
:content-loading="isFetchingInitialData"
|
||||
>
|
||||
<BaseMultiselect
|
||||
v-model="selectedDisk"
|
||||
:content-loading="isFetchingInitialData"
|
||||
:options="disks"
|
||||
track-by="id"
|
||||
value-prop="id"
|
||||
label="display_name"
|
||||
:placeholder="$t('settings.disk.select_disk')"
|
||||
object
|
||||
searchable
|
||||
class="w-full"
|
||||
/>
|
||||
</BaseInputGroup>
|
||||
</div>
|
||||
|
||||
<BaseErrorAlert
|
||||
v-if="backupError"
|
||||
class="mt-6"
|
||||
:errors="[backupError]"
|
||||
/>
|
||||
|
||||
<BaseTable
|
||||
ref="table"
|
||||
class="mt-10"
|
||||
:show-filter="false"
|
||||
:data="fetchData"
|
||||
:columns="backupColumns"
|
||||
>
|
||||
<template #cell-actions="{ row }">
|
||||
<BaseDropdown>
|
||||
<template #activator>
|
||||
<div class="inline-block">
|
||||
<BaseIcon name="EllipsisHorizontalIcon" class="text-muted" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<BaseDropdownItem @click="downloadBackup(row.data)">
|
||||
<BaseIcon name="CloudArrowDownIcon" class="mr-3 text-body" />
|
||||
{{ $t('general.download') }}
|
||||
</BaseDropdownItem>
|
||||
|
||||
<BaseDropdownItem @click="removeBackup(row.data)">
|
||||
<BaseIcon name="TrashIcon" class="mr-3 text-body" />
|
||||
{{ $t('general.delete') }}
|
||||
</BaseDropdownItem>
|
||||
</BaseDropdown>
|
||||
</template>
|
||||
</BaseTable>
|
||||
</BaseSettingCard>
|
||||
</template>
|
||||
@@ -0,0 +1,295 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useModalStore } from '@v2/stores/modal.store'
|
||||
import { useDialogStore } from '@v2/stores/dialog.store'
|
||||
import { useGlobalStore } from '@v2/stores/global.store'
|
||||
import { useNotificationStore } from '@v2/stores/notification.store'
|
||||
import { diskService, type Disk } from '@v2/api/services/disk.service'
|
||||
import {
|
||||
getErrorTranslationKey,
|
||||
handleApiError,
|
||||
} from '@v2/utils/error-handling'
|
||||
import AdminFileDiskModal from '@v2/features/admin/components/settings/AdminFileDiskModal.vue'
|
||||
|
||||
interface TableColumn {
|
||||
key: string
|
||||
label?: string
|
||||
thClass?: string
|
||||
tdClass?: string
|
||||
sortable?: boolean
|
||||
}
|
||||
|
||||
interface FetchParams {
|
||||
page: number
|
||||
filter: Record<string, unknown>
|
||||
sort: { fieldName: string; order: string }
|
||||
}
|
||||
|
||||
interface FetchResult {
|
||||
data: Disk[]
|
||||
pagination: {
|
||||
totalPages: number
|
||||
currentPage: number
|
||||
totalCount: number
|
||||
limit: number
|
||||
}
|
||||
}
|
||||
|
||||
const modalStore = useModalStore()
|
||||
const dialogStore = useDialogStore()
|
||||
const globalStore = useGlobalStore()
|
||||
const notificationStore = useNotificationStore()
|
||||
const { t } = useI18n()
|
||||
|
||||
const table = ref<{ refresh: () => void } | null>(null)
|
||||
const savePdfToDisk = ref(
|
||||
(globalStore.globalSettings?.save_pdf_to_disk ?? 'NO') === 'YES'
|
||||
)
|
||||
|
||||
const fileDiskColumns = computed<TableColumn[]>(() => [
|
||||
{
|
||||
key: 'name',
|
||||
label: t('settings.disk.disk_name'),
|
||||
thClass: 'extra',
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'driver',
|
||||
label: t('settings.disk.filesystem_driver'),
|
||||
thClass: 'extra',
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'type',
|
||||
label: t('settings.disk.disk_type'),
|
||||
thClass: 'extra',
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'set_as_default',
|
||||
label: t('settings.disk.is_default'),
|
||||
thClass: 'extra',
|
||||
tdClass: 'font-medium text-heading',
|
||||
},
|
||||
{
|
||||
key: 'actions',
|
||||
label: '',
|
||||
tdClass: 'text-right text-sm font-medium',
|
||||
sortable: false,
|
||||
},
|
||||
])
|
||||
|
||||
const savePdfToDiskField = computed<boolean>({
|
||||
get: () => savePdfToDisk.value,
|
||||
set: async (enabled) => {
|
||||
savePdfToDisk.value = enabled
|
||||
|
||||
await globalStore.updateGlobalSettings({
|
||||
data: {
|
||||
settings: {
|
||||
save_pdf_to_disk: enabled ? 'YES' : 'NO',
|
||||
},
|
||||
},
|
||||
message: t('general.setting_updated'),
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
async function fetchData({ page, sort }: FetchParams): Promise<FetchResult> {
|
||||
const response = await diskService.list({
|
||||
orderByField: sort.fieldName || 'created_at',
|
||||
orderBy: sort.order || 'desc',
|
||||
page,
|
||||
})
|
||||
|
||||
return {
|
||||
data: response.data,
|
||||
pagination: {
|
||||
totalPages: response.meta.last_page,
|
||||
currentPage: page,
|
||||
totalCount: response.meta.total,
|
||||
limit: Number(response.meta.per_page) || 5,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async function setDefaultDisk(id: number): Promise<void> {
|
||||
const confirmed = await dialogStore.openDialog({
|
||||
title: t('general.are_you_sure'),
|
||||
message: t('settings.disk.set_default_disk_confirm'),
|
||||
yesLabel: t('general.ok'),
|
||||
noLabel: t('general.cancel'),
|
||||
variant: 'primary',
|
||||
hideNoButton: false,
|
||||
size: 'lg',
|
||||
})
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
await diskService.update(id, { set_as_default: true })
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.disk.success_set_default_disk'),
|
||||
})
|
||||
|
||||
refreshTable()
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
}
|
||||
}
|
||||
|
||||
async function removeDisk(id: number): Promise<void> {
|
||||
const confirmed = await dialogStore.openDialog({
|
||||
title: t('general.are_you_sure'),
|
||||
message: t('settings.disk.confirm_delete'),
|
||||
yesLabel: t('general.ok'),
|
||||
noLabel: t('general.cancel'),
|
||||
variant: 'danger',
|
||||
hideNoButton: false,
|
||||
size: 'lg',
|
||||
})
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await diskService.delete(id)
|
||||
|
||||
if (response.success) {
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.disk.deleted_message'),
|
||||
})
|
||||
refreshTable()
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
}
|
||||
}
|
||||
|
||||
function openCreateDiskModal(): void {
|
||||
modalStore.openModal({
|
||||
title: t('settings.disk.new_disk'),
|
||||
componentName: 'AdminFileDiskModal',
|
||||
size: 'lg',
|
||||
refreshData: table.value?.refresh,
|
||||
})
|
||||
}
|
||||
|
||||
function openEditDiskModal(disk: Disk): void {
|
||||
modalStore.openModal({
|
||||
title: t('settings.disk.edit_file_disk'),
|
||||
componentName: 'AdminFileDiskModal',
|
||||
id: disk.id,
|
||||
data: disk,
|
||||
size: 'lg',
|
||||
refreshData: table.value?.refresh,
|
||||
})
|
||||
}
|
||||
|
||||
function canShowActions(disk: Disk): boolean {
|
||||
return !disk.set_as_default || disk.type !== 'SYSTEM'
|
||||
}
|
||||
|
||||
function refreshTable(): void {
|
||||
table.value?.refresh()
|
||||
}
|
||||
|
||||
function showApiError(error: unknown): void {
|
||||
const normalizedError = handleApiError(error)
|
||||
const translationKey = getErrorTranslationKey(normalizedError.message)
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: translationKey ? t(translationKey) : normalizedError.message,
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<AdminFileDiskModal />
|
||||
|
||||
<BaseSettingCard
|
||||
:title="$t('settings.disk.title', 1)"
|
||||
:description="$t('settings.disk.description')"
|
||||
>
|
||||
<template #action>
|
||||
<BaseButton variant="primary-outline" @click="openCreateDiskModal">
|
||||
<template #left="slotProps">
|
||||
<BaseIcon :class="slotProps.class" name="PlusIcon" />
|
||||
</template>
|
||||
{{ $t('settings.disk.new_disk') }}
|
||||
</BaseButton>
|
||||
</template>
|
||||
|
||||
<BaseTable
|
||||
ref="table"
|
||||
class="mt-16"
|
||||
:data="fetchData"
|
||||
:columns="fileDiskColumns"
|
||||
>
|
||||
<template #cell-set_as_default="{ row }">
|
||||
<span
|
||||
:class="
|
||||
row.data.set_as_default
|
||||
? 'bg-success text-status-green'
|
||||
: 'bg-surface-muted text-muted'
|
||||
"
|
||||
class="inline-flex rounded-full px-2 py-1 text-xs font-medium uppercase"
|
||||
>
|
||||
{{
|
||||
row.data.set_as_default ? $t('general.yes') : $t('general.no')
|
||||
}}
|
||||
</span>
|
||||
</template>
|
||||
|
||||
<template #cell-actions="{ row }">
|
||||
<BaseDropdown v-if="canShowActions(row.data)">
|
||||
<template #activator>
|
||||
<div class="inline-block">
|
||||
<BaseIcon name="EllipsisHorizontalIcon" class="text-muted" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<BaseDropdownItem
|
||||
v-if="!row.data.set_as_default"
|
||||
@click="setDefaultDisk(row.data.id)"
|
||||
>
|
||||
<BaseIcon class="mr-3 text-body" name="CheckCircleIcon" />
|
||||
{{ $t('settings.disk.set_default_disk') }}
|
||||
</BaseDropdownItem>
|
||||
|
||||
<BaseDropdownItem
|
||||
v-if="row.data.type !== 'SYSTEM'"
|
||||
@click="openEditDiskModal(row.data)"
|
||||
>
|
||||
<BaseIcon name="PencilIcon" class="mr-3 text-body" />
|
||||
{{ $t('general.edit') }}
|
||||
</BaseDropdownItem>
|
||||
|
||||
<BaseDropdownItem
|
||||
v-if="row.data.type !== 'SYSTEM' && !row.data.set_as_default"
|
||||
@click="removeDisk(row.data.id)"
|
||||
>
|
||||
<BaseIcon name="TrashIcon" class="mr-3 text-body" />
|
||||
{{ $t('general.delete') }}
|
||||
</BaseDropdownItem>
|
||||
</BaseDropdown>
|
||||
</template>
|
||||
</BaseTable>
|
||||
|
||||
<BaseDivider class="mt-8 mb-2" />
|
||||
|
||||
<BaseSwitchSection
|
||||
v-model="savePdfToDiskField"
|
||||
:title="$t('settings.disk.save_pdf_to_disk')"
|
||||
:description="$t('settings.disk.disk_setting_description')"
|
||||
/>
|
||||
</BaseSettingCard>
|
||||
</template>
|
||||
@@ -0,0 +1,127 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useModalStore } from '@v2/stores/modal.store'
|
||||
import { useNotificationStore } from '@v2/stores/notification.store'
|
||||
import { mailService } from '@v2/api/services/mail.service'
|
||||
import type { MailConfig, MailDriver } from '@v2/api/services/mail.service'
|
||||
import SmtpMailDriver from '@v2/features/company/settings/components/SmtpMailDriver.vue'
|
||||
import MailgunMailDriver from '@v2/features/company/settings/components/MailgunMailDriver.vue'
|
||||
import SesMailDriver from '@v2/features/company/settings/components/SesMailDriver.vue'
|
||||
import BasicMailDriver from '@v2/features/company/settings/components/BasicMailDriver.vue'
|
||||
import MailTestModal from '@v2/features/company/settings/components/MailTestModal.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
const modalStore = useModalStore()
|
||||
const notificationStore = useNotificationStore()
|
||||
|
||||
const isSaving = ref(false)
|
||||
const isFetchingInitialData = ref(false)
|
||||
const mailConfigData = ref<Record<string, unknown> | null>(null)
|
||||
const mailDrivers = ref<MailDriver[]>([])
|
||||
const currentMailDriver = ref('smtp')
|
||||
|
||||
loadData()
|
||||
|
||||
async function loadData(): Promise<void> {
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
try {
|
||||
const [driversResponse, configResponse] = await Promise.all([
|
||||
mailService.getDrivers(),
|
||||
mailService.getConfig(),
|
||||
])
|
||||
|
||||
mailDrivers.value = driversResponse
|
||||
mailConfigData.value = configResponse
|
||||
currentMailDriver.value = configResponse.mail_driver ?? 'smtp'
|
||||
} finally {
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const mailDriver = computed(() => {
|
||||
switch (currentMailDriver.value) {
|
||||
case 'mailgun':
|
||||
return MailgunMailDriver
|
||||
case 'ses':
|
||||
return SesMailDriver
|
||||
case 'sendmail':
|
||||
case 'mail':
|
||||
return BasicMailDriver
|
||||
default:
|
||||
return SmtpMailDriver
|
||||
}
|
||||
})
|
||||
|
||||
function changeDriver(value: string): void {
|
||||
currentMailDriver.value = value
|
||||
|
||||
if (mailConfigData.value) {
|
||||
mailConfigData.value.mail_driver = value
|
||||
}
|
||||
}
|
||||
|
||||
async function saveEmailConfig(value: MailConfig): Promise<void> {
|
||||
isSaving.value = true
|
||||
|
||||
try {
|
||||
const response = await mailService.saveConfig(value)
|
||||
|
||||
if (response.success) {
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t(`settings.success.${response.success}`),
|
||||
})
|
||||
|
||||
if (mailConfigData.value) {
|
||||
mailConfigData.value = {
|
||||
...mailConfigData.value,
|
||||
...value,
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
isSaving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function openMailTestModal(): void {
|
||||
modalStore.openModal({
|
||||
title: t('general.test_mail_conf'),
|
||||
componentName: 'MailTestModal',
|
||||
size: 'sm',
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<MailTestModal store-type="global" />
|
||||
|
||||
<BaseSettingCard
|
||||
:title="$t('settings.mail.mail_config')"
|
||||
:description="$t('settings.mail.mail_config_desc')"
|
||||
>
|
||||
<div v-if="mailConfigData" class="mt-14">
|
||||
<component
|
||||
:is="mailDriver"
|
||||
:config-data="mailConfigData"
|
||||
:is-saving="isSaving"
|
||||
:mail-drivers="mailDrivers"
|
||||
:is-fetching-initial-data="isFetchingInitialData"
|
||||
@on-change-driver="changeDriver"
|
||||
@submit-data="saveEmailConfig"
|
||||
>
|
||||
<BaseButton
|
||||
variant="primary-outline"
|
||||
type="button"
|
||||
class="ml-2"
|
||||
:content-loading="isFetchingInitialData"
|
||||
@click="openMailTestModal"
|
||||
>
|
||||
{{ $t('general.test_mail_conf') }}
|
||||
</BaseButton>
|
||||
</component>
|
||||
</div>
|
||||
</BaseSettingCard>
|
||||
</template>
|
||||
@@ -0,0 +1,96 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useNotificationStore } from '@v2/stores/notification.store'
|
||||
import { pdfService } from '@v2/api/services/pdf.service'
|
||||
import type { PdfConfig, PdfDriver } from '@v2/api/services/pdf.service'
|
||||
import AdminPdfDomDriver from '@v2/features/admin/components/settings/AdminPdfDomDriver.vue'
|
||||
import AdminPdfGotenbergDriver from '@v2/features/admin/components/settings/AdminPdfGotenbergDriver.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
const notificationStore = useNotificationStore()
|
||||
|
||||
const isSaving = ref(false)
|
||||
const isFetchingInitialData = ref(false)
|
||||
const configData = ref<Record<string, unknown> | null>(null)
|
||||
const drivers = ref<PdfDriver[]>([])
|
||||
const currentDriver = ref('dompdf')
|
||||
|
||||
loadData()
|
||||
|
||||
async function loadData(): Promise<void> {
|
||||
isFetchingInitialData.value = true
|
||||
|
||||
try {
|
||||
const [driversResponse, configResponse] = await Promise.all([
|
||||
pdfService.getDrivers(),
|
||||
pdfService.getConfig(),
|
||||
])
|
||||
|
||||
drivers.value = driversResponse
|
||||
configData.value = configResponse
|
||||
currentDriver.value = configResponse.pdf_driver ?? 'dompdf'
|
||||
} finally {
|
||||
isFetchingInitialData.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const pdfDriver = computed(() => {
|
||||
if (currentDriver.value === 'gotenberg') {
|
||||
return AdminPdfGotenbergDriver
|
||||
}
|
||||
|
||||
return AdminPdfDomDriver
|
||||
})
|
||||
|
||||
function changeDriver(value: string): void {
|
||||
currentDriver.value = value
|
||||
|
||||
if (configData.value) {
|
||||
configData.value.pdf_driver = value
|
||||
}
|
||||
}
|
||||
|
||||
async function saveConfig(value: PdfConfig): Promise<void> {
|
||||
isSaving.value = true
|
||||
|
||||
try {
|
||||
const response = await pdfService.saveConfig(value)
|
||||
|
||||
if (response.success) {
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t(`settings.pdf.${response.success}`),
|
||||
})
|
||||
|
||||
if (configData.value) {
|
||||
configData.value = {
|
||||
...configData.value,
|
||||
...value,
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
isSaving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseSettingCard
|
||||
:title="$t('settings.pdf.pdf_configuration')"
|
||||
:description="$t('settings.pdf.section_description')"
|
||||
>
|
||||
<div v-if="configData" class="mt-14">
|
||||
<component
|
||||
:is="pdfDriver"
|
||||
:config-data="configData"
|
||||
:is-saving="isSaving"
|
||||
:drivers="drivers"
|
||||
:is-fetching-initial-data="isFetchingInitialData"
|
||||
@on-change-driver="changeDriver"
|
||||
@submit-data="saveConfig"
|
||||
/>
|
||||
</div>
|
||||
</BaseSettingCard>
|
||||
</template>
|
||||
@@ -0,0 +1,481 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useDialogStore } from '@v2/stores/dialog.store'
|
||||
import { useNotificationStore } from '@v2/stores/notification.store'
|
||||
import { settingService } from '@v2/api/services/setting.service'
|
||||
import { updateService, type UpdateRelease } from '@v2/api/services/update.service'
|
||||
import {
|
||||
getErrorTranslationKey,
|
||||
handleApiError,
|
||||
} from '@v2/utils/error-handling'
|
||||
|
||||
type UpdateStepKey =
|
||||
| 'download'
|
||||
| 'unzip'
|
||||
| 'copy'
|
||||
| 'delete'
|
||||
| 'migrate'
|
||||
| 'finish'
|
||||
|
||||
type UpdateStepStatus = 'pending' | 'running' | 'finished' | 'error'
|
||||
|
||||
interface UpdateStep {
|
||||
key: UpdateStepKey
|
||||
translationKey: string
|
||||
status: UpdateStepStatus
|
||||
time: string | null
|
||||
}
|
||||
|
||||
const dialogStore = useDialogStore()
|
||||
const notificationStore = useNotificationStore()
|
||||
const { t } = useI18n()
|
||||
|
||||
const isCheckingForUpdate = ref(false)
|
||||
const isUpdating = ref(false)
|
||||
const insiderChannel = ref(false)
|
||||
const currentVersion = ref('')
|
||||
const updateRelease = ref<UpdateRelease | null>(null)
|
||||
const isMinorUpdate = ref(false)
|
||||
|
||||
const updateSteps = ref<UpdateStep[]>([
|
||||
{
|
||||
key: 'download',
|
||||
translationKey: 'settings.update_app.download_zip_file',
|
||||
status: 'pending',
|
||||
time: null,
|
||||
},
|
||||
{
|
||||
key: 'unzip',
|
||||
translationKey: 'settings.update_app.unzipping_package',
|
||||
status: 'pending',
|
||||
time: null,
|
||||
},
|
||||
{
|
||||
key: 'copy',
|
||||
translationKey: 'settings.update_app.copying_files',
|
||||
status: 'pending',
|
||||
time: null,
|
||||
},
|
||||
{
|
||||
key: 'delete',
|
||||
translationKey: 'settings.update_app.deleting_files',
|
||||
status: 'pending',
|
||||
time: null,
|
||||
},
|
||||
{
|
||||
key: 'migrate',
|
||||
translationKey: 'settings.update_app.running_migrations',
|
||||
status: 'pending',
|
||||
time: null,
|
||||
},
|
||||
{
|
||||
key: 'finish',
|
||||
translationKey: 'settings.update_app.finishing_update',
|
||||
status: 'pending',
|
||||
time: null,
|
||||
},
|
||||
])
|
||||
|
||||
const isUpdateAvailable = computed<boolean>(() => {
|
||||
return Boolean(updateRelease.value)
|
||||
})
|
||||
|
||||
const requirementEntries = computed(() => {
|
||||
return Object.entries(updateRelease.value?.extensions ?? {})
|
||||
})
|
||||
|
||||
const allowToUpdate = computed<boolean>(() => {
|
||||
return requirementEntries.value.every(([, isAvailable]) => isAvailable)
|
||||
})
|
||||
|
||||
onMounted(async () => {
|
||||
window.addEventListener('beforeunload', preventUnloadDuringUpdate)
|
||||
await loadCurrentVersion()
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener('beforeunload', preventUnloadDuringUpdate)
|
||||
})
|
||||
|
||||
async function loadCurrentVersion(): Promise<void> {
|
||||
try {
|
||||
const response = await settingService.getAppVersion()
|
||||
currentVersion.value = response.version
|
||||
insiderChannel.value = response.channel === 'insider'
|
||||
} catch (error: unknown) {
|
||||
showApiError(error)
|
||||
}
|
||||
}
|
||||
|
||||
async function checkUpdate(): Promise<void> {
|
||||
isCheckingForUpdate.value = true
|
||||
|
||||
try {
|
||||
const response = await updateService.check(
|
||||
insiderChannel.value ? 'insider' : 'stable'
|
||||
)
|
||||
|
||||
if (!response.release) {
|
||||
updateRelease.value = null
|
||||
notificationStore.showNotification({
|
||||
type: 'info',
|
||||
message: t('settings.update_app.latest_message'),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
updateRelease.value = response.release
|
||||
isMinorUpdate.value = Boolean(response.is_minor)
|
||||
resetUpdateProgress()
|
||||
} catch (error: unknown) {
|
||||
updateRelease.value = null
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isCheckingForUpdate.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function startUpdate(): Promise<void> {
|
||||
if (!updateRelease.value) {
|
||||
return
|
||||
}
|
||||
|
||||
const confirmed = await dialogStore.openDialog({
|
||||
title: t('general.are_you_sure'),
|
||||
message: t('settings.update_app.update_warning'),
|
||||
yesLabel: t('general.ok'),
|
||||
noLabel: t('general.cancel'),
|
||||
variant: 'danger',
|
||||
hideNoButton: false,
|
||||
size: 'lg',
|
||||
})
|
||||
|
||||
if (!confirmed) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!allowToUpdate.value) {
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: t('settings.update_app.requirements_not_met'),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
resetUpdateProgress()
|
||||
isUpdating.value = true
|
||||
|
||||
let updatePath: string | null = null
|
||||
|
||||
try {
|
||||
for (const step of updateSteps.value) {
|
||||
step.status = 'running'
|
||||
|
||||
switch (step.key) {
|
||||
case 'download': {
|
||||
const response = await updateService.download({
|
||||
version: updateRelease.value.version,
|
||||
})
|
||||
|
||||
updatePath = extractPath(response.path)
|
||||
break
|
||||
}
|
||||
|
||||
case 'unzip': {
|
||||
if (!updatePath) {
|
||||
throw new Error('Missing update package path.')
|
||||
}
|
||||
|
||||
const response = await updateService.unzip({ path: updatePath })
|
||||
updatePath = extractPath(response.path) ?? updatePath
|
||||
break
|
||||
}
|
||||
|
||||
case 'copy': {
|
||||
if (!updatePath) {
|
||||
throw new Error('Missing extracted update path.')
|
||||
}
|
||||
|
||||
const response = await updateService.copy({ path: updatePath })
|
||||
updatePath = extractPath(response.path) ?? updatePath
|
||||
break
|
||||
}
|
||||
|
||||
case 'delete':
|
||||
await updateService.delete({
|
||||
deleted_files: updateRelease.value.deleted_files ?? null,
|
||||
})
|
||||
break
|
||||
|
||||
case 'migrate':
|
||||
await updateService.migrate()
|
||||
break
|
||||
|
||||
case 'finish':
|
||||
await updateService.finish({
|
||||
installed: currentVersion.value,
|
||||
version: updateRelease.value.version,
|
||||
})
|
||||
break
|
||||
}
|
||||
|
||||
step.status = 'finished'
|
||||
step.time = new Date().toLocaleTimeString()
|
||||
}
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'success',
|
||||
message: t('settings.update_app.update_success'),
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
window.location.reload()
|
||||
}, 3000)
|
||||
} catch (error: unknown) {
|
||||
const currentStep = updateSteps.value.find((step) => step.status === 'running')
|
||||
|
||||
if (currentStep) {
|
||||
currentStep.status = 'error'
|
||||
currentStep.time = new Date().toLocaleTimeString()
|
||||
}
|
||||
|
||||
showApiError(error)
|
||||
} finally {
|
||||
isUpdating.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function resetUpdateProgress(): void {
|
||||
updateSteps.value = updateSteps.value.map((step) => ({
|
||||
...step,
|
||||
status: 'pending',
|
||||
time: null,
|
||||
}))
|
||||
}
|
||||
|
||||
function statusClass(step: UpdateStep): string {
|
||||
if (step.status === 'finished') {
|
||||
return 'text-status-green bg-success'
|
||||
}
|
||||
|
||||
if (step.status === 'running') {
|
||||
return 'text-primary-700 bg-primary-100'
|
||||
}
|
||||
|
||||
if (step.status === 'error') {
|
||||
return 'text-danger bg-red-200'
|
||||
}
|
||||
|
||||
return 'text-muted bg-surface-muted'
|
||||
}
|
||||
|
||||
function preventUnloadDuringUpdate(event: BeforeUnloadEvent): void {
|
||||
if (!isUpdating.value) {
|
||||
return
|
||||
}
|
||||
|
||||
event.preventDefault()
|
||||
event.returnValue = 'Update is in progress!'
|
||||
}
|
||||
|
||||
function extractPath(value: unknown): string | null {
|
||||
if (typeof value === 'string') {
|
||||
return value
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function showApiError(error: unknown): void {
|
||||
const normalizedError = handleApiError(error)
|
||||
const translationKey = getErrorTranslationKey(normalizedError.message)
|
||||
|
||||
notificationStore.showNotification({
|
||||
type: 'error',
|
||||
message: translationKey ? t(translationKey) : normalizedError.message,
|
||||
})
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseSettingCard
|
||||
:title="$t('settings.update_app.title')"
|
||||
:description="$t('settings.update_app.description')"
|
||||
>
|
||||
<div class="pb-8">
|
||||
<label class="text-sm font-medium input-label">
|
||||
{{ $t('settings.update_app.current_version') }}
|
||||
</label>
|
||||
|
||||
<div class="w-full border-b-2 border-line-light border-solid pb-4">
|
||||
<div
|
||||
class="my-2 inline-block rounded-md border border-line-default bg-surface-muted p-3 text-sm text-body"
|
||||
>
|
||||
{{ currentVersion }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="pt-4">
|
||||
<BaseCheckbox
|
||||
v-model="insiderChannel"
|
||||
:label="$t('settings.update_app.insider_consent')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<BaseButton
|
||||
:loading="isCheckingForUpdate"
|
||||
:disabled="isCheckingForUpdate || isUpdating"
|
||||
variant="primary-outline"
|
||||
class="mt-6"
|
||||
@click="checkUpdate"
|
||||
>
|
||||
{{ $t('settings.update_app.check_update') }}
|
||||
</BaseButton>
|
||||
|
||||
<BaseDivider v-if="isUpdateAvailable" class="mt-6 mb-4" />
|
||||
|
||||
<div v-if="isUpdateAvailable && updateRelease && !isUpdating" class="mt-4">
|
||||
<BaseHeading type="heading-title" class="mb-2">
|
||||
{{ $t('settings.update_app.avail_update') }}
|
||||
</BaseHeading>
|
||||
|
||||
<div class="mb-3 rounded-md bg-primary-50 p-4">
|
||||
<div class="flex">
|
||||
<div class="shrink-0">
|
||||
<BaseIcon
|
||||
name="InformationCircleIcon"
|
||||
class="h-5 w-5 text-primary-400"
|
||||
/>
|
||||
</div>
|
||||
<div class="ml-3">
|
||||
<h3 class="text-sm font-medium text-primary-800">
|
||||
{{ $t('general.note') }}
|
||||
</h3>
|
||||
<div class="mt-2 text-sm text-primary-700">
|
||||
<p>{{ $t('settings.update_app.update_warning') }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<label class="text-sm font-medium input-label">
|
||||
{{ $t('settings.update_app.next_version') }}
|
||||
</label>
|
||||
<br />
|
||||
<div
|
||||
class="my-2 inline-block rounded-md border border-line-default bg-surface-muted p-3 text-sm text-body"
|
||||
>
|
||||
{{ updateRelease.version }}
|
||||
<span v-if="isMinorUpdate" class="ml-2 text-xs text-muted">
|
||||
(minor)
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="updateRelease.description"
|
||||
class="update-rich-text mt-4 max-w-[680px] text-sm leading-snug text-muted"
|
||||
v-html="updateRelease.description"
|
||||
/>
|
||||
|
||||
<div
|
||||
v-if="updateRelease.changelog"
|
||||
class="update-rich-text mt-4 max-w-[680px] text-sm leading-snug text-muted"
|
||||
v-html="updateRelease.changelog"
|
||||
/>
|
||||
|
||||
<div v-if="requirementEntries.length" class="mt-6">
|
||||
<label class="text-sm font-medium input-label">
|
||||
{{ $t('settings.update_app.requirements') }}
|
||||
</label>
|
||||
|
||||
<table class="mt-2 w-full max-w-xl border border-line-default">
|
||||
<tbody>
|
||||
<tr
|
||||
v-for="([extension, available], index) in requirementEntries"
|
||||
:key="extension"
|
||||
:class="index === requirementEntries.length - 1 ? '' : 'border-b border-line-default'"
|
||||
>
|
||||
<td class="p-3 text-sm">
|
||||
{{ extension }}
|
||||
</td>
|
||||
<td class="p-3 text-right text-sm">
|
||||
<span
|
||||
:class="available ? 'bg-success' : 'bg-red-500'"
|
||||
class="inline-block h-4 w-4 rounded-full"
|
||||
/>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="!allowToUpdate"
|
||||
class="mt-6 rounded-md bg-red-50 p-4 text-sm text-red-700"
|
||||
>
|
||||
{{ $t('settings.update_app.requirements_not_met') }}
|
||||
</div>
|
||||
|
||||
<BaseButton
|
||||
class="mt-10"
|
||||
variant="primary"
|
||||
:disabled="!allowToUpdate"
|
||||
@click="startUpdate"
|
||||
>
|
||||
{{ $t('settings.update_app.update') }}
|
||||
</BaseButton>
|
||||
</div>
|
||||
|
||||
<div v-if="isUpdating" class="mt-4">
|
||||
<div class="mb-6 flex items-start justify-between">
|
||||
<div>
|
||||
<BaseHeading type="heading-title" class="mb-2">
|
||||
{{ $t('settings.update_app.update_progress') }}
|
||||
</BaseHeading>
|
||||
<p class="max-w-[480px] text-sm leading-snug text-muted">
|
||||
{{ $t('settings.update_app.progress_text') }}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<BaseIcon
|
||||
name="ArrowPathIcon"
|
||||
class="h-6 w-6 animate-spin text-primary-400"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<ul class="w-full list-none p-0">
|
||||
<li
|
||||
v-for="step in updateSteps"
|
||||
:key="step.key"
|
||||
class="flex w-full justify-between border-b border-line-default py-3 last:border-b-0"
|
||||
>
|
||||
<p class="m-0 text-sm leading-8">{{ $t(step.translationKey) }}</p>
|
||||
<div class="flex items-center">
|
||||
<span v-if="step.time" class="mr-3 text-xs text-muted">
|
||||
{{ step.time }}
|
||||
</span>
|
||||
<span
|
||||
:class="statusClass(step)"
|
||||
class="block rounded-full px-3 py-1 text-sm uppercase"
|
||||
>
|
||||
{{ step.status }}
|
||||
</span>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</BaseSettingCard>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.update-rich-text :deep(ul) {
|
||||
list-style: disc;
|
||||
margin-left: 1.5rem;
|
||||
}
|
||||
|
||||
.update-rich-text :deep(li) {
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user