FollowCollectionButton.js 6.17 KiB
/*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, useContext } 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 { getRequest, putRequest } from './HelperFunctions/getAxiosConfig'
import { Store } from '../Store'
export default function FollowCollectionButton(props) {
const { state } = useContext(Store)
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
function handleSuccessGet(data) {
if (!data.errors)
data.map((e) => {
if (e["followable"]["id"] === Number(props.collection_id)) {
setVariant("contained");
setButtonText("Seguindo");
setIcon(<CheckIcon fontSize="large" />)
setFollowing(true);
}
return undefined
})
else {
const info = {
open: true,
text: 'Falha ao verificar se o usuário segue a coleção!',
severity: 'error',
color: 'red'
}
handleSnackInfo(info)
}
}
useEffect(() => {
if (state.currentUser.id) {
const url = `/users/${props.user_id}/following/Collection`
getRequest(
url,
handleSuccessGet,
(error) => {
const info = {
open: true,
text: 'Falha ao verificar se o usuário segue a coleção!',
severity: 'error',
color: 'red'
}
handleSnackInfo(info)
})
}
else {
setIcon(<AddIcon fontSize="large" />)
setButtonText("Seguir Coleção")
setVariant("outlined")
setFollowing(false)
}
}, [state.currentUser.id]);
//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
variant={variant}
color="primary"
startIcon={icon}
size="small"
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onClick={handleClick}
>
<ButtonText>{button_text}</ButtonText>
</FollowButton>
<SignUpModal
open={sign_up_open}
handleClose={() => setSignUpOpen(false)}
openLogin={handleOpenLogin}
/>
<LoginModal
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)`
padding-left: 10;
padding-right: 10;
width: 250px;
`