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

69 lines
1.8 KiB
JavaScript
Raw Normal View History

import { defineStore } from "pinia"
import { useContentStore } from "~/composables/stores/useContentStore.js"
2023-12-19 15:24:54 +01:00
import templateTypes from "~/data/forms/templates/types.json"
import industryTypes from "~/data/forms/templates/industries.json"
2023-12-09 15:47:03 +01:00
const templatesEndpoint = "templates"
export const useTemplatesStore = defineStore("templates", () => {
const contentStore = useContentStore("slug")
2023-12-09 15:47:03 +01:00
const allLoaded = ref(false)
const industries = ref(new Map())
const types = ref(new Map())
2023-12-19 15:24:54 +01:00
const getTemplateTypes = (slugs) => {
2024-01-30 12:32:33 +01:00
if (!slugs) return []
return slugs
.map((slug) => {
return types.value.get(slug)
})
.filter((item) => item !== undefined)
2023-12-19 15:24:54 +01:00
}
2023-12-19 17:10:07 +01:00
const getTemplateIndustries = (slugs) => {
2024-01-30 12:32:33 +01:00
if (!slugs) return []
return slugs
.map((slug) => {
return industries.value.get(slug)
})
.filter((item) => item !== undefined)
2023-12-19 15:24:54 +01:00
}
const initTypesAndIndustries = () => {
if (types.value.size === 0) {
types.value = new Map(Object.entries(templateTypes))
}
if (industries.value.size === 0) {
industries.value = new Map(Object.entries(industryTypes))
2023-12-09 15:47:03 +01:00
}
}
return {
...contentStore,
industries,
types,
2023-12-19 18:57:31 +01:00
allLoaded,
getTemplateTypes,
getTemplateIndustries,
initTypesAndIndustries,
}
2023-12-09 15:47:03 +01:00
})
2023-12-19 18:57:31 +01:00
export const fetchTemplate = (slug, options = {}) => {
return useOpnApi(templatesEndpoint + "/" + slug, options)
}
2023-12-19 18:57:31 +01:00
export const fetchAllTemplates = (options = {}) => {
return useOpnApi(templatesEndpoint, options)
}
export const loadAllTemplates = async (store, options = {}) => {
2023-12-19 17:10:07 +01:00
if (!store.allLoaded) {
store.startLoading()
store.initTypesAndIndustries()
const { data } = await fetchAllTemplates(options)
2023-12-19 17:10:07 +01:00
store.set(data.value)
2023-12-19 18:57:31 +01:00
store.stopLoading()
2023-12-19 17:10:07 +01:00
store.allLoaded = true
}
2023-12-19 15:24:54 +01:00
}