opnform-host-nginx/client/stores/forms.js

97 lines
2.5 KiB
JavaScript
Raw Normal View History

import { defineStore } from "pinia"
import { useContentStore } from "~/composables/stores/useContentStore.js"
2023-12-09 15:47:03 +01:00
export const formsEndpoint = "/open/workspaces/{workspaceId}/forms"
export const singleFormEndpoint = "/open/forms/{slug}"
2023-12-09 15:47:03 +01:00
export const useFormsStore = defineStore("forms", () => {
const contentStore = useContentStore("slug")
2023-12-20 16:10:32 +01:00
const allLoaded = ref(false)
2023-12-19 19:42:02 +01:00
const currentPage = ref(1)
2023-12-20 18:38:43 +01:00
const loadAll = (workspaceId) => {
2023-12-19 19:42:02 +01:00
contentStore.startLoading()
return opnFetch(formsEndpoint.replace("{workspaceId}", workspaceId), {
query: { page: currentPage.value },
})
2023-12-19 19:42:02 +01:00
.then((response) => {
if (currentPage.value === 1) {
contentStore.resetState()
contentStore.save(response.data)
2023-12-09 15:47:03 +01:00
} else {
2023-12-19 19:42:02 +01:00
contentStore.save(response.data)
2023-12-09 15:47:03 +01:00
}
2023-12-19 19:42:02 +01:00
if (currentPage.value < response.meta.last_page) {
currentPage.value++
2023-12-20 18:38:43 +01:00
loadAll(workspaceId)
2023-12-09 15:47:03 +01:00
} else {
2023-12-20 16:10:32 +01:00
allLoaded.value = true
2023-12-19 19:42:02 +01:00
contentStore.stopLoading()
currentPage.value = 1
2023-12-09 15:47:03 +01:00
}
})
.catch((error) => {
2023-12-24 09:51:22 +01:00
contentStore.stopLoading()
currentPage.value = 1
throw error
2023-12-09 15:47:03 +01:00
})
2023-12-19 19:42:02 +01:00
}
const loadForm = (slug) => {
contentStore.startLoading()
return opnFetch(singleFormEndpoint.replace("{slug}", slug))
.then((response) => {
contentStore.save(response)
})
.finally(() => {
contentStore.stopLoading()
})
}
2023-12-19 19:42:02 +01:00
2023-12-20 18:38:43 +01:00
const load = (workspaceId, slug) => {
contentStore.startLoading()
return opnFetch(
formsEndpoint.replace("{workspaceId}", workspaceId) + "/" + slug,
).finally(() => {
contentStore.stopLoading()
})
2023-12-20 18:38:43 +01:00
}
/**
* Load a form from the public API
*/
const publicLoad = (slug) => {
contentStore.startLoading()
return useOpnApi("/forms/" + slug)
}
2024-01-16 13:27:54 +01:00
const publicFetch = (slug) => {
contentStore.startLoading()
return opnFetch("/forms/" + slug)
2024-01-16 13:27:54 +01:00
}
2023-12-20 16:10:32 +01:00
const allTags = computed(() => {
let tags = []
contentStore.getAll.value.forEach((form) => {
if (form.tags && form.tags.length) {
if (typeof form.tags === "string" || form.tags instanceof String) {
tags = tags.concat(form.tags.split(","))
2024-01-16 12:20:05 +01:00
} else if (Array.isArray(form.tags)) {
tags = tags.concat(form.tags)
}
2023-12-20 16:10:32 +01:00
}
})
return [...new Set(tags)]
})
2023-12-19 19:42:02 +01:00
return {
...contentStore,
2023-12-20 16:10:32 +01:00
allLoaded,
allTags,
publicLoad,
2024-01-16 13:27:54 +01:00
publicFetch,
2023-12-20 18:38:43 +01:00
loadAll,
loadForm,
load,
2023-12-09 15:47:03 +01:00
}
})