import React from 'react'; import { runInAction } from 'mobx'; import moment from 'moment'; import * as helpers from '@educt/helpers'; import { Link as ReactRouterLink } from 'react-router-dom'; import { Box, BoxProps, Flex, FlexProps, Button, Icon, Image, Text, Heading, Tag, Stack, ImageProps, LinkBox, LinkOverlay, LinkBoxProps, StackProps, } from '@chakra-ui/react'; import { MdThumbUpOffAlt, MdPlayCircleOutline, MdOutlineGroup, MdOutlineVideoLibrary, MdOutlineLock, } from 'react-icons/md'; /** * Types */ import type { ICourse } from '@educt/interfaces'; /** * Hooks */ import { useState } from 'react'; import { useColorModeValue } from '@chakra-ui/react'; import { useRootStore } from '@educt/hooks/useRootStore'; import { CourseServiceInstance } from '@educt/services'; export const CourseHeader: React.FC = props => ( {props.children} ); interface ICourseHeadingProps extends BoxProps { title: string; teacher: ICourse['teacher']; category: ICourse['category']; } export const CourseHeading: React.FC = props => { const { title, teacher, category, ...boxProps } = props; return ( {title} {teacher.fullname} •  {category.title} ); }; interface ICourseInfoProps extends BoxProps { id: string; lessonsCount: string; studentsCount: string; likesCount: string; } export const CourseInfo: React.FC = props => { const { userStore } = useRootStore(); const { me } = userStore; const { id, lessonsCount, studentsCount, likesCount, ...boxProps } = props; const [isLoading, setIsLoading] = useState(false); const [likes, setLikes] = useState(Number.parseInt(likesCount, 10)); const handleLikeCourse = (id: string) => async () => { if (isLoading || me === null) { return; } try { const isCourseLiked = me.likes.some(course => course.id === id); const setCourseLike = () => me.likes.push({ id }); const unsetCourseLike = () => (me.likes = me.likes.filter(course => course.id !== id)); if (!isCourseLiked) { setIsLoading(true); setLikes(likes => likes + 1); await CourseServiceInstance.setLike(id); runInAction(setCourseLike); } else { setIsLoading(true); setLikes(likes => likes - 1); await CourseServiceInstance.unsetLike(id); runInAction(unsetCourseLike); } } finally { setIsLoading(false); } }; return ( {lessonsCount} {studentsCount} ); }; export const CourseBackgroundImage: React.FC = props => { return ( ); }; interface ILessonListProps extends StackProps { course: ICourse; render: React.FC; } export const LessonList: React.FC = props => { const { course, render: Item, ...stackProps } = props; const { userStore: { me }, } = useRootStore(); const bg = useColorModeValue('gray.50', 'gray.700'); if (!me) return null; const isCourseAvailable = helpers.userHasCourse(course, me.courses); return ( {course.lessons.map(lesson => ( ))} ); }; interface ILessonItemProps extends LinkBoxProps { id: string; title: string; duration: string; isDisabled?: boolean | undefined; } export const LessonItem: React.FC = props => { const { id, title, duration, isDisabled, ...linkBoxProps } = props; return ( {!isDisabled ? ( {title} ) : ( {title} )} {moment.duration(duration).asMinutes()}min ); };