78 lines
2.4 KiB
TypeScript
78 lines
2.4 KiB
TypeScript
import type { ApplicationFormDto } from '~~/.api-client'
|
|
|
|
export async function useApplicationFormNavigation(applicationFormId: string) {
|
|
const { t } = useI18n()
|
|
const { getApplicationFormById } = useApplicationForm()
|
|
const { getVersions } = useApplicationFormVersion()
|
|
|
|
const applicationFormAsync = useAsyncData<ApplicationFormDto>(
|
|
`application-form-${applicationFormId}`,
|
|
async () => await getApplicationFormById(applicationFormId),
|
|
{ deep: true }
|
|
)
|
|
|
|
const versionsAsync = useAsyncData(
|
|
`application-form-${applicationFormId}-versions-nav`,
|
|
async () => await getVersions(applicationFormId),
|
|
{ deep: false }
|
|
)
|
|
|
|
await Promise.all([applicationFormAsync, versionsAsync])
|
|
|
|
if (applicationFormAsync.error.value) {
|
|
throw createError({ statusText: applicationFormAsync.error.value.message })
|
|
}
|
|
|
|
const applicationForm = computed<ApplicationFormDto>(() => applicationFormAsync.data.value as ApplicationFormDto)
|
|
|
|
const latestVersionNumber = computed<number | null>(() => {
|
|
const versions = versionsAsync.data.value ?? []
|
|
if (versions.length === 0) return null
|
|
return Math.max(...versions.map((v) => v.versionNumber ?? 0))
|
|
})
|
|
|
|
const navigationLinks = computed(() => [
|
|
[
|
|
{
|
|
label: t('applicationForms.tabs.form'),
|
|
icon: 'i-lucide-file',
|
|
to: `/application-forms/${applicationForm.value.id}/0`,
|
|
exact: true
|
|
},
|
|
{
|
|
label: t('applicationForms.tabs.versions'),
|
|
icon: 'i-lucide-file-clock',
|
|
to: `/application-forms/${applicationForm.value.id}/versions`,
|
|
exact: true
|
|
}
|
|
],
|
|
[
|
|
{
|
|
label: t('applicationForms.tabs.preview'),
|
|
icon: 'i-lucide-file-text',
|
|
to: `/api/application-forms/${applicationForm.value.id}/versions/${latestVersionNumber.value}/pdf`,
|
|
target: '_blank',
|
|
disabled: Boolean(versionsAsync.error.value) || latestVersionNumber.value === null
|
|
}
|
|
]
|
|
])
|
|
|
|
function updateApplicationForm(updatedForm: ApplicationFormDto) {
|
|
applicationFormAsync.data.value = updatedForm
|
|
// Refresh the versions list so the Preview link points to the latest version.
|
|
void versionsAsync.refresh()
|
|
}
|
|
|
|
async function refresh() {
|
|
await Promise.all([applicationFormAsync.refresh(), versionsAsync.refresh()])
|
|
}
|
|
|
|
return {
|
|
applicationForm,
|
|
navigationLinks,
|
|
refresh,
|
|
updateApplicationForm,
|
|
error: applicationFormAsync.error
|
|
}
|
|
}
|