2024-04-15 19:39:03 +02:00
|
|
|
import { hash } from "~/lib/utils.js"
|
|
|
|
|
import { useStorage } from "@vueuse/core"
|
2023-12-24 20:19:59 +01:00
|
|
|
|
|
|
|
|
export const pendingSubmission = (form) => {
|
|
|
|
|
const formPendingSubmissionKey = computed(() => {
|
2024-04-15 19:39:03 +02:00
|
|
|
return form
|
|
|
|
|
? form.form_pending_submission_key + "-" + hash(window.location.href)
|
|
|
|
|
: ""
|
2023-12-24 20:19:59 +01:00
|
|
|
})
|
2024-09-18 19:20:52 +02:00
|
|
|
const formPendingSubmissionTimerKey = computed(() => {
|
|
|
|
|
return formPendingSubmissionKey.value + "-timer"
|
|
|
|
|
})
|
2023-12-24 20:19:59 +01:00
|
|
|
|
|
|
|
|
const enabled = computed(() => {
|
|
|
|
|
return form?.auto_save ?? false
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const set = (value) => {
|
2024-03-28 17:59:41 +01:00
|
|
|
if (import.meta.server || !enabled.value) return
|
2024-04-15 19:39:03 +02:00
|
|
|
useStorage(formPendingSubmissionKey.value).value =
|
|
|
|
|
value === null ? value : JSON.stringify(value)
|
2023-12-24 20:19:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const remove = () => {
|
|
|
|
|
return set(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const get = (defaultValue = {}) => {
|
2024-03-28 17:59:41 +01:00
|
|
|
if (import.meta.server || !enabled.value) return
|
2023-12-24 20:19:59 +01:00
|
|
|
const pendingSubmission = useStorage(formPendingSubmissionKey.value).value
|
|
|
|
|
return pendingSubmission ? JSON.parse(pendingSubmission) : defaultValue
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-28 17:33:55 +02:00
|
|
|
const setSubmissionHash = (hash) => {
|
|
|
|
|
set({
|
|
|
|
|
...get(),
|
|
|
|
|
submission_hash: hash
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getSubmissionHash = () => {
|
|
|
|
|
return get()?.submission_hash ?? null
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 19:20:52 +02:00
|
|
|
const setTimer = (value) => {
|
|
|
|
|
if (import.meta.server) return
|
|
|
|
|
useStorage(formPendingSubmissionTimerKey.value).value = value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const removeTimer = () => {
|
|
|
|
|
return setTimer(null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getTimer = (defaultValue = null) => {
|
|
|
|
|
if (import.meta.server) return
|
|
|
|
|
return useStorage(formPendingSubmissionTimerKey.value).value ?? defaultValue
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-24 20:19:59 +01:00
|
|
|
return {
|
2025-04-28 17:33:55 +02:00
|
|
|
formPendingSubmissionKey,
|
2023-12-24 20:19:59 +01:00
|
|
|
enabled,
|
|
|
|
|
set,
|
2024-02-03 12:50:57 +01:00
|
|
|
get,
|
2024-04-15 19:39:03 +02:00
|
|
|
remove,
|
2025-04-28 17:33:55 +02:00
|
|
|
setSubmissionHash,
|
|
|
|
|
getSubmissionHash,
|
2024-09-18 19:20:52 +02:00
|
|
|
setTimer,
|
|
|
|
|
removeTimer,
|
|
|
|
|
getTimer,
|
2023-12-24 20:19:59 +01:00
|
|
|
}
|
|
|
|
|
}
|