|
| 1 | +/** |
| 2 | + * Copyright 2024 OpenBuild |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { useSession } from 'next-auth/react'; |
| 18 | +import Image from 'next/image'; |
| 19 | +import { useRouter } from 'next/navigation'; |
| 20 | +import QuizBannerPic from 'public/images/quiz-banner.png'; |
| 21 | +import { useState } from 'react'; |
| 22 | +import useSWR from 'swr'; |
| 23 | + |
| 24 | +import { Button } from '@/components/Button'; |
| 25 | +import { ArrowUturnLeftIcon } from '@/components/icon/solid'; |
| 26 | +import { HistoryIcon } from '@/components/Icons'; |
| 27 | +import { OViewer } from '@/components/MarkDown'; |
| 28 | +import { fetcher } from '@/utils/request'; |
| 29 | + |
| 30 | +import { useMediaUrl } from '#/state/application/hooks'; |
| 31 | + |
| 32 | +import { CourseListViewWidget } from '../../../course'; |
| 33 | +import QuizLimiterWidget from '../../widgets/quiz-limiter'; |
| 34 | +import RankList from './RankList'; |
| 35 | +import RankListModal from './RankListModal'; |
| 36 | +import RecordListModal from './RecordListModal'; |
| 37 | + |
| 38 | +function QuizDetailView({ quizId }) { |
| 39 | + const mediaUrl = useMediaUrl(); |
| 40 | + const [openChallenge, setOpenChallenge] = useState(false); |
| 41 | + const [openRankList, setOpenRankList] = useState(false); |
| 42 | + const [checkLimit, setCheckLimit] = useState(false); |
| 43 | + const { data } = useSWR(`/ts/v1/quiz/${quizId}/index`, fetcher); |
| 44 | + const { data: coursesList } = useSWR(`v1/learn/course/opencourse?skip=0&take=2&order=default&quiz_bind_id=${quizId}`, fetcher); |
| 45 | + const { status } = useSession(); |
| 46 | + const router = useRouter(); |
| 47 | + |
| 48 | + return ( |
| 49 | + <QuizLimiterWidget |
| 50 | + id={quizId} |
| 51 | + type={data?.limit?.limit_type} |
| 52 | + check={checkLimit} |
| 53 | + quiz |
| 54 | + onReset={() => setCheckLimit(false)} |
| 55 | + > |
| 56 | + <div className="max-md:flex max-md:flex-col max-md:gap-y-4 h-[250px] md:h-[360px] bg-cover bg-center bg-no-repeat relative" style={{ backgroundImage: `url(${QuizBannerPic.src})` }}> |
| 57 | + <div className="md:absolute flex items-center mt-[15px] md:mt-6 mx-4 md:mx-14"> |
| 58 | + <span |
| 59 | + onClick={() => window.history.back()} |
| 60 | + className="transition-all hidden md:flex items-center cursor-pointer text-sm opacity-80 rounded py-2 px-3 border border-gray-1100 text-black mr-2 hover:border-gray"> |
| 61 | + <ArrowUturnLeftIcon className="h-4 w-4 mr-2" />Return |
| 62 | + </span> |
| 63 | + <span onClick={() => { |
| 64 | + if (status !== 'authenticated') { |
| 65 | + router.push(`/signin?from=/quiz/${quizId}`); |
| 66 | + } else { |
| 67 | + setOpenChallenge(true); |
| 68 | + } |
| 69 | + }} className="cursor-pointer transition-all flex text-sm items-center opacity-80 rounded py-2 px-3 border border-gray-1100 text-black hover:border-gray"> |
| 70 | + <HistoryIcon className="mr-2" />Challenge Record |
| 71 | + </span> |
| 72 | + </div> |
| 73 | + <div className="flex items-center justify-center md:pt-9 md:pb-4"> |
| 74 | + {data?.quiz_user?.user_avatar && mediaUrl && ( |
| 75 | + <Image |
| 76 | + className="h-7 w-7 rounded object-cover mr-2" |
| 77 | + height={24} |
| 78 | + width={24} |
| 79 | + alt={'user_avatar'} |
| 80 | + src={mediaUrl + data?.quiz_user?.user_avatar} |
| 81 | + /> |
| 82 | + )} |
| 83 | + <p className="opacity-90 max-md:text-[18px]">by <a href={`/u/${data?.quiz_user?.user_handle}`}><strong>{data?.quiz_user?.user_nick_name}</strong></a></p> |
| 84 | + </div> |
| 85 | + <h1 className="text-[28px] md:text-[42px] leading-[32px] md:leading-[52px] text-center max-md:px-6 md:max-w-[692px] mx-auto">{data?.title}</h1> |
| 86 | + </div> |
| 87 | + <div className="max-w-[800px] mx-auto bg-white rounded-xl p-6 md:px-9 md:pt-10 md:pb-6 relative z-[2] md:top-[-155px]"> |
| 88 | + <h5 className="text-lg mb-4 md:mb-3">Quiz Describe</h5> |
| 89 | + <OViewer value={data?.describe} /> |
| 90 | + <Button |
| 91 | + onClick={() => setCheckLimit(true)} |
| 92 | + className="mt-4 md:mt-6 mb-9 md:mb-10 !font-bold px-[64px] !text-base max-md:w-full"> |
| 93 | + Challenge now |
| 94 | + </Button> |
| 95 | + <RankList rank={data?.my_rank} list={data?.rank}/> |
| 96 | + <p className="text-sm text-center mt-6 cursor-pointer" onClick={()=>{setOpenRankList(true);}}><strong>{data?.user_num}</strong> builders have participated</p> |
| 97 | + </div> |
| 98 | + { |
| 99 | + coursesList?.count > 0 && ( |
| 100 | + <div className="max-w-[800px] max-md:mt-9 mx-6 md:mx-auto relative md:top-[-105px] max-md:pb-14"> |
| 101 | + <h3 className="text-[18px] max-md:leading-[24px] md:text-lg mb-6">Related courses</h3> |
| 102 | + <CourseListViewWidget className="gap-y-6 md:gap-4 md:grid-cols-2" data={coursesList?.list} /> |
| 103 | + </div>) |
| 104 | + } |
| 105 | + <RankListModal quizId={quizId} shown={openRankList} onClose={() => setOpenRankList(false)} rank={data?.my_rank}/> |
| 106 | + <RecordListModal quizId={quizId} shown={openChallenge} onClose={() => setOpenChallenge(false)} /> |
| 107 | + </QuizLimiterWidget> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +export default QuizDetailView; |
0 commit comments