opnform-host-nginx/client/pages/forms/[slug]/edit.vue

112 lines
2.4 KiB
Vue
Raw Normal View History

2023-12-09 15:47:03 +01:00
<template>
<div class="w-full flex flex-col">
<form-editor
v-if="(!formsLoading || form ) && !error "
ref="editor"
:is-edit="true"
@on-save="formInitialHash = null"
2023-12-09 15:47:03 +01:00
/>
<div
v-else-if="error && !formsLoading"
class="mt-4 rounded-lg max-w-xl mx-auto p-6 bg-red-100 text-red-500"
>
2023-12-09 15:47:03 +01:00
{{ error }}
</div>
<div
v-else
class="text-center mt-4 py-6"
>
<Loader class="h-6 w-6 text-nt-blue mx-auto" />
2023-12-09 15:47:03 +01:00
</div>
</div>
</template>
2024-01-18 11:37:04 +01:00
<script setup>
import { computed } from "vue"
import FormEditor from "~/components/open/forms/components/FormEditor.vue"
import { hash } from "~/lib/utils.js"
2023-12-09 15:47:03 +01:00
2024-01-18 11:37:04 +01:00
const formsStore = useFormsStore()
const workingFormStore = useWorkingFormStore()
const workspacesStore = useWorkspacesStore()
2023-12-09 15:47:03 +01:00
2024-01-18 11:37:04 +01:00
if (!formsStore.allLoaded) {
formsStore.startLoading()
}
const updatedForm = storeToRefs(workingFormStore).content
const form = computed(() => formsStore.getByKey(useRoute().params.slug))
const formsLoading = computed(() => formsStore.loading)
2023-12-09 15:47:03 +01:00
2024-01-18 11:37:04 +01:00
const error = ref(null)
const formInitialHash = ref(null)
2023-12-20 18:38:43 +01:00
2024-01-18 11:37:04 +01:00
function isDirty() {
try {
return (
formInitialHash.value &&
updatedForm.value &&
formInitialHash.value !==
hash(JSON.stringify(updatedForm?.value?.data() ?? null))
)
} catch (e) {
return false
}
2024-01-18 11:37:04 +01:00
}
2023-12-24 09:51:22 +01:00
2024-01-18 11:37:04 +01:00
function initUpdatedForm() {
if (!form.value || !form.value) {
2024-01-18 11:37:04 +01:00
return
}
2023-12-20 18:38:43 +01:00
2024-01-18 11:37:04 +01:00
updatedForm.value = useForm(form.value)
if (!updatedForm.value) {
return
}
formInitialHash.value = hash(JSON.stringify(updatedForm.value.data()))
}
2023-12-09 15:47:03 +01:00
2024-01-18 11:37:04 +01:00
// Create a form.id watcher that updates working form
watch(form, (form) => {
2024-02-10 12:46:17 +01:00
if (form?.value) {
2024-01-18 11:37:04 +01:00
initUpdatedForm()
}
})
2023-12-09 15:47:03 +01:00
2024-01-18 11:37:04 +01:00
onBeforeRouteLeave((to, from, next) => {
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)
}
2024-01-18 11:37:04 +01:00
}
next()
})
2023-12-09 15:47:03 +01:00
2024-01-18 11:37:04 +01:00
onBeforeMount(() => {
if (import.meta.client) {
2023-12-09 15:47:03 +01:00
window.onbeforeunload = () => {
2024-01-18 11:37:04 +01:00
if (isDirty()) {
2023-12-09 15:47:03 +01:00
return false
}
}
2024-01-18 11:37:04 +01:00
}
2023-12-09 15:47:03 +01:00
2024-01-18 11:37:04 +01:00
if (!form.value && !formsStore.allLoaded) {
formsStore.loadAll(workspacesStore.currentId).then(() => {
2024-01-18 11:37:04 +01:00
initUpdatedForm()
})
} else {
initUpdatedForm()
2023-12-09 15:47:03 +01:00
}
2024-01-18 11:37:04 +01:00
})
useOpnSeoMeta({
title: "Edit " + (form.value && form.value ? form.value.title : "Your Form"),
2024-01-18 11:37:04 +01:00
})
definePageMeta({
middleware: "auth",
2024-01-18 11:37:04 +01:00
})
2023-12-09 15:47:03 +01:00
</script>