/*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 axios from 'axios'; import {apiUrl} from '../env'; 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 [following, setFollowing] = useState(false); //user following collection useEffect(() => { axios.get(apiUrl+'/users/'+props.user_id+'/following/Collection') .then(res => { for (const element in res.body) if (element.id == props.collection_id) setFollowing(true); }); }, []) //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" />); } } const handleClick = () => { if (!props.user_id) setSignUpOpen(true); else if (!following) { setVariant("contained"); setButtonText("Seguindo"); setIcon(<CheckIcon fontSize="large"/>) setFollowing(true); axios.post(apiUrl+'/collections/'+props.collection_id+'/follow') .then(res => { if (Number(res.status) != 201) setFollowing(false); setIcon(<AddIcon fontSize="large"/>); setButtonText("Seguindo"); setVariant("contained"); }); } else { setVariant("outlined"); setButtonText("Deixar de seguir"); setIcon(<AddIcon fontSize="large"/>); setFollowing(false); axios.delete(apiUrl+'/collections/'+props.collection_id+'/follow') .then(res => { if (Number(res.status) == 200) setFollowing(true); setIcon(<CheckIcon fontSize="large"/>); setButtonText("Deixar de seguir"); setVariant("outlined"); }); } }; if (!props.user_is_owner) return ( <div> <FollowButton variant={variant} color="primary" startIcon={icon} size="large" onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} onClick={handleClick} > <ButtonText>{button_text}</ButtonText> </FollowButton> <SignUpModal open={sign_up_open} handleClose={() => setSignUpOpen(false)} /> </div> ); else return (<div></div>); } const Title=styled.h1` font-size: 3em; color: rgb(102, 102, 102); float: left; ` const ButtonText=styled.span` font-weight: bolder; font-size: 1.2em; ` const FollowButton=styled(Button)` padding-left: 10; padding-right: 10; width: 250px; `