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';
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
const url = apiUrl + '/users/' + props.user_id + '/following/Collection';
console.log("props: ", props);
axios({
method: 'get',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'access-token': sessionStorage.getItem('@portalmec/accessToken'),
'client': sessionStorage.getItem('@portalmec/clientToken'),
'uid': sessionStorage.getItem('@portalmec/uid'),
'If-None-Match': null
},
}).then(response => {
const data = [...response.data];
if(data)
data.map((e) => {
if (e["followable"]["id"] === Number(props.collection_id)){
setVariant("contained");
setButtonText("Seguindo");
setIcon(<CheckIcon fontSize="large" />)
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 SaveNewHeaders = (response) => {
if (
(response.headers['access-token'] === undefined || response.headers['access-token'] === null) &&
(response.headers.client === undefined || response.headers.client === null)
) {
} else {
sessionStorage.setItem('@portalmec/accessToken', response.headers['access-token'])
sessionStorage.setItem('@portalmec/clientToken', response.headers.client)
console.log('saved')
}
}
const handleClick = () => {
if (!props.user_id)
setSignUpOpen(true);
else if (!following) {
const url = apiUrl + '/collections/' + props.collection_id + '/follow';
setVariant("contained");
setButtonText("Seguindo");
axios({
method: 'put',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'access-token': sessionStorage.getItem('@portalmec/accessToken'),
'client': sessionStorage.getItem('@portalmec/clientToken'),
'uid': sessionStorage.getItem('@portalmec/uid'),
'If-None-Match': null
},
}).then(response => {
if (response.status === 201)
setFollowing(true);
else if (response.status === 200)
setFollowing(false);
SaveNewHeaders(response)
})
const url = apiUrl + '/collections/' + props.collection_id + '/follow';
setButtonText("Seguir Coleção");
setIcon(<AddIcon fontSize="large" />);
axios({
method: 'put',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'access-token': sessionStorage.getItem('@portalmec/accessToken'),
'client': sessionStorage.getItem('@portalmec/clientToken'),
'uid': sessionStorage.getItem('@portalmec/uid'),
'If-None-Match': null
},
}).then(response => {
if (response.status === 201)
setFollowing(true);
else if (response.status === 200)
setFollowing(false);
SaveNewHeaders(response)
})
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;