thainv-dev: Nhúng

This commit is contained in:
nguyen van thai
2024-07-06 16:28:45 +07:00
parent a47229f44e
commit ccd92c86ee
4 changed files with 116 additions and 32 deletions
+62 -15
View File
@@ -18,6 +18,7 @@ const { currentPollOptions } = storeToRefs(store.pollOptions);
const { currentPollResponses } = storeToRefs(store.pollResponse); const { currentPollResponses } = storeToRefs(store.pollResponse);
const poll = reactive<Poll | any>({}); const poll = reactive<Poll | any>({});
const options = ref<PollOption[] | any[]>([]); const options = ref<PollOption[] | any[]>([]);
async function loadData() { async function loadData() {
await store.poll.fetchById(String(props.dataId)); await store.poll.fetchById(String(props.dataId));
await store.pollOptions.fetchByPollId(String(props.dataId)); await store.pollOptions.fetchByPollId(String(props.dataId));
@@ -30,13 +31,14 @@ function assignData() {
options.value = currentPollOptions.value; options.value = currentPollOptions.value;
} }
onBeforeMount(async () => {
await loadData(); await loadData();
}); // onBeforeMount(async () => {
// });
const selectedOption = ref<any>(-1); // const selectedOption = ref<any>(-1);
const showResult = ref(false); // const showResult = ref(false);
const alreadyVoted = ref(false); const alreadyVoted = ref(false);
const showResult = ref(false)
const canShowResult = computed(() => { const canShowResult = computed(() => {
switch (poll.settings?.resultPublication) { switch (poll.settings?.resultPublication) {
case 0: case 0:
@@ -49,29 +51,74 @@ const canShowResult = computed(() => {
return alreadyVoted.value && (!poll.endTime || new Date() > new Date(poll.endTime)); return alreadyVoted.value && (!poll.endTime || new Date() > new Date(poll.endTime));
} }
}); });
// const toggleResults = () => {
// if (canShowResult.value) showResult.value = !showResult.value;
// };
const singleSelect = ref<number>(-1)
const multipleSelect = ref<number []>([])
const totalResponses = ref(0); const totalResponses = ref(0);
async function submitVote() { async function submitVote() {
if (selectedOption.value >= 0 && !alreadyVoted.value) { // if (selectedOption.value >= 0 && !alreadyVoted.value) {
const result = await store.pollResponse.create({ optionId: selectedOption.value }); // const result = await store.pollResponse.create({ optionId: selectedOption.value });
if (result?.id) { // 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 === selectedOption.value) {
// option.responseCount = (option.responseCount ?? 0) + 1;
// }
// option.percentage = Number(((option.responseCount / totalResponses.value) * 100).toFixed(1));
// });
// alreadyVoted.value = true;
// }
// }
switch (poll.type) {
case 1:
if(singleSelect.value >= 0) {
totalResponses.value = options.value?.reduce((sum, option) => sum + Number(option.responseCount ?? 0), 1); totalResponses.value = options.value?.reduce((sum, option) => sum + Number(option.responseCount ?? 0), 1);
options?.value?.forEach((option: PollOption | any) => { options?.value?.forEach((option: PollOption | any) => {
if (option.id === selectedOption.value) { if (option.id === singleSelect.value) {
option.responseCount = (option.responseCount ?? 0) + 1; option.responseCount = (option.responseCount ?? 0) + 1;
} }
option.percentage = Number(((option.responseCount / totalResponses.value) * 100).toFixed(1)); option.percentage = Number(((option.responseCount / totalResponses.value) * 100).toFixed(1));
alreadyVoted.value = true
}); });
alreadyVoted.value = true;
} }
} }
} }
const toggleResults = () => {
if (canShowResult.value) showResult.value = !showResult.value;
};
</script> </script>
<template> <template>
<span class="inline-block px-4 py-2 shadow-xl rounded-lg bg-[#f5f5f5]">
<div class="inline-block px-4 py-2 mb-5">
<h3 class="text-#000 font-raleway font-italic text-16px font-500 leading-140% mb-2">{{ poll?.title }}</h3>
<ul class="flex flex-col gap-3">
<template v-if="poll.type === 1">
<li v-for="(option, index) in options" :key="index">
<input :id="`option-${index}`" :disabled="singleSelect >= 0 && alreadyVoted ? true : false" type="radio" class="peer opacity-0" :value="option.id" v-model="singleSelect">
<label :for="`option-${index}`" class="font-raleway text-16px relative text-#000 font-400 leading-140% pl-16px before:content-[''] before:inline-block before:w-14px before:h-14px before:rounded-50% before:border-1 before:absolute before:left--3 before:top-10px before:border-1px before:border-#000 before:translate-y--50% after:absolute after:left--10px after:top-10px after:translate-y--50% after:border-#000 after:content-[''] after:inline-block after:w-10px after:h-10px after:rounded-full peer-checked:after:bg-primary peer-checked:before:border-primary ">{{ option?.title }}</label>
</li>
</template>
</ul>
<div class="flex items-center justify-center mt-3">
<button @click="submitVote()" v-if="!alreadyVoted" :disabled="alreadyVoted && singleSelect >= 0 ? true : false" class="px-5 py-10px rounded-6px bg-primary text-#fff font-raleway text-14px font-400">Gửi kết quả</button>
<button @click="showResult = true" v-if="alreadyVoted" class="px-5 py-10px rounded-6px bg-primary text-#fff font-raleway text-14px font-400">Xem kết quả</button>
</div>
<div class="mt-5" v-if="showResult && canShowResult">
<h3 class="text-#000 font-raleway font-italic text-16px font-500 leading-140% mb-2">{{ poll?.title }}</h3>
<ul>
<li v-for="(answer, index) in options" :key="index" class="flex gap-4 items-center text-#000 font-raleway text-12px leading-140%">
{{ answer.percentage}}%
<div :style="{ width: `${answer.percentage}%` }" :class="answer.id === singleSelect ? 'bg-primary' : 'bg-#AEAEAE'" class="h-1.5 rounded-full"></div>
</li>
</ul>
</div>
</div>
<!-- <span class="inline-block px-4 py-2 shadow-xl rounded-lg bg-[#f5f5f5]">
<span class="block"> <span class="block">
<span class="underline decoration-gray-500 font-bold"> <span class="underline decoration-gray-500 font-bold">
{{ poll?.title }} {{ poll?.title }}
@@ -100,5 +147,5 @@ const toggleResults = () => {
</span> </span>
<b>Tổng số lượt binh chọn: {{ totalResponses }}</b> <b>Tổng số lượt binh chọn: {{ totalResponses }}</b>
</span> </span>
</span> </span> -->
</template> </template>
+16 -6
View File
@@ -246,23 +246,33 @@ async function submitSend() {}
</script> </script>
<template> <template>
<div class="inline-block px-4 py-2 shadow-xl rounded-lg bg-[#f5f5f5] !text-black-500"> <div class="inline-block px-4 py-2 !text-black-500">
<h5 class="underline decoration-gray-500 font-bold mb-2">Câu đố: {{ data?.title }}</h5> <h5 class="underline decoration-gray-500 font-bold mb-2">Câu đố: {{ data?.title }}</h5>
<ul class="px-3"> <ul class="px-3 flex flex-col gap-3">
<li v-for="(question, questionIndex) in data.questionGeneral" :key="questionIndex" class="mb-2"> <li v-for="(question, questionIndex) in data.questionGeneral" :key="questionIndex" class="mb-2">
<h5 class="mb-1 font-700 text-black-500">{{ `${questionIndex + 1}. ${question.title}` }}</h5> <h5 class="text-#000 font-raleway font-italic text-16px font-500 leading-140% mb-2">{{ `${questionIndex + 1}. ${question.title}` }}</h5>
<ul> <ul>
<template v-if="question.type === 1">
<li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="flex items-center gap-1 py-1"> <li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="flex items-center gap-1 py-1">
<input :id="`answer-${questionIndex}-${answerIndex}`" :type="question.type === 1 ? 'radio' : 'checkbox'" :value="answerIndex" v-model="selectQuizAnswer[questionIndex]" /> <input :id="`answer-${questionIndex}-${answerIndex}`" type="radio" :value="answerIndex" class="peer opacity-0" v-model="selectQuizAnswer[questionIndex]" />
<label :for="`answer-${questionIndex}-${answerIndex}`" class="font-semibold">{{ answer.title }}</label> <label :for="`answer-${questionIndex}-${answerIndex}`" class="font-raleway text-16px relative text-#000 font-400 leading-140% pl-16px before:content-[''] before:inline-block before:w-14px before:h-14px before:rounded-50% before:border-1 before:absolute before:left--3 before:top-10px before:border-1px before:border-#000 before:translate-y--50% after:absolute after:left--10px after:top-10px after:translate-y--50% after:border-#000 after:content-[''] after:inline-block after:w-10px after:h-10px after:rounded-full peer-checked:after:bg-primary peer-checked:before:border-primary ">{{ answer.title }}</label>
</li> </li>
</template>
<template v-else>
<li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="flex items-center gap-1 py-1">
<input :id="`answer-${questionIndex}-${answerIndex}`" type="checkbox" :value="answerIndex" class="peer opacity-0" v-model="selectQuizAnswer[questionIndex]" />
<label :for="`answer-${questionIndex}-${answerIndex}`" class="font-raleway text-16px relative text-#000 font-400 leading-140% pl-16px before:content-[''] before:inline-block before:w-14px before:h-14px before:rounded-2px before:border-1 before:absolute before:left--3 before:top-10px before:border-1px before:border-#000 before:translate-y--50% after:absolute after:left--10px after:top-6px after:translate-y--50% after:border-#000 after:content-['✔'] after:text-12px after:text-transparent after:inline-block after:w-14px after:h-14px peer-checked:after:text-white peer-checked:before:border-primary peer-checked:before:bg-primary ">{{ answer.title }}</label>
</li>
</template>
</ul> </ul>
</li> </li>
</ul> </ul>
<button @click="submitSend" class="bg-primary-500 text-white py-1 px-3 rounded-4px cursor-pointer hover:bg-primary-600 float-right">Gửi câu trả lời</button> <div class="flex justify-center">
<button @click="submitSend" class="mx-auto px-5 py-10px rounded-6px bg-primary text-#fff font-raleway text-14px font-400">Gửi câu trả lời</button>
</div>
</div> </div>
<!-- <div> <!-- <div>
<h5 class="text-black text-18px font-700">{{ data?.title }}</h5> <h5 class="text-black text-18px font-700">{{ data?.title }}</h5>
+22 -5
View File
@@ -270,24 +270,41 @@ async function submitSend() {
<template> <template>
<div class="inline-block px-4 py-2 shadow-xl rounded-lg bg-[#f5f5f5] !text-black-500"> <div class="inline-block px-4 py-2 !text-black-500">
<h5 class="underline decoration-gray-500 font-bold mb-2">Khảo sát: {{ dataSurvey?.title }}</h5> <h5 class="underline decoration-gray-500 font-bold mb-2">Khảo sát: {{ dataSurvey?.title }}</h5>
<ul class="px-3"> <ul class="px-3">
<li v-for="(question, questionIndex) in dataSurvey.questionGeneral" :key="questionIndex" class="mb-2"> <li v-for="(question, questionIndex) in dataSurvey.questionGeneral" :key="questionIndex" class="mb-2">
<h5 class="mb-1 font-700 text-black-500">{{ `${questionIndex + 1}. ${question.title}` }}</h5> <h5 class="text-#000 font-raleway font-italic text-16px font-500 leading-140% mb-2">{{ `${questionIndex + 1}. ${question.title}` }}</h5>
<ul> <!-- <ul>
<li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="flex items-center gap-1 py-1"> <li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="flex items-center gap-1 py-1">
<input :id="`answer-survey-${questionIndex}-${answerIndex}`" :type="question.type === 1 ? 'radio' : 'checkbox'" :value="answerIndex" v-model="selectSurveyAnswer[questionIndex]"> <input :id="`answer-survey-${questionIndex}-${answerIndex}`" :type="question.type === 1 ? 'radio' : 'checkbox'" :value="answerIndex" v-model="selectSurveyAnswer[questionIndex]">
<label :for="`answer-survey-${questionIndex}-${answerIndex}`" class="font-semibold">{{ answer.title }}</label> <label :for="`answer-survey-${questionIndex}-${answerIndex}`" class="font-semibold">{{ answer.title }}</label>
</li> </li>
</ul> -->
<ul>
<template v-if="question.type === 1">
<li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="flex items-center gap-1 py-1">
<input :id="`answer-survey-${questionIndex}-${answerIndex}`" type="radio" :value="answerIndex" class="peer opacity-0" v-model="selectSurveyAnswer[questionIndex]" />
<label :for="`answer-survey-${questionIndex}-${answerIndex}`" class="font-raleway text-16px relative text-#000 font-400 leading-140% pl-16px before:content-[''] before:inline-block before:w-14px before:h-14px before:rounded-50% before:border-1 before:absolute before:left--3 before:top-10px before:border-1px before:border-#000 before:translate-y--50% after:absolute after:left--10px after:top-10px after:translate-y--50% after:border-#000 after:content-[''] after:inline-block after:w-10px after:h-10px after:rounded-full peer-checked:after:bg-primary peer-checked:before:border-primary ">{{ answer.title }}</label>
</li>
</template>
<template v-else>
<li v-for="(answer, answerIndex) in question.answers" :key="answerIndex" class="flex items-center gap-1 py-1">
<input :id="`answer-survey-${questionIndex}-${answerIndex}`" type="checkbox" :value="answerIndex" class="peer opacity-0" v-model="selectSurveyAnswer[questionIndex]" />
<label :for="`answer-survey-${questionIndex}-${answerIndex}`" class="font-raleway text-16px relative text-#000 font-400 leading-140% pl-16px before:content-[''] before:inline-block before:w-14px before:h-14px before:rounded-2px before:border-1 before:absolute before:left--3 before:top-10px before:border-1px before:border-#000 before:translate-y--50% after:absolute after:left--10px after:top-6px after:translate-y--50% after:border-#000 after:content-['✔'] after:text-12px after:text-transparent after:inline-block after:w-14px after:h-14px peer-checked:after:text-white peer-checked:before:border-primary peer-checked:before:bg-primary ">{{ answer.title }}</label>
</li>
</template>
</ul> </ul>
</li> </li>
</ul> </ul>
<button @click="submitSend" class="bg-primary-500 text-white py-1 px-3 rounded-4px cursor-pointer hover:bg-primary-600 float-right">Gửi câu trả lời</button> <div class="flex justify-center">
<button @click="submitSend" class="mx-auto px-5 py-10px rounded-6px bg-primary text-#fff font-raleway text-14px font-400">Gửi câu trả lời</button>
</div>
<!-- <button @click="submitSend" class="bg-primary-500 text-white py-1 px-3 rounded-4px cursor-pointer hover:bg-primary-600 float-right">Gửi câu trả lời</button> -->
</div> </div>
</template> </template>
@@ -1,4 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import Poll from "~/components/article/immerse/Poll.vue";
import Quiz from "~/components/article/immerse/Quiz.vue";
import Survey from "~/components/article/immerse/Survey.vue";
import Document from "~/components/article/immerse/Document.vue";
import Attachment from "@/components/article/immerse/Attachment.vue";
import Tag from "@/components/article/immerse/Tag.vue";
// import articlerelation from "@/components/article/immerse/ArticleRelation.vue";
import RecusiveSection from "@/components/dynamic-page/page-section/RecusiveSection.vue"; import RecusiveSection from "@/components/dynamic-page/page-section/RecusiveSection.vue";
import { getInputValue } from "@/utils/parseSQL"; import { getInputValue } from "@/utils/parseSQL";
import { enumPageSectionLayouts, enumPageSectionTemplate, enumPageSectionKey } from "~/definitions/enum"; import { enumPageSectionLayouts, enumPageSectionTemplate, enumPageSectionKey } from "~/definitions/enum";
@@ -92,6 +99,8 @@ function findElementPathById(categories: any[], targetId: number, path: any[] =
} }
return null; return null;
} }
console.log(currentArticle.value ,'currentArticle')
</script> </script>
<template> <template>
@@ -296,7 +305,8 @@ function findElementPathById(categories: any[], targetId: number, path: any[] =
<p class="my-10px" v-if="currentArticle.intro" v-html="currentArticle.intro"> <p class="my-10px" v-if="currentArticle.intro" v-html="currentArticle.intro">
</p> </p>
<div v-html="currentArticle.detail" class="[&_p_>_span]:!font-raleway [&_p]:mb-10px"></div> <!-- <div v-html="currentArticle.detail" class="[&_p_>_span]:!font-raleway [&_p]:mb-10px"></div> -->
<component :is="{ template: currentArticle.detail, components: { Poll, Quiz, Survey, Document, Attachment, Tag } }" />
</div> </div>
</div> </div>
<div class="content__bottom__right"> <div class="content__bottom__right">