/*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, { useState, useEffect } from 'react'; import Button from '@material-ui/core/Button'; import CheckIcon from '@material-ui/icons/Check'; import AddIcon from '@material-ui/icons/Add'; import styled from 'styled-components'; import SignUpModal from './SignUpModal.js'; import LoginModal from './LoginModal'; import SnackBarComponent from './SnackbarComponent'; import { putRequest } from './HelperFunctions/getAxiosConfig' export default function FollowCollectionButton(props) { const [icon, setIcon] = useState(<AddIcon fontSize="large" />); const [button_text, setButtonText] = useState("Seguir coleção"); const [variant, setVariant] = useState("outlined"); const [sign_up_open, setSignUpOpen] = useState(false); const [open_login, setOpenLogin] = useState(false); const [snackInfo, setSnackInfo] = useState({ open: false, text: '', severity: '', color: '' }); const [following, setFollowing] = useState(false); //user following collection useEffect(() => { if (props.followed) { setFollowing(true) setButtonText("Seguindo") setVariant("contained") setIcon(<CheckIcon fontSize="large" />); } else { setFollowing(false) setButtonText("Seguir coleção") setVariant("outlined") setIcon(<AddIcon fontSize="large" />); } }, [props]) //handleMouse{Enter, Leave} only do anything when user follows given collection: const handleMouseEnter = () => { if (following) { setVariant("outlined"); setButtonText("Deixar de seguir"); setIcon(null); } } const handleMouseLeave = () => { if (following) { setVariant("contained"); setButtonText("Seguindo"); setIcon(<CheckIcon fontSize="large" />); } } function handleOpenSnackSignIn() { const info = { open: true, text: 'Você foi logado com sucesso!', severity: 'success', color: '', } handleSnackInfo(info) } function handleCloseSnack() { setSnackInfo({ open: false, text: '', severity: '', color: '', }) } function handleSnackInfo(info) { setSnackInfo({ ...info }) } function handleOpenLogin() { setOpenLogin(true) } function handleOpenSignUp() { setSignUpOpen(true) } function handleSuccessfulFollow(data) { if (data.errors) { setVariant("contained"); handleFailFollow() } else { const info = { open: true, text: 'Sucesso ao seguir a coleção!', severity: 'success', color: 'green' } handleSnackInfo(info) setVariant("contained"); setButtonText("Seguindo"); setIcon(<CheckIcon fontSize="large" />) setFollowing(true); } } function handleFailFollow(err) { const info = { open: true, text: 'Falha ao seguir coleção', severity: 'fail', color: 'red', } handleSnackInfo(info) } function handleSuccessfulUnfollow(data) { if (data.errors) handleFailUnfollow() else { const info = { open: true, text: 'Sucesso ao deixar de seguir a coleção!', severity: 'success', color: 'green' } handleSnackInfo(info) setVariant("outlined"); setButtonText("Seguir Coleção"); setIcon(<AddIcon fontSize="large" />); setFollowing(false); } } function handleFailUnfollow(err) { const info = { open: true, text: 'Falha ao deixar de seguir coleção', severity: 'error', color: 'red', } handleSnackInfo(info) } const handleClick = () => { const url = `/collections/${props.collection_id}/follow` if (!props.user_id) setSignUpOpen(true); else if (!following) { putRequest(url, {}, handleSuccessfulFollow, handleFailFollow) } else { putRequest(url, {}, handleSuccessfulUnfollow, handleFailUnfollow) } }; if (!props.user_is_owner) return ( <div> <SnackBarComponent snackbarOpen={snackInfo.open} handleClose={handleCloseSnack} severity={snackInfo.severity} text={snackInfo.text} color={snackInfo.color} /> <FollowButton contrast={props.contrast} variant={variant} startIcon={icon} size="small" onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} onClick={handleClick} > <ButtonText>{button_text}</ButtonText> </FollowButton> <SignUpModal contrast={props.contrast} open={sign_up_open} handleClose={() => setSignUpOpen(false)} openLogin={handleOpenLogin} /> <LoginModal contrast={props.contrast} openSnackbar={handleOpenSnackSignIn} open={open_login} handleClose={() => setOpenLogin(false)} openSignUp={handleOpenSignUp} /> </div> ); else return (<div></div>); } const ButtonText = styled.span` font-weight: bolder; font-size: 1.2em; ` const FollowButton = styled(Button)` color: ${props => props.contrast === "" ? "white !important" : "yellow !important"}; background-color: ${props => props.contrast === "" ? "#3f51b5 !important" : "black !important"}; text-decoration: ${props => props.contrast === "" ? "none !important" : "yellow underline !important"}; border: ${props => props.contrast === "" ? "none !important" : "1px solid white !important"}; padding-left: 10; padding-right: 10; width: 250px; `