opnform/client/pages/forms/[slug]/edit.vue

105 lines
2.6 KiB
Vue
Raw Normal View History

2023-12-09 14:47:03 +00:00
<template>
<div class="w-full flex flex-col">
2023-12-24 08:51:22 +00:00
<form-editor v-if="!formsLoading || form" ref="editor"
2023-12-09 14:47:03 +00:00
:is-edit="true"
@on-save="formInitialHash=null"
/>
2023-12-24 08:51:22 +00:00
<div v-else-if="!formsLoading && error" class="mt-4 rounded-lg max-w-xl mx-auto p-6 bg-red-100 text-red-500">
2023-12-09 14:47:03 +00:00
{{ error }}
</div>
<div v-else class="text-center mt-4 py-6">
2023-12-11 10:56:21 +00:00
<Loader class="h-6 w-6 text-nt-blue mx-auto" />
2023-12-09 14:47:03 +00:00
</div>
</div>
</template>
<script>
import { computed } from 'vue'
import Breadcrumb from '~/components/global/Breadcrumb.vue'
2023-12-24 08:51:22 +00:00
import FormEditor from "~/components/open/forms/components/FormEditor.vue";
import {hash} from "~/lib/utils.js";
2023-12-09 14:47:03 +00:00
export default {
name: 'EditForm',
2023-12-24 08:51:22 +00:00
components: { Breadcrumb, FormEditor },
2023-12-09 14:47:03 +00:00
middleware: 'auth',
beforeRouteLeave (to, from, next) {
if (this.isDirty()) {
return useAlert().confirm('Changes you made may not be saved. Are you sure want to leave?', () => {
2023-12-09 14:47:03 +00:00
window.onbeforeunload = null
next()
}, () => {})
}
next()
},
setup () {
const formsStore = useFormsStore()
const workingFormStore = useWorkingFormStore()
const workspacesStore = useWorkspacesStore()
2023-12-20 17:38:43 +00:00
if (!formsStore.allLoaded) {
2023-12-24 08:51:22 +00:00
formsStore.startLoading()
2023-12-20 17:38:43 +00:00
}
2023-12-24 08:51:22 +00:00
const updatedForm = storeToRefs(workingFormStore).content
const form = computed(() => formsStore.getByKey(useRoute().params.slug))
// Create a form.id watcher that updates working form
watch(form, (form) => {
if (form) {
updatedForm.value = useForm(form)
}
})
2023-12-20 17:38:43 +00:00
useOpnSeoMeta({
title: 'Edit ' + ((form && form.value) ? form.value.title : 'Your Form')
})
2023-12-09 14:47:03 +00:00
return {
formsStore,
workingFormStore,
workspacesStore,
2023-12-24 08:51:22 +00:00
updatedForm,
form,
formsLoading: computed(() => formsStore.loading),
2023-12-09 14:47:03 +00:00
}
},
data () {
return {
error: null,
formInitialHash: null
}
},
computed: {
},
2023-12-24 08:51:22 +00:00
async beforeMount() {
2023-12-09 14:47:03 +00:00
window.onbeforeunload = () => {
if (this.isDirty()) {
return false
}
}
2023-12-24 08:51:22 +00:00
if (!this.form && !this.formsStore.allLoaded) {
await this.formsStore.loadAll(this.workspacesStore.currentId)
2023-12-09 14:47:03 +00:00
}
2023-12-24 08:51:22 +00:00
this.updatedForm = useForm(this.form)
this.formInitialHash = hash(JSON.stringify(this.updatedForm.data()))
2023-12-24 08:51:22 +00:00
2023-12-09 14:47:03 +00:00
if (this.updatedForm && (!this.updatedForm.notification_settings || Array.isArray(this.updatedForm.notification_settings))) {
this.updatedForm.notification_settings = {}
}
},
methods: {
isDirty () {
return this.formInitialHash && this.formInitialHash !== hash(JSON.stringify(this.updatedForm.data()))
2023-12-09 14:47:03 +00:00
}
}
}
</script>