From 5f9525371d739c169ea1ee200fa870d104c204a5 Mon Sep 17 00:00:00 2001 From: nguyen van thai Date: Tue, 16 Jul 2024 22:54:15 +0700 Subject: [PATCH] Poll --- components/article/immerse/Poll.vue | 21 ++++++++++++--------- nuxt.config.ts | 2 +- pages/bai-viet/[slug].vue | 4 ++-- pages/index.vue | 4 ++-- server/models/poll-option.ts | 5 +++-- server/models/poll-response.ts | 8 ++++---- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/components/article/immerse/Poll.vue b/components/article/immerse/Poll.vue index c350278..01943d7 100644 --- a/components/article/immerse/Poll.vue +++ b/components/article/immerse/Poll.vue @@ -15,14 +15,14 @@ const store = reactive({ }); const { currentPoll } = storeToRefs(store.poll); const { currentPollOptions } = storeToRefs(store.pollOptions); -const { currentPollResponses } = storeToRefs(store.pollResponse); +// const { currentPollResponses } = storeToRefs(store.pollResponse); const poll = reactive({}); const options = ref([]); async function loadData() { await store.poll.fetchById(String(props.dataId)); await store.pollOptions.fetchByPollId(String(props.dataId)); - await store.pollResponse.fetchByPollId(String(props.dataId)); + // await store.pollResponse.fetchByPollId(String(props.dataId)); assignData(); } @@ -78,14 +78,17 @@ async function submitVote() { switch (poll.type) { case 1: if(singleSelect.value >= 0) { - totalResponses.value = options.value?.reduce((sum, option) => sum + Number(option.responseCount ?? 0), 1); - options?.value?.forEach((option: PollOption | any) => { - if (option.id === singleSelect.value) { - option.responseCount = (option.responseCount ?? 0) + 1; + const result = await store.pollResponse.create({ optionId: singleSelect.value }) + if(result?.id) { + totalResponses.value = options.value?.reduce((sum, option) => sum + Number(option.responseCount ?? 0), 1); + options?.value?.forEach((option: PollOption | any) => { + if (option.id === singleSelect.value) { + option.responseCount = (option.responseCount ?? 0) + 1; + } + option.percentage = Number(((option.responseCount / totalResponses.value) * 100).toFixed(1)); + alreadyVoted.value = true + }); } - option.percentage = Number(((option.responseCount / totalResponses.value) * 100).toFixed(1)); - alreadyVoted.value = true - }); } } } diff --git a/nuxt.config.ts b/nuxt.config.ts index 497a066..ecc29b6 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -32,7 +32,7 @@ export default defineNuxtConfig({ runtimeConfig: { public: { - apiUrl: "http://uat-api-portal.vpress.vn/api-v1", + apiUrl: "http://api-portal.vpress.vn/api-v1", site: process.env.NUXT_PUBLIC_SITE_DEFAULT || "1", }, authSecret: process.env.AUTH_SECRET||"vpress" diff --git a/pages/bai-viet/[slug].vue b/pages/bai-viet/[slug].vue index b39b782..a52f322 100644 --- a/pages/bai-viet/[slug].vue +++ b/pages/bai-viet/[slug].vue @@ -9,7 +9,7 @@ import DynamicTemplate from "~/components/dynamic-page/page/templates/index.vue" import DynamicSection from "~/components/dynamic-page/page-section/templates/index.vue"; const route = useRoute(); - +const site = useCookie("site") const store = reactive({ dynamicPage: useDynamicPageStore(), article: useArticleStore(), @@ -20,7 +20,7 @@ import { useArticleStore } from '~/stores/articles'; const loadPage = async () => { const article = await store.article.getArticleBySlug(String(route.params.slug)); -console.log(article?.value, 'article') + site.value = article?.value.siteId let isContentType switch (article?.value?.contentType) { case 1: diff --git a/pages/index.vue b/pages/index.vue index 6eed51c..6d0acfe 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -11,12 +11,12 @@ const store = reactive({ }); //lấy danh sách categoryTree await store.category.fetchBySiteId() - +const site = useCookie('site') const { data } = await useAsyncData('index', () => store.dynamicPage.fetchPageByCode(route.path === '/' ? 'trang-chu' : route.path.replace('/', ''))) const asycnCurrentPage = data.value && data.value.currentPage; const asycnSectionPublished = data.value && data.value.sectionPublished; const asycnComponentPublished = data.value && data.value.componentPublished; - +site.value = data.value?.currentPage?.siteId useHead({ title: () => 'Trang chủ', description: () => 'Với công nghệ đột phá và giải pháp sáng tạo, Vpress sẽ là đối tác tin cậy của các tòa soạn báo, cùng nhau kiến tạo nên những giá trị bền vững trong kỷ nguyên chuyển đổi số báo chí.', diff --git a/server/models/poll-option.ts b/server/models/poll-option.ts index 386d31a..10d3414 100644 --- a/server/models/poll-option.ts +++ b/server/models/poll-option.ts @@ -1,5 +1,6 @@ import { H3Event } from 'h3'; import Base from './base' +import { useCookie } from 'nuxt/app'; export type PollOption = { id?: number; // Mã định danh @@ -22,8 +23,8 @@ export type PollOption = { const { items }: PollOption[] | any = await $fetch(`${apiUrl}/cms/poll-option/poll:${pollId}`, { method: 'GET', headers: { - site: 1 - } + site: Number(useCookie('site').value) + }, }) return items diff --git a/server/models/poll-response.ts b/server/models/poll-response.ts index 98a4755..70c4782 100644 --- a/server/models/poll-response.ts +++ b/server/models/poll-response.ts @@ -1,5 +1,6 @@ import { H3Event } from 'h3'; import Base from './base' +import { useCookie } from 'nuxt/app'; export type PollResponse = { id?: number; // Mã định danh @@ -18,7 +19,7 @@ export const create = async (event: H3Event) => { const { item }: any = await $fetch(`${apiUrl}/cms/poll-response`, { method: 'POST', headers: { - site: 1 + site: Number(useCookie('site').value) }, body: payload }) @@ -32,14 +33,13 @@ export const create = async (event: H3Event) => { export const fetchByPollId = async (event: H3Event) => { try { - const { apiUrl } = useRuntimeConfig().public const { pollId }: any = getQuery(event) const { items }: PollResponse[] | any = await $fetch(`${apiUrl}/cms/poll-response/poll:${pollId}`, { method: 'GET', headers: { - site: 1 - } + site: Number(useCookie('site').value) + }, }) return items