opnform/client/stores/forms.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-12-14 15:53:05 +00:00
import {defineStore} from 'pinia'
2023-12-19 18:42:02 +00:00
import {useContentStore} from "~/composables/stores/useContentStore.js";
2023-12-09 14:47:03 +00:00
2023-12-14 15:53:05 +00:00
export const formsEndpoint = '/open/workspaces/{workspaceId}/forms'
2023-12-09 14:47:03 +00:00
2023-12-19 18:42:02 +00:00
export const useFormsStore = defineStore('forms', () => {
const contentStore = useContentStore('slug')
2023-12-20 15:10:32 +00:00
const allLoaded = ref(false)
2023-12-19 18:42:02 +00:00
const currentPage = ref(1)
2023-12-20 17:38:43 +00:00
const loadAll = (workspaceId) => {
2023-12-19 18:42:02 +00:00
contentStore.startLoading()
return opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId),{query: {page: currentPage.value}})
.then((response) => {
if (currentPage.value === 1) {
contentStore.resetState()
contentStore.save(response.data)
2023-12-09 14:47:03 +00:00
} else {
2023-12-19 18:42:02 +00:00
contentStore.save(response.data)
2023-12-09 14:47:03 +00:00
}
2023-12-19 18:42:02 +00:00
if (currentPage.value < response.meta.last_page) {
currentPage.value++
2023-12-20 17:38:43 +00:00
loadAll(workspaceId)
2023-12-09 14:47:03 +00:00
} else {
2023-12-20 15:10:32 +00:00
allLoaded.value = true
2023-12-19 18:42:02 +00:00
contentStore.stopLoading()
currentPage.value = 1
2023-12-09 14:47:03 +00:00
}
})
2023-12-19 18:42:02 +00:00
}
2023-12-20 17:38:43 +00:00
const load = (workspaceId, slug) => {
contentStore.startLoading()
return opnFetch(formsEndpoint.replace('{workspaceId}', workspaceId) + '/' + slug)
.then((response) => {
console.log(response.data.value)
})
}
2023-12-20 15:10:32 +00:00
const allTags = computed(() => {
let tags = []
contentStore.getAll.value.forEach((form) => {
if (form.tags && form.tags.length) {
tags = tags.concat(form.tags.split(','))
}
})
return [...new Set(tags)]
})
2023-12-19 18:42:02 +00:00
return {
...contentStore,
2023-12-20 15:10:32 +00:00
allLoaded,
allTags,
2023-12-20 17:38:43 +00:00
loadAll,
2023-12-19 18:42:02 +00:00
load
2023-12-09 14:47:03 +00:00
}
})