/*Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre Departamento de Informatica - Universidade Federal do Parana This file is part of Plataforma Integrada MEC. Plataforma Integrada MEC is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Plataforma Integrada MEC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Plataforma Integrada MEC. If not, see <http://www.gnu.org/licenses/>.*/ import React, { useContext, useState, useEffect } from 'react'; import { Grid } from '@material-ui/core'; import Rating from '@material-ui/lab/Rating'; import IconButton from '@material-ui/core/IconButton'; import StarBorderIcon from '@material-ui/icons/StarBorder'; import FavoriteIcon from '@material-ui/icons/Favorite'; import InfoIcon from '@material-ui/icons/Info'; import { Store } from '../Store.js' import ReportModal from './ReportModal.js'; import SignUpModal from './SignUpModal.js'; import LoginModal from './LoginModal.js'; import { putRequest } from './HelperFunctions/getAxiosConfig.js' import SnackBarComponent from './SnackbarComponent'; export default function CollectionReview(props) { const { state } = useContext(Store); const [likes, setLikes] = useState(); const [liked, setLiked] = useState(); const [stars, setStars] = useState(); const [reportOpen, setReportOpen] = useState(false); const [sign_up_open, setSignUpOpen] = useState(false); const [log_in_open, setLoginOpen] = useState(false); const [snackInfo, setSnackInfo] = useState({ open: false, text: '', severity: '', color: '' }); const handleClickReport = () => { if (state.currentUser.id) setReportOpen(true); else setSignUpOpen(true) } function handleSuccess(data) { setLiked(!liked) setLikes(data.count) } const handleLikeClick = () => { if (state.currentUser.id) { const url = `/collections/${props.id}/like` putRequest(url, {}, handleSuccess, (error) => { console.log(error) }) } else setSignUpOpen(true); } const handleCloseModal = () => { setReportOpen(false); } function handleOpenSnackSignIn() { const info = { open: true, text: 'VocĂȘ foi logado com sucesso!', severity: 'success', color: '', } handleSnackInfo(info) } function handleSnackInfo(info) { setSnackInfo({ ...info }) } function handleCloseSnack() { setSnackInfo({ open: false, text: '', severity: '', color: '', }) } function handleOpenSignUp() { setSignUpOpen(true) } useEffect(() => { setLiked(props.liked) setLikes(props.likes) setStars(props.stars) }, [props]) return ( <Grid container direction="column" style={props.contrast === "" ? {color: "#666"} : {color: "white"}}> <SnackBarComponent snackbarOpen={snackInfo.open} handleClose={handleCloseSnack} severity={snackInfo.severity} text={snackInfo.text} color={snackInfo.color} /> <Grid sm={12} container direction="row" alignItems="center" style={{justifyContent: "center"}} > <Grid item> <Rating style={props.contrast === "" ? {} : {color: "white"}} name="customized-empty" value={Number(stars)} readOnly onClick={props.scrollToComment} emptyIcon={<StarBorderIcon className={`${props.contrast}Text`} fontSize="inherit" />} /> </Grid> <Grid item justify="center" alignItems="center"> <IconButton style={{color: "inherit"}} aria-label="like" onClick={handleLikeClick}> {likes}<FavoriteIcon style={props.contrast === "" ? {fill: liked ? "red" : null} : {fill: "yellow"}} /> </IconButton> </Grid> </Grid> <Grid item sm={12}> <IconButton aria-label="report" style={{ fontSize: 'medium' }} onClick={handleClickReport}> <InfoIcon style={props.contrast === "" ? {color: "#666"} : {color: "white"}}/><span style={props.contrast === "" ? {color: "#666"} : {color: "yellow", textDecoration: "underline"}}>Reportar erro ou abuso</span> </IconButton> <ReportModal contrast={props.contrast} open={reportOpen} handleClose={handleCloseModal} form="colecao" complainableId={props.id} complainableType="Collection" /> </Grid> <SignUpModal contrast={props.contrast} open={sign_up_open} handleClose={() => setSignUpOpen(false)} openLogin={() => setLoginOpen(true)} /> <LoginModal contrast={props.contrast} openSnackbar={handleOpenSnackSignIn} open={log_in_open} handleClose={() => setLoginOpen(false)} openSignUp={handleOpenSignUp} /> </Grid> ); }