opnform-host-nginx/client/pages/forms/create/index.vue

129 lines
3.1 KiB
Vue
Raw Normal View History

2023-12-20 16:10:32 +01:00
<template>
<div class="flex flex-wrap flex-col">
<div key="2">
<create-form-base-modal
:show="showInitialFormModal"
@form-generated="formGenerated"
@close="showInitialFormModal = false"
/>
<form-editor
v-if="form && !workspacesLoading"
ref="editor"
class="w-full flex flex-grow"
:error="error"
@on-save="formInitialHash = null"
/>
<div
v-else
class="text-center mt-4 py-6"
>
<Loader class="h-6 w-6 text-nt-blue mx-auto" />
2023-12-20 16:10:32 +01:00
</div>
</div>
2023-12-20 16:10:32 +01:00
</div>
</template>
<script setup>
import { watch } from "vue"
import { initForm } from "~/composables/forms/initForm.js"
2023-12-20 16:10:32 +01:00
import FormEditor from "~/components/open/forms/components/FormEditor.vue"
import CreateFormBaseModal from "../../../components/pages/forms/create/CreateFormBaseModal.vue"
import { fetchTemplate } from "~/stores/templates.js"
import { hash } from "~/lib/utils.js"
import { onBeforeRouteLeave } from "vue-router"
2023-12-20 16:10:32 +01:00
2024-01-02 16:35:16 +01:00
definePageMeta({
middleware: "auth",
2024-01-02 16:35:16 +01:00
})
useOpnSeoMeta({
title: "Create a new Form",
})
2023-12-20 16:10:32 +01:00
onBeforeRouteLeave((to, from, next) => {
2024-05-07 12:44:54 +02:00
if (isDirty()) {
if (window.confirm('Changes you made may not be saved. Are you sure want to leave?')) {
window.onbeforeunload = null
next()
} else {
next(false)
}
}
next()
})
2023-12-20 16:10:32 +01:00
const route = useRoute()
const templatesStore = useTemplatesStore()
const workingFormStore = useWorkingFormStore()
const workspacesStore = useWorkspacesStore()
const formStore = useFormsStore()
// Fetch the template
if (route.query.template !== undefined && route.query.template) {
const { data } = await fetchTemplate(route.query.template)
templatesStore.save(data.value)
}
2023-12-20 16:10:32 +01:00
const {
getCurrent: workspace,
workspacesLoading: workspacesLoading,
2023-12-20 16:10:32 +01:00
} = storeToRefs(workspacesStore)
const { content: form } = storeToRefs(workingFormStore)
2023-12-20 16:10:32 +01:00
// State
const loading = ref(false)
const error = ref("")
2023-12-20 16:10:32 +01:00
const showInitialFormModal = ref(false)
const formInitialHash = ref(null)
watch(
() => workspace,
() => {
if (workspace) {
form.workspace_id = workspace.value.id
}
},
)
2023-12-20 16:10:32 +01:00
onMounted(() => {
if (import.meta.client) {
window.onbeforeunload = () => {
if (isDirty()) {
return false
}
2023-12-20 16:10:32 +01:00
}
}
if (!formStore.allLoaded) {
formStore.loadAll(workspace.value.id)
2023-12-20 16:10:32 +01:00
}
form.value = initForm({ workspace_id: workspace.value?.id }, true)
formInitialHash.value = hash(JSON.stringify(form.value.data()))
if (route.query.template !== undefined && route.query.template) {
const template = templatesStore.getByKey(route.query.template)
if (template && template.structure) {
form.value = useForm({ ...form.value.data(), ...template.structure })
}
} else {
// No template loaded, ask how to start
showInitialFormModal.value = true
}
// workspacesStore.loadIfEmpty()
2023-12-20 16:10:32 +01:00
})
// Methods
const formGenerated = (newForm) => {
form.value = useForm({ ...form.value.data(), ...newForm })
2023-12-20 16:10:32 +01:00
}
const isDirty = () => {
return (
!loading.value &&
formInitialHash.value &&
formInitialHash.value !== hash(JSON.stringify(form.value.data()))
)
2023-12-20 16:10:32 +01:00
}
</script>