import React from "react"; import PropTypes from "prop-types"; import { Container, CardImg, CardBody, CardTitle, CardFooter, Button, Input, FormGroup, Label, Card, Modal, ModalHeader, ModalBody, ModalFooter, Row, Col, } from "reactstrap"; import CommentList from "../../Components/Comment/CommentList"; import Loading from "../../Components/Loading"; import Error from "../../Components/Alert/Error"; import CommentApi from "../../Apis/CommentApi"; import InfoMessage from "../../Components/Alert/InfoMessage"; import MovieDetails from "./partials/MovieDetails"; import {ratings} from "../../Utils/utilsConst"; import {CardHeader} from "semantic-ui-react"; class DetailMovie extends React.Component { state = { comments: [], comment: "", rate: 1, disabled: true, hasErrors: false, loading: false, message: "There is an issue to fetch data from server. Please try again later.", info: false, infoMessage: "The comment is added successfully", modal: false, }; async componentDidMount() { await this.onFetchComments(); } onFetchComments = async () => { this.setState({loading: true}); try { const commentApi = new CommentApi(); const data = await commentApi.getAllComments(this.props.match.params.imdbID); if (!data.success) { this.onShowErrorMessage(); } else { this.setState({ comments: data.data, loading: false, }); } } catch (err) { console.log("onFetchComments err: ", err); this.onShowErrorMessage(); } }; toggle = () => { const {modal} = this.state; this.setState({modal: !modal}); }; onChange = e => { this.setState({ [e.target.name]: e.target.value, disabled: e.target.value === "", }); }; onSubmit = async () => { const {comment, rate} = this.state; if (comment === "") { return; } this.setState({loading: true, modal: false}); try { const commentApi = new CommentApi(); const body = { comment, rate, elementId: this.props.match.params.imdbID, }; const data = await commentApi.addComment(body); if (!data.success) { this.onShowErrorMessage(); } else { await this.onFetchComments(); this.setState({ comment: "", disabled: true, loading: false, info: true, infoMessage: "The comment is added successfully", }); setTimeout(this.onClearMessage, 2000); } } catch (err) { console.log("onSubmit err: ", err); this.onShowErrorMessage(); } }; onShowErrorMessage = () => { this.setState({hasErrors: true, loading: false}); setTimeout(this.onClearMessage, 5000); }; onClearMessage = () => { this.setState({hasErrors: false, info: false}); }; getReviewModal = () => { const {modal, comment, rate} = this.state; return ( Add Review {ratings.map(rating => ( // eslint-disable-next-line react/jsx-key ))} ); }; getAverageRating = () => { const {comments} = this.state; let rating = 0; comments.length > 0 && comments.map(comment => { rating += parseInt(comment.rate); }); const averageRating = rating / comments.length; return averageRating; }; render() { const {data} = this.props.history.location.state; const {comments, loading, hasErrors, message, info, infoMessage} = this.state; if (!data) { return null; } const reviewModal = this.getReviewModal(); const averageRating = this.getAverageRating(); return ( {hasErrors && } {loading && }
{averageRating ? averageRating.toFixed(1) : 0}
{comments.length} votes
{reviewModal} {info && }
); } } DetailMovie.propTypes = { history: PropTypes.any, match: PropTypes.shape({ params: PropTypes.shape({ imdbID: PropTypes.string.isRequired, }), }), }; export default DetailMovie;