diff --git a/src/pages/lesson/lesson.tsx b/src/pages/lesson/lesson.tsx index ef9cdd0..86d6707 100644 --- a/src/pages/lesson/lesson.tsx +++ b/src/pages/lesson/lesson.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import moment from 'moment'; import { Link as ReactRouterLink, useHistory } from 'react-router-dom'; import { @@ -16,6 +16,7 @@ import { Icon, IconButton, useToast, + useColorModeValue, } from '@chakra-ui/react'; /** @@ -30,7 +31,7 @@ import { Redirect, useParams } from 'react-router-dom'; import { PageContent, PageHeading, PageWrapper } from '@educt/components/PageElements'; import { useFetchLesson } from '@educt/hooks/queries/lesson/useFetchLesson'; import LoadingPage from '@educt/components/LoadingPage'; -import { MdCheckCircle, MdOutlineCircle, MdOutlineFilePresent, MdSkipNext, MdSkipPrevious } from 'react-icons/md'; +import { MdOutlineCircle, MdOutlineFilePresent, MdSkipNext, MdSkipPrevious } from 'react-icons/md'; import { useRootStore } from '@educt/hooks/useRootStore'; import { Helmet } from 'react-helmet'; import { useFetchCourseLessons } from '@educt/hooks/queries/course/useFetchCourseLessons'; @@ -39,28 +40,36 @@ import { useFetchCourseLessons } from '@educt/hooks/queries/course/useFetchCours * Lesson Page */ const LessonPage: React.FC = () => { + const { + userStore: { me }, + } = useRootStore(); const { id } = useParams<{ id: string }>(); const { error, data: lesson, isLoading } = useFetchLesson(id); const { fetchCourseLessons, data: lessons } = useFetchCourseLessons(); const toast = useToast(); const history = useHistory(); - const { - userStore: { me }, - } = useRootStore(); const [nextLesson, setNextLesson] = useState(null); const [prevLesson, setPrevLesson] = useState(null); const [isPageAvailable, setIsPageAvailable] = useState(true); + const currentLessonRef = useRef(null); + const bg = useColorModeValue('gray.50', 'gray.700'); useEffect(() => { /** * Сheck if the user has access to the lesson */ - const isHasAccess = me?.courses?.some(course => course.id === lesson?.course_id) ?? true; + if (me === null || lesson === null) return; + const isHasAccess = me.courses?.some(course => course.id === lesson.course_id) ?? true; if (!isHasAccess) { toast({ title: 'You are not able to view this lesson.', status: 'warning' }); setIsPageAvailable(false); } + }, []); + + useEffect(() => { + currentLessonRef.current?.scrollIntoView({ block: 'center' }); + window.scrollTo(0, 0); /** * Define next and prev lessons @@ -104,7 +113,7 @@ const LessonPage: React.FC = () => { - + @@ -133,19 +142,20 @@ const LessonPage: React.FC = () => { - + {lessons.map((lesson, i) => ( @@ -169,7 +179,7 @@ const LessonPage: React.FC = () => { {[1, 2, 3].map(n => ( - + lesson-1_sourse-code.zip