mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 01:04:03 +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>
|
||||
Reference in New Issue
Block a user