Newer
Older
/*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 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';

Lucas Eduardo Schoenfelder
committed
import {getRequest, 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 [following, setFollowing] = useState(false); //user following collection
function handleSuccessGet (data) {
if(data)
data.map((e) => {
if (e["followable"]["id"] === Number(props.collection_id)){
setVariant("contained");
setButtonText("Seguindo");
setIcon(<CheckIcon fontSize="large" />)
setFollowing(true);
}
return undefined
})
}
const url = `/users/${props.user_id}/following/Collection`

Lucas Eduardo Schoenfelder
committed
getRequest(url, handleSuccessGet, (error) => console.log(error))
//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 handleSuccessfulFollow (data) {
setVariant("contained");
setButtonText("Seguindo");
setIcon(<CheckIcon fontSize="large" />)
setFollowing(true);
}
function handleSuccessfulUnfollow (data) {
setVariant("outlined");
setButtonText("Seguir Coleção");
setIcon(<AddIcon fontSize="large" />);
setFollowing(false);
}
const url = `/collections/${props.collection_id}/follow`
if (!props.user_id)
setSignUpOpen(true);
else if (!following) {

Lucas Eduardo Schoenfelder
committed
putRequest(url, {}, handleSuccessfulFollow, (error) => {console.log(error)})

Lucas Eduardo Schoenfelder
committed
putRequest(url, {}, handleSuccessfulUnfollow, (error) => {console.log(error)})
return (
<div>
<FollowButton
variant={variant}
color="primary"
startIcon={icon}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onClick={handleClick}
>
<ButtonText>{button_text}</ButtonText>
</FollowButton>
<SignUpModal open={sign_up_open} handleClose={() => setSignUpOpen(false)} />
</div>
);
padding-left: 10;
padding-right: 10;
width: 250px;