2024-04-15 19:39:03 +02:00
|
|
|
import { defineStore } from "pinia"
|
|
|
|
|
import { useContentStore } from "~/composables/stores/useContentStore.js"
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const formsEndpoint = "/open/workspaces/{workspaceId}/forms"
|
|
|
|
|
export const singleFormEndpoint = "/open/forms/{slug}"
|
2023-12-09 15:47:03 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
export const useFormsStore = defineStore("forms", () => {
|
|
|
|
|
const contentStore = useContentStore("slug")
|
2023-12-20 16:10:32 +01:00
|
|
|
const allLoaded = ref(false)
|
2025-02-11 17:14:44 +01:00
|
|
|
const loadAllRequest = ref(null)
|
2023-12-19 19:42:02 +01:00
|
|
|
|
2023-12-20 18:38:43 +01:00
|
|
|
const loadAll = (workspaceId) => {
|
2025-02-11 17:14:44 +01:00
|
|
|
if (loadAllRequest.value) {
|
|
|
|
|
return loadAllRequest.value
|
|
|
|
|
}
|
|
|
|
|
if (!workspaceId) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-12-19 19:42:02 +01:00
|
|
|
contentStore.startLoading()
|
2025-02-11 17:14:44 +01:00
|
|
|
|
|
|
|
|
loadAllRequest.value = new Promise((resolve, reject) => {
|
|
|
|
|
opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId), { query: { page: 1 } })
|
|
|
|
|
.then(firstResponse => {
|
2023-12-19 19:42:02 +01:00
|
|
|
contentStore.resetState()
|
2025-02-11 17:14:44 +01:00
|
|
|
contentStore.save(firstResponse.data)
|
|
|
|
|
|
|
|
|
|
const lastPage = firstResponse.meta.last_page
|
|
|
|
|
|
|
|
|
|
if (lastPage > 1) {
|
|
|
|
|
// Create an array of promises for remaining pages
|
|
|
|
|
const remainingPages = Array.from({ length: lastPage - 1 }, (_, i) => {
|
|
|
|
|
const page = i + 2 // Start from page 2
|
|
|
|
|
return opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId), { query: { page } })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Fetch all remaining pages in parallel
|
|
|
|
|
return Promise.all(remainingPages)
|
|
|
|
|
}
|
|
|
|
|
return []
|
|
|
|
|
})
|
|
|
|
|
.then(responses => {
|
|
|
|
|
// Save all responses data
|
|
|
|
|
responses.forEach(response => {
|
|
|
|
|
contentStore.save(response.data)
|
|
|
|
|
})
|
|
|
|
|
|
2023-12-20 16:10:32 +01:00
|
|
|
allLoaded.value = true
|
2023-12-19 19:42:02 +01:00
|
|
|
contentStore.stopLoading()
|
2025-02-11 17:14:44 +01:00
|
|
|
loadAllRequest.value = null
|
|
|
|
|
resolve()
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
contentStore.stopLoading()
|
|
|
|
|
loadAllRequest.value = null
|
|
|
|
|
reject(err)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return loadAllRequest.value
|
2023-12-19 19:42:02 +01:00
|
|
|
}
|
2025-02-11 17:14:44 +01:00
|
|
|
|
2024-03-25 16:05:24 +01:00
|
|
|
const loadForm = (slug) => {
|
|
|
|
|
contentStore.startLoading()
|
2024-04-15 19:39:03 +02:00
|
|
|
return opnFetch(singleFormEndpoint.replace("{slug}", slug))
|
|
|
|
|
.then((response) => {
|
2024-03-25 16:05:24 +01:00
|
|
|
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()
|
2024-04-15 19:39:03 +02:00
|
|
|
return opnFetch(
|
|
|
|
|
formsEndpoint.replace("{workspaceId}", workspaceId) + "/" + slug,
|
|
|
|
|
).finally(() => {
|
|
|
|
|
contentStore.stopLoading()
|
|
|
|
|
})
|
2023-12-20 18:38:43 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-24 20:19:59 +01:00
|
|
|
/**
|
|
|
|
|
* Load a form from the public API
|
|
|
|
|
*/
|
|
|
|
|
const publicLoad = (slug) => {
|
|
|
|
|
contentStore.startLoading()
|
2024-04-15 19:39:03 +02:00
|
|
|
return useOpnApi("/forms/" + slug)
|
2023-12-24 20:19:59 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-16 13:27:54 +01:00
|
|
|
const publicFetch = (slug) => {
|
|
|
|
|
contentStore.startLoading()
|
2024-04-15 19:39:03 +02:00
|
|
|
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) {
|
2024-04-15 19:39:03 +02:00
|
|
|
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,
|
2023-12-24 20:19:59 +01:00
|
|
|
publicLoad,
|
2024-01-16 13:27:54 +01:00
|
|
|
publicFetch,
|
2023-12-20 18:38:43 +01:00
|
|
|
loadAll,
|
2024-03-25 16:05:24 +01:00
|
|
|
loadForm,
|
2023-12-24 20:19:59 +01:00
|
|
|
load,
|
2023-12-09 15:47:03 +01:00
|
|
|
}
|
|
|
|
|
})
|