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

115 lines
3.1 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";
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 this.alertConfirm('Changes you made may not be saved. Are you sure want to leave?', () => {
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
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: {
metaTitle () {
return 'Edit ' + (this.form ? this.form.title : 'Your Form')
}
},
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 = this.hashString(JSON.stringify(this.updatedForm.data()))
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 () {
2023-12-24 08:51:22 +00:00
return this.formInitialHash && this.formInitialHash !== this.hashString(JSON.stringify(this.updatedForm.data()))
2023-12-09 14:47:03 +00:00
},
hashString (str, seed = 0) {
let h1 = 0xdeadbeef ^ seed
let h2 = 0x41c6ce57 ^ seed
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i)
h1 = Math.imul(h1 ^ ch, 2654435761)
h2 = Math.imul(h2 ^ ch, 1597334677)
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909)
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909)
return 4294967296 * (2097151 & h2) + (h1 >>> 0)
}
}
}
</script>