2022-09-20 19:59:52 +00:00
|
|
|
<template>
|
|
|
|
<div class="w-full flex flex-col">
|
|
|
|
<form-editor v-if="pageLoaded" ref="editor"
|
2023-10-14 16:24:44 +00:00
|
|
|
:is-edit="true"
|
2023-05-21 17:34:47 +00:00
|
|
|
@on-save="formInitialHash=null"
|
2022-09-20 19:59:52 +00:00
|
|
|
/>
|
|
|
|
<div v-else-if="!loading && error" class="mt-4 rounded-lg max-w-xl mx-auto p-6 bg-red-100 text-red-500">
|
|
|
|
{{ error }}
|
|
|
|
</div>
|
|
|
|
<div v-else class="text-center mt-4 py-6">
|
|
|
|
<loader class="h-6 w-6 text-nt-blue mx-auto" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-12-01 17:57:14 +00:00
|
|
|
import { computed } from 'vue'
|
2022-09-20 19:59:52 +00:00
|
|
|
import Form from 'vform'
|
2023-12-01 17:57:14 +00:00
|
|
|
import { useFormsStore } from '../../stores/forms'
|
|
|
|
import { useWorkingFormStore } from '../../stores/working_form'
|
|
|
|
import { useWorkspacesStore } from '../../stores/workspaces'
|
|
|
|
import Breadcrumb from '../../components/common/Breadcrumb.vue'
|
2023-01-21 11:57:37 +00:00
|
|
|
import SeoMeta from '../../mixins/seo-meta.js'
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
const loadForms = function () {
|
2023-12-01 17:57:14 +00:00
|
|
|
const formsStore = useFormsStore()
|
|
|
|
const workspacesStore = useWorkspacesStore()
|
|
|
|
formsStore.startLoading()
|
|
|
|
workspacesStore.loadIfEmpty().then(() => {
|
|
|
|
formsStore.load(workspacesStore.currentId)
|
2022-09-20 19:59:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'EditForm',
|
2022-12-14 09:27:21 +00:00
|
|
|
components: { Breadcrumb },
|
2023-10-14 16:24:44 +00:00
|
|
|
mixins: [SeoMeta],
|
2023-12-01 17:57:14 +00:00
|
|
|
middleware: 'auth',
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
beforeRouteEnter (to, from, next) {
|
2023-12-01 17:57:14 +00:00
|
|
|
const formsStore = useFormsStore()
|
|
|
|
const workingFormStore = useWorkingFormStore()
|
|
|
|
if (!formsStore.getBySlug(to.params.slug)) {
|
2022-09-20 19:59:52 +00:00
|
|
|
loadForms()
|
|
|
|
}
|
2023-12-01 17:57:14 +00:00
|
|
|
workingFormStore.set(null) // Reset old working form
|
2022-09-20 19:59:52 +00:00
|
|
|
next()
|
|
|
|
},
|
2023-05-21 17:34:47 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
},
|
|
|
|
|
2023-12-01 17:57:14 +00:00
|
|
|
setup () {
|
|
|
|
const formsStore = useFormsStore()
|
|
|
|
const workingFormStore = useWorkingFormStore()
|
|
|
|
const workspacesStore = useWorkspacesStore()
|
|
|
|
return {
|
|
|
|
formsStore,
|
|
|
|
workingFormStore,
|
|
|
|
workspacesStore,
|
|
|
|
formsLoading : computed(() => formsStore.loading)
|
|
|
|
}
|
|
|
|
},
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
loading: false,
|
|
|
|
error: null,
|
2023-05-21 17:34:47 +00:00
|
|
|
formInitialHash: null
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
computed: {
|
|
|
|
updatedForm: {
|
|
|
|
get () {
|
2023-12-01 17:57:14 +00:00
|
|
|
return this.workingFormStore.content
|
2022-09-20 19:59:52 +00:00
|
|
|
},
|
|
|
|
/* We add a setter */
|
|
|
|
set (value) {
|
2023-12-01 17:57:14 +00:00
|
|
|
this.workingFormStore.set(value)
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
form () {
|
2023-12-01 17:57:14 +00:00
|
|
|
return this.formsStore.getBySlug(this.$route.params.slug)
|
2022-09-20 19:59:52 +00:00
|
|
|
},
|
|
|
|
pageLoaded () {
|
|
|
|
return !this.loading && this.updatedForm !== null
|
2022-12-22 10:55:17 +00:00
|
|
|
},
|
|
|
|
metaTitle () {
|
|
|
|
return 'Edit ' + (this.form ? this.form.title : 'Your Form')
|
2023-10-14 16:24:44 +00:00
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
form () {
|
|
|
|
this.updatedForm = new Form(this.form)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-09-05 14:25:33 +00:00
|
|
|
created () {},
|
2023-10-14 16:24:44 +00:00
|
|
|
unmounted () {},
|
2022-09-20 19:59:52 +00:00
|
|
|
|
|
|
|
mounted () {
|
2023-05-21 17:34:47 +00:00
|
|
|
window.onbeforeunload = () => {
|
|
|
|
if (this.isDirty()) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
this.closeAlert()
|
|
|
|
if (!this.form) {
|
|
|
|
loadForms()
|
|
|
|
} else {
|
|
|
|
this.updatedForm = new Form(this.form)
|
2023-05-21 17:34:47 +00:00
|
|
|
this.formInitialHash = this.hashString(JSON.stringify(this.updatedForm.data()))
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
2023-10-08 08:28:46 +00:00
|
|
|
|
2023-11-28 10:22:05 +00:00
|
|
|
if (this.updatedForm && (!this.updatedForm.notification_settings || Array.isArray(this.updatedForm.notification_settings))) {
|
2023-10-08 08:28:46 +00:00
|
|
|
this.updatedForm.notification_settings = {}
|
|
|
|
}
|
2022-09-20 19:59:52 +00:00
|
|
|
},
|
2023-10-14 16:24:44 +00:00
|
|
|
|
2022-09-20 19:59:52 +00:00
|
|
|
methods: {
|
2023-05-21 17:34:47 +00:00
|
|
|
isDirty () {
|
|
|
|
return !this.loading && this.formInitialHash && this.formInitialHash !== this.hashString(JSON.stringify(this.updatedForm.data()))
|
|
|
|
},
|
|
|
|
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)
|
2022-09-20 19:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|