2024-05-30 18:06:50 +07:00
|
|
|
import { defineStore, acceptHMRUpdate } from "pinia";
|
2024-06-03 21:53:23 +07:00
|
|
|
import { useLocalStorage } from "@vueuse/core";
|
2024-05-30 18:06:50 +07:00
|
|
|
export const useDynamicPageStore = defineStore("dynamicPageStore", () => {
|
|
|
|
|
const currentPage = ref<any>({});
|
|
|
|
|
const sectionPublished = ref<any[]>([]);
|
|
|
|
|
const componentPublished = ref<any[]>([]);
|
|
|
|
|
|
|
|
|
|
const setSectionPublished = () => {
|
2024-06-19 16:13:42 +07:00
|
|
|
const exsitsTemplate = ['None']
|
2024-06-21 09:56:34 +07:00
|
|
|
const contentArr: any = [];
|
|
|
|
|
currentPage.value.sections && currentPage.value.sections.map((section: any) => {
|
|
|
|
|
contentArr.push(section.content && typeof section.content === 'string' && JSON.parse(section.content));
|
|
|
|
|
return section;
|
|
|
|
|
});
|
2024-05-31 15:31:05 +07:00
|
|
|
|
2024-06-21 09:56:34 +07:00
|
|
|
sectionPublished.value = currentPage.value.sections && currentPage.value.sections.filter(
|
|
|
|
|
(section: any) => !exsitsTemplate.includes(section.settings?.template) && section.isPublished && !contentArr.flat().some((_section: any) => _section && _section.data && _section.type === "section" && section.id === _section.data)
|
|
|
|
|
).sort((a: any, b: any) => a.order - b.order);
|
2024-05-30 18:06:50 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setComponentPublished = () => {
|
2024-06-21 09:56:34 +07:00
|
|
|
const contentArr: any = [];
|
|
|
|
|
currentPage.value.sections && currentPage.value.sections.map((section: any) => {
|
|
|
|
|
contentArr.push(section.content && JSON.parse(section.content) && JSON.parse(section.content));
|
|
|
|
|
return section;
|
|
|
|
|
});
|
|
|
|
|
componentPublished.value = currentPage.value.components && currentPage.value.components.filter((section: any) => section.isPublished);
|
|
|
|
|
};
|
2024-05-30 18:06:50 +07:00
|
|
|
|
|
|
|
|
const setDataQuery = (query: any, componentId: number | string) => {
|
|
|
|
|
for (const _component of currentPage.value.components && currentPage.value.components) {
|
|
|
|
|
if (_component.id === componentId) {
|
|
|
|
|
const currentSetting = {
|
|
|
|
|
..._component.settings,
|
|
|
|
|
dataQuery: query,
|
|
|
|
|
};
|
|
|
|
|
_component.settings = {
|
|
|
|
|
...currentSetting,
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setComponentPublished();
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-24 16:46:59 +07:00
|
|
|
async function fetchPageByCode(slug: any) {
|
|
|
|
|
try {
|
|
|
|
|
const page = await $fetch(`/api/dynamic-page/get-by-code/${slug}`)
|
|
|
|
|
currentPage.value = {}
|
|
|
|
|
currentPage.value = page
|
|
|
|
|
|
|
|
|
|
setSectionPublished();
|
|
|
|
|
setComponentPublished();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
currentPage,
|
|
|
|
|
sectionPublished,
|
|
|
|
|
componentPublished
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 14:51:25 +07:00
|
|
|
async function getOverviewPageComponentById(componentId, dataQuery) {
|
|
|
|
|
try {
|
|
|
|
|
const { apiUrl } = useRuntimeConfig().public
|
|
|
|
|
const res = await $fetch(`${apiUrl}/cms/page-component/overview-page-component/${componentId}`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: dataQuery,
|
|
|
|
|
});
|
|
|
|
|
console.log("res",res)
|
|
|
|
|
return res
|
|
|
|
|
} catch (err) {
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-30 18:06:50 +07:00
|
|
|
return {
|
|
|
|
|
currentPage,
|
|
|
|
|
sectionPublished,
|
|
|
|
|
componentPublished,
|
|
|
|
|
|
|
|
|
|
fetchPageByCode,
|
|
|
|
|
setSectionPublished,
|
|
|
|
|
setComponentPublished,
|
|
|
|
|
setDataQuery,
|
2024-07-05 14:51:25 +07:00
|
|
|
getOverviewPageComponentById
|
2024-05-30 18:06:50 +07:00
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (import.meta.hot) {
|
|
|
|
|
import.meta.hot.accept(acceptHMRUpdate(useDynamicPageStore, import.meta.hot));
|
|
|
|
|
}
|