2023-12-20 16:10:32 +01:00
|
|
|
<template>
|
|
|
|
|
<div class="flex flex-wrap flex-col">
|
2024-04-15 19:39:03 +02:00
|
|
|
<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>
|
2024-04-15 19:39:03 +02:00
|
|
|
</div>
|
2023-12-20 16:10:32 +01:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2024-04-15 19:39:03 +02:00
|
|
|
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"
|
2024-04-15 19:39:03 +02:00
|
|
|
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({
|
2024-04-15 19:39:03 +02:00
|
|
|
middleware: "auth",
|
2024-01-02 16:35:16 +01:00
|
|
|
})
|
|
|
|
|
|
2024-01-04 18:38:50 +01:00
|
|
|
useOpnSeoMeta({
|
2024-04-15 19:39:03 +02:00
|
|
|
title: "Create a new Form",
|
2024-01-04 18:38:50 +01:00
|
|
|
})
|
2023-12-20 16:10:32 +01:00
|
|
|
|
2024-01-04 17:05:55 +01:00
|
|
|
onBeforeRouteLeave((to, from, next) => {
|
|
|
|
|
if (isDirty()) {
|
2024-04-15 19:39:03 +02:00
|
|
|
return useAlert().confirm(
|
|
|
|
|
"Changes you made may not be saved. Are you sure want to leave?",
|
|
|
|
|
() => {
|
|
|
|
|
window.onbeforeunload = null
|
|
|
|
|
next()
|
|
|
|
|
},
|
|
|
|
|
() => {},
|
|
|
|
|
)
|
2024-01-04 17:05:55 +01:00
|
|
|
}
|
|
|
|
|
next()
|
|
|
|
|
})
|
2023-12-20 16:10:32 +01:00
|
|
|
|
|
|
|
|
const route = useRoute()
|
|
|
|
|
const templatesStore = useTemplatesStore()
|
|
|
|
|
const workingFormStore = useWorkingFormStore()
|
|
|
|
|
const workspacesStore = useWorkspacesStore()
|
|
|
|
|
const formStore = useFormsStore()
|
|
|
|
|
|
2024-01-03 11:30:48 +01:00
|
|
|
// Fetch the template
|
|
|
|
|
if (route.query.template !== undefined && route.query.template) {
|
2024-04-15 19:39:03 +02:00
|
|
|
const { data } = await fetchTemplate(route.query.template)
|
2024-01-03 11:30:48 +01:00
|
|
|
templatesStore.save(data.value)
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 16:10:32 +01:00
|
|
|
const {
|
|
|
|
|
getCurrent: workspace,
|
2024-04-15 19:39:03 +02:00
|
|
|
workspacesLoading: workspacesLoading,
|
2023-12-20 16:10:32 +01:00
|
|
|
} = storeToRefs(workspacesStore)
|
2024-04-15 19:39:03 +02:00
|
|
|
const { content: form } = storeToRefs(workingFormStore)
|
2023-12-20 16:10:32 +01:00
|
|
|
|
|
|
|
|
// State
|
|
|
|
|
const loading = ref(false)
|
2024-04-15 19:39:03 +02:00
|
|
|
const error = ref("")
|
2023-12-20 16:10:32 +01:00
|
|
|
const showInitialFormModal = ref(false)
|
|
|
|
|
const formInitialHash = ref(null)
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
watch(
|
|
|
|
|
() => workspace,
|
|
|
|
|
() => {
|
|
|
|
|
if (workspace) {
|
|
|
|
|
form.workspace_id = workspace.value.id
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
)
|
2023-12-20 16:10:32 +01:00
|
|
|
|
|
|
|
|
onMounted(() => {
|
2024-03-28 17:59:41 +01:00
|
|
|
if (import.meta.client) {
|
2024-01-04 17:05:55 +01:00
|
|
|
window.onbeforeunload = () => {
|
|
|
|
|
if (isDirty()) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-12-20 16:10:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!formStore.allLoaded) {
|
2024-01-03 11:30:48 +01:00
|
|
|
formStore.loadAll(workspace.value.id)
|
2023-12-20 16:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
form.value = initForm({ workspace_id: workspace.value?.id }, true)
|
2024-01-03 11:30:48 +01:00
|
|
|
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) {
|
2024-04-15 19:39:03 +02:00
|
|
|
form.value = useForm({ ...form.value.data(), ...template.structure })
|
2024-01-03 11:30:48 +01:00
|
|
|
}
|
|
|
|
|
} 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) => {
|
2024-04-15 19:39:03 +02:00
|
|
|
form.value = useForm({ ...form.value.data(), ...newForm })
|
2023-12-20 16:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isDirty = () => {
|
2024-04-15 19:39:03 +02:00
|
|
|
return (
|
|
|
|
|
!loading.value &&
|
|
|
|
|
formInitialHash.value &&
|
|
|
|
|
formInitialHash.value !== hash(JSON.stringify(form.value.data()))
|
|
|
|
|
)
|
2023-12-20 16:10:32 +01:00
|
|
|
}
|
|
|
|
|
</script>
|