|
| 1 | +// tslint:disable:no-magic-numbers |
| 2 | +import { Avatar, Button, Container, Grid, Paper } from '@material-ui/core'; |
| 3 | +import React, { memo, useEffect } from 'react'; |
| 4 | +import { useDispatch, useSelector } from 'react-redux'; |
| 5 | +import { RouteComponentProps } from 'react-router'; |
| 6 | +import { Redirect } from 'react-router-dom'; |
| 7 | +import { ROOT } from '../../constants/routes'; |
| 8 | +import { getCourseDetailsRequested } from '../../redux/courseDetails/actionCreators'; |
| 9 | +import { State } from '../../redux/rootReducer'; |
| 10 | +import useStyles from './styles'; |
| 11 | + |
| 12 | +export interface Params { |
| 13 | + readonly courseSlug: string; |
| 14 | +} |
| 15 | + |
| 16 | +const CourseView = ({ match }: RouteComponentProps<Params>) => { |
| 17 | + const classes = useStyles(); |
| 18 | + |
| 19 | + if (match.params.courseSlug.trim() === '') { |
| 20 | + return <Redirect to={ROOT} />; |
| 21 | + } |
| 22 | + const { course, getCourseDetailsLoading } = useSelector( |
| 23 | + (state: State) => state.courseDetails |
| 24 | + ); |
| 25 | + |
| 26 | + const dispatch = useDispatch(); |
| 27 | + |
| 28 | + useEffect(() => { |
| 29 | + dispatch(getCourseDetailsRequested(match.params.courseSlug)); |
| 30 | + }, [match.params.courseSlug]); |
| 31 | + |
| 32 | + if (getCourseDetailsLoading || course === undefined) { |
| 33 | + // TODO: make course placeholder |
| 34 | + return <div>Loading...</div>; |
| 35 | + } |
| 36 | + |
| 37 | + const coursePrice = Number(Math.random() * 10 + 9).toFixed(2); |
| 38 | + |
| 39 | + return ( |
| 40 | + <div className={classes.root}> |
| 41 | + <Container component="main" maxWidth="lg"> |
| 42 | + <Grid container spacing={3}> |
| 43 | + <Grid item xs={12} sm={9}> |
| 44 | + <Paper className={classes.paper}> |
| 45 | + <h3>{course.title}</h3> |
| 46 | + <h4> |
| 47 | + <Avatar className={classes.avatar}> |
| 48 | + {`${(course.user.firstName as string).substr(0, 1)}${(course |
| 49 | + .user.lastName as string).substr(0, 1)}`} |
| 50 | + </Avatar> |
| 51 | + {course.user.firstName} {course.user.lastName} |
| 52 | + </h4> |
| 53 | + <div |
| 54 | + dangerouslySetInnerHTML={{ |
| 55 | + __html: course.description as string, |
| 56 | + }} |
| 57 | + /> |
| 58 | + </Paper> |
| 59 | + </Grid> |
| 60 | + <Grid item xs={12} sm={3}> |
| 61 | + <Paper className={classes.paper}> |
| 62 | + <img src={course.imageUrl} style={{ width: '100%' }} /> |
| 63 | + <h4>{`£${coursePrice}`}</h4> |
| 64 | + <Button variant="contained" fullWidth color="primary"> |
| 65 | + Add to cart |
| 66 | + </Button> |
| 67 | + <Button variant="contained" fullWidth color="secondary"> |
| 68 | + Buy now |
| 69 | + </Button> |
| 70 | + </Paper> |
| 71 | + </Grid> |
| 72 | + </Grid> |
| 73 | + </Container> |
| 74 | + </div> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +export default memo(CourseView); |
0 commit comments