feat(#4): Add versioning of application form
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import type { ApplicationFormVersionDto, ApplicationFormVersionListItemDto, ApplicationFormDto } from '~~/.api-client'
|
||||
import { useApplicationFormVersionApi } from '~/composables'
|
||||
|
||||
export function useApplicationFormVersion() {
|
||||
const versionApi = useApplicationFormVersionApi()
|
||||
|
||||
async function getVersions(applicationFormId: string): Promise<ApplicationFormVersionListItemDto[]> {
|
||||
try {
|
||||
return await versionApi.getVersions(applicationFormId)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed retrieving versions for application form ${applicationFormId}:`, e)
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
async function getVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormVersionDto> {
|
||||
try {
|
||||
return await versionApi.getVersion(applicationFormId, versionNumber)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed retrieving version ${versionNumber} for application form ${applicationFormId}:`, e)
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormDto> {
|
||||
if (!applicationFormId) {
|
||||
return Promise.reject(new Error('Application form ID missing'))
|
||||
}
|
||||
|
||||
try {
|
||||
return await versionApi.restoreVersion(applicationFormId, versionNumber)
|
||||
} catch (e: unknown) {
|
||||
console.error(`Failed restoring version ${versionNumber} for application form ${applicationFormId}:`, e)
|
||||
return Promise.reject(e)
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
getVersions,
|
||||
getVersion,
|
||||
restoreVersion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
ApplicationFormVersionApi,
|
||||
Configuration,
|
||||
type ApplicationFormVersionDto,
|
||||
type ApplicationFormVersionListItemDto,
|
||||
type ApplicationFormDto
|
||||
} from '~~/.api-client'
|
||||
import { cleanDoubleSlashes, withoutTrailingSlash } from 'ufo'
|
||||
import { wrappedFetchWrap } from '~/utils/wrappedFetch'
|
||||
|
||||
export function useApplicationFormVersionApi() {
|
||||
const appBaseUrl = useRuntimeConfig().app.baseURL
|
||||
const { serverApiBasePath, clientProxyBasePath } = useRuntimeConfig().public
|
||||
|
||||
const basePath = withoutTrailingSlash(
|
||||
cleanDoubleSlashes(
|
||||
import.meta.client
|
||||
? appBaseUrl + clientProxyBasePath
|
||||
: useRequestURL().origin + clientProxyBasePath + serverApiBasePath
|
||||
)
|
||||
)
|
||||
|
||||
const versionApiClient = new ApplicationFormVersionApi(
|
||||
new Configuration({ basePath, fetchApi: wrappedFetchWrap(useRequestFetch()) })
|
||||
)
|
||||
|
||||
async function getVersions(applicationFormId: string): Promise<ApplicationFormVersionListItemDto[]> {
|
||||
return versionApiClient.getApplicationFormVersions({ id: applicationFormId })
|
||||
}
|
||||
|
||||
async function getVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormVersionDto> {
|
||||
return versionApiClient.getApplicationFormVersion({
|
||||
id: applicationFormId,
|
||||
versionNumber
|
||||
})
|
||||
}
|
||||
|
||||
async function restoreVersion(applicationFormId: string, versionNumber: number): Promise<ApplicationFormDto> {
|
||||
return versionApiClient.restoreApplicationFormVersion({
|
||||
id: applicationFormId,
|
||||
versionNumber
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
getVersions,
|
||||
getVersion,
|
||||
restoreVersion
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
export { useApplicationFormTemplate } from './applicationFormTemplate/useApplicationFormTemplate'
|
||||
export { useApplicationForm } from './applicationForm/useApplicationForm'
|
||||
export { useApplicationFormVersion } from './applicationFormVersion/useApplicationFormVersion'
|
||||
export { useApplicationFormVersionApi } from './applicationFormVersion/useApplicationFormVersionApi'
|
||||
export { useApplicationFormNavigation } from './useApplicationFormNavigation'
|
||||
export { useNotification } from './notification/useNotification'
|
||||
export { useNotificationApi } from './notification/useNotificationApi'
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import type { ApplicationFormDto } from '~~/.api-client'
|
||||
|
||||
export async function useApplicationFormNavigation(applicationFormId: string) {
|
||||
const { getApplicationFormById } = useApplicationForm()
|
||||
|
||||
const { data, error, refresh } = await useAsyncData<ApplicationFormDto>(
|
||||
`application-form-${applicationFormId}`,
|
||||
async () => await getApplicationFormById(applicationFormId)
|
||||
)
|
||||
|
||||
if (error.value) {
|
||||
throw createError({ statusText: error.value.message })
|
||||
}
|
||||
|
||||
const applicationForm = computed<ApplicationFormDto>(() => data?.value as ApplicationFormDto)
|
||||
|
||||
const navigationLinks = computed(() => [
|
||||
[
|
||||
{
|
||||
label: 'Formular',
|
||||
icon: 'i-lucide-file',
|
||||
to: `/application-forms/${applicationForm.value.id}/0`,
|
||||
exact: true
|
||||
},
|
||||
{
|
||||
label: 'Versionen',
|
||||
icon: 'i-lucide-file-clock',
|
||||
to: `/application-forms/${applicationForm.value.id}/versions`,
|
||||
exact: true
|
||||
}
|
||||
],
|
||||
[
|
||||
{
|
||||
label: 'PDF-Vorschau',
|
||||
icon: 'i-lucide-file-text',
|
||||
to: `/api/application-forms/${applicationForm.value.id}/pdf`,
|
||||
target: '_blank'
|
||||
}
|
||||
]
|
||||
])
|
||||
|
||||
const dropdownItems = [
|
||||
[
|
||||
{
|
||||
label: 'Neuer Mitbestimmungsantrag',
|
||||
icon: 'i-lucide-send',
|
||||
to: '/create'
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
function updateApplicationForm(updatedForm: ApplicationFormDto) {
|
||||
data.value = updatedForm
|
||||
}
|
||||
|
||||
return {
|
||||
applicationForm,
|
||||
navigationLinks,
|
||||
dropdownItems,
|
||||
refresh,
|
||||
updateApplicationForm,
|
||||
error
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user