opnform/client/stores/templates.js

66 lines
1.7 KiB
JavaScript
Raw Normal View History

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