import { ICategory } from '@/database/baseTypes'; import { CaseStudiesInterface } from '@/database/case-studies/caseStudies.types'; import { CaseStudiesGateway } from '@/database/case-studies/caseStudies.gateway'; import { defineStore } from 'pinia'; import { ref, Ref } from 'vue'; import isClientOr from '@/utils/static-helpers/storeHelper'; import uniqBy from 'lodash/uniqBy'; interface Category extends ICategory { itemsCount: number; } interface State { post: Ref; postsPreview: Ref; categories: Ref; industries: Ref; totalPostsCount: Ref; currentCategoryTotalPostsCount: Ref; } export const useCaseStudiesStore = defineStore('caseStudies', () => { const state: State = { post: ref({} as CaseStudiesInterface.Post), postsPreview: ref([]), categories: ref([]), industries: ref([]), totalPostsCount: ref(0), currentCategoryTotalPostsCount: ref(0), }; const actions = { async getPost(params: CaseStudiesInterface.GetPostsParams) { const isEmpty = !Object.keys(state.post.value).length; if (true) { state.post.value = await CaseStudiesGateway.getPost(params); } }, async getPostsPreview(params: CaseStudiesInterface.GetPostsParams) { if (true) { state.postsPreview.value = await CaseStudiesGateway.getPostsPreview( params, ); } }, async getMorePostsPreview(params: CaseStudiesInterface.GetPostsParams) { state.postsPreview.value.push( ...(await CaseStudiesGateway.getPostsPreview(params)), ); }, async getTotalPostsCount() { if (isClientOr(!state.totalPostsCount.value)) { state.totalPostsCount.value = await CaseStudiesGateway.getPostsCount( {}, ); } }, async getCurrentCategoryTotalPostsCount( params: CaseStudiesInterface.GetPostsParams, ) { if (true) { state.currentCategoryTotalPostsCount.value = await CaseStudiesGateway.getPostsCount(params); } }, async getCategories() { if (!state.categories.value.length) { const categories = await CaseStudiesGateway.getCategories(); state.categories.value = []; for await (const category of categories) { state.categories.value.push({ ...category, itemsCount: await CaseStudiesGateway.getPostsCount({ categorySlug: category.slug, }), }); } } }, async getIndustries(categorySlug?: string) { if (true) { if (!categorySlug) { state.industries.value = await CaseStudiesGateway.getIndustries(); } else { const filteredPosts = await CaseStudiesGateway.getPostsPreview({ fields: 'industry.*', categorySlug, }); const filteredIndustries = filteredPosts.map(post => post.industry); state.industries.value = uniqBy(filteredIndustries, 'slug'); } } }, }; return { ...state, ...actions, }; });