opnform/client/stores/templates.js

119 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-12-16 18:21:03 +00:00
import {defineStore} from 'pinia'
2023-12-09 14:47:03 +00:00
2023-12-16 18:21:03 +00:00
export const templatesEndpoint = '/templates'
2023-12-09 14:47:03 +00:00
export const useTemplatesStore = defineStore('templates', {
state: () => ({
content: [],
industries: {},
types: {},
allLoaded: false,
loading: false
}),
getters: {
getBySlug: (state) => (slug) => {
if (state.content.length === 0) return null
return state.content.find(item => item.slug === slug)
},
getTemplateTypes: (state) => (slugs) => {
if (state.types.length === 0) return null
2023-12-16 18:21:03 +00:00
return Object.values(state.types).filter((val) => slugs.includes(val.slug)).map((item) => {
return item.name
})
2023-12-09 14:47:03 +00:00
},
getTemplateIndustries: (state) => (slugs) => {
if (state.industries.length === 0) return null
2023-12-16 18:21:03 +00:00
return Object.values(state.industries).filter((val) => slugs.includes(val.slug)).map((item) => {
return item.name
})
2023-12-09 14:47:03 +00:00
}
},
actions: {
2023-12-16 18:21:03 +00:00
set(items) {
2023-12-09 14:47:03 +00:00
this.content = items
this.allLoaded = true
},
2023-12-16 18:21:03 +00:00
append(items) {
const ids = items.map((item) => {
return item.id
})
2023-12-09 14:47:03 +00:00
this.content = this.content.filter((val) => !ids.includes(val.id))
this.content = this.content.concat(items)
},
2023-12-16 18:21:03 +00:00
addOrUpdate(item) {
2023-12-09 14:47:03 +00:00
this.content = this.content.filter((val) => val.id !== item.id)
this.content.push(item)
},
2023-12-16 18:21:03 +00:00
remove(item) {
2023-12-09 14:47:03 +00:00
this.content = this.content.filter((val) => val.id !== item.id)
},
2023-12-16 18:21:03 +00:00
startLoading() {
2023-12-09 14:47:03 +00:00
this.loading = true
},
2023-12-16 18:21:03 +00:00
stopLoading() {
2023-12-09 14:47:03 +00:00
this.loading = false
},
2023-12-16 18:21:03 +00:00
setAllLoaded(val) {
2023-12-09 14:47:03 +00:00
this.allLoaded = val
},
2023-12-16 18:21:03 +00:00
resetState() {
2023-12-09 14:47:03 +00:00
this.set([])
this.stopLoading()
},
2023-12-16 18:21:03 +00:00
async loadTypesAndIndustries() {
if (Object.keys(this.industries).length === 0 || Object.keys(this.types).length === 0) {
const files = import.meta.glob('~/data/forms/templates/*.json')
this.industries = await files['/data/forms/templates/industries.json']()
this.types = await files['/data/forms/templates/types.json']()
2023-12-09 14:47:03 +00:00
}
},
2023-12-16 18:21:03 +00:00
loadTemplate(slug) {
2023-12-09 14:47:03 +00:00
this.startLoading()
this.loadTypesAndIndustries()
if (this.getBySlug(slug)) {
this.stopLoading()
return Promise.resolve()
}
2023-12-16 18:21:03 +00:00
return useOpnApi(templatesEndpoint + '/' + slug).then(({data, error}) => {
this.addOrUpdate(data.value)
2023-12-09 14:47:03 +00:00
this.stopLoading()
}).catch((error) => {
this.stopLoading()
})
},
2023-12-16 18:21:03 +00:00
loadAll(options = null) {
2023-12-09 14:47:03 +00:00
this.startLoading()
this.loadTypesAndIndustries()
// Prepare with options
let queryStr = ''
2023-12-16 18:21:03 +00:00
if (options !== null) {
2023-12-09 14:47:03 +00:00
for (const [key, value] of Object.entries(options)) {
queryStr += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(value)
}
queryStr = queryStr.slice(1)
}
2023-12-16 18:21:03 +00:00
return useOpnApi((queryStr) ? templatesEndpoint + '?' + queryStr : templatesEndpoint).then(({data, error}) => {
if (options !== null) {
this.set(data.value)
2023-12-09 14:47:03 +00:00
this.setAllLoaded(false)
} else {
2023-12-16 18:21:03 +00:00
this.append(data.value)
2023-12-09 14:47:03 +00:00
this.setAllLoaded(true)
}
this.stopLoading()
}).catch((error) => {
this.stopLoading()
})
},
2023-12-16 18:21:03 +00:00
loadIfEmpty() {
2023-12-09 14:47:03 +00:00
if (!this.allLoaded) {
return this.loadAll()
}
this.stopLoading()
return Promise.resolve()
}
}
})