Newer
Older

Lucas Eduardo Schoenfelder
committed
/*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, useContext, useEffect } from 'react';
import { Store } from '../Store.js'
import { apiDomain, apiUrl } from '../env';

Lucas Eduardo Schoenfelder
committed
import noAvatar from "../img/default_profile.png";
import Button from '@material-ui/core/Button';
import styled from 'styled-components'
import Slide from '@material-ui/core/Slide';
import Grid from '@material-ui/core/Grid';
import { StyledCard, CardDiv, CardReaDiv, Footer, LikeCounter, ButtonNoWidth, EnviadoPor, TagContainer } from './ResourceCardFunction.js'

Lucas Eduardo Schoenfelder
committed
import Rating from '@material-ui/lab/Rating';
import StarBorderIcon from '@material-ui/icons/StarBorder';
import FolderIcon from '@material-ui/icons/Folder';
import FavoriteIcon from '@material-ui/icons/Favorite';
import AddIcon from '@material-ui/icons/Add';
import CheckIcon from '@material-ui/icons/Check';
import LockIcon from '@material-ui/icons/Lock';
import ColCardOwnerOptions from './ColCardOwnerOptions.js'
import ColCardPublicOptions from './ColCardPublicOptions'
import axios from 'axios'
import { getAxiosConfig } from '../Components/HelperFunctions/getAxiosConfig'
import { saveHeaders } from '../Components/HelperFunctions/saveTokens';
import SignUpModal from './SignUpModal'
import LoginModal from './LoginModal.js'
import Snackbar from '@material-ui/core/Snackbar';
import MuiAlert from '@material-ui/lab/Alert';

Lucas Eduardo Schoenfelder
committed
export default function CollectionCardFunction(props) {
// eslint-disable-next-line
const [userAvatar] = useState(props.avatar ? (`${apiDomain}` + props.avatar) : noAvatar)
const [userFollowingCol, toggleUserFollowingCol] = useState(props.followed ? props.followed : false)
const handleToggleUserFollowingCol = () => { toggleUserFollowingCol(!userFollowingCol) }

Lucas Eduardo Schoenfelder
committed

Lucas Eduardo Schoenfelder
committed
const [likesCount, setLikesCount] = useState(props.likeCount)
const [liked, toggleLiked] = useState(props.liked)
const [signUpOpen, setSignUp] = useState(false)
const [loginOpen, setLogin] = useState(false)
const [successfulLoginOpen, handleSuccessfulLogin] = useState(false)

Lucas Eduardo Schoenfelder
committed
const handleLike = () => {
let payload = {}
let config = getAxiosConfig()
if (state.currentUser.id) {
axios({
method: 'put',
url: `${apiUrl}/collections/` + props.id + '/like',
headers: config.headers
}).then(
(response) => {
toggleLiked(!liked)
setLikesCount(response.data.count)
saveHeaders(response)
},
(error) => { console.log(error) }
)
}
else {
setLogin(!loginOpen)
}

Lucas Eduardo Schoenfelder
committed
}

Lucas Eduardo Schoenfelder
committed
const [followingHover, handleFollowingHover] = useState(false)
const toggleFollowingHover = (value) => { handleFollowingHover(value) }

Lucas Eduardo Schoenfelder
committed
const [slideIn, setSlide] = useState(false)

Lucas Eduardo Schoenfelder
committed
const handleFollow = () => {
let config = getAxiosConfig()
let payload = {}
if (state.currentUser.id) {
axios.put((`${apiUrl}/collections/` + props.id + '/follow'), payload, config).then(
(response) => {
saveHeaders(response)
handleToggleUserFollowingCol()
},
(error) => { console.log(error) }
)
}
else {
setLogin(!loginOpen)
}

Lucas Eduardo Schoenfelder
committed
const RenderFollowButton = () => {
return (
<FollowButton onClick={handleFollow}>

Lucas Eduardo Schoenfelder
committed
</FollowButton>
)
}
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// setTimeout(function () {
// if (state.currentUser.id) {
// const config = getAxiosConfig();
// axios({
// method: 'get',
// url: `${apiUrl}/users/${state.currentUser.id}/following/Collection`,
// headers: config.headers
// }).then(
// (response) => {
// const data = response.data
// for (let i = 0; i < data.length; i++)
// if (data[i].followable.id === props.id)
// toggleUserFollowingCol(true)
// saveHeaders(response)
// })
// }
// else {
// toggleLiked(false);
// toggleUserFollowingCol(false);
// }
// }, 1000);
// setTimeout(function () {
// if (state.currentUser.id) {
// const config = getAxiosConfig();
// axios({
// method: 'get',
// url: `${apiUrl}/users/${state.currentUser.id}/collections/liked`,
// headers: config.headers
// }).then(
// (response) => {
// const data = response.data
// for (let i = 0; i < data.length; i++)
// if (data[i].id === props.id)
// toggleLiked(true)
// saveHeaders(response)
// })
// }
// else {
// toggleLiked(false);
// toggleUserFollowingCol(false);
// }
// }, 2000);
if(!state.currentUser.id)
{
toggleLiked(false);
toggleUserFollowingCol(false);
}

Lucas Eduardo Schoenfelder
committed
const RenderFollowingButton = () => {
return (
<FollowingButton onMouseOver={() => toggleFollowingHover(true)}
onMouseLeave={() => toggleFollowingHover(false)} onClick={handleFollow}>

Lucas Eduardo Schoenfelder
committed
{
followingHover ?
(
[
<span>DEIXAR DE SEGUIR</span>
]
)
:
(
[
<React.Fragment>
<CheckIcon /><span>SEGUINDO</span>
</React.Fragment>
]
)

Lucas Eduardo Schoenfelder
committed
}
</FollowingButton>
)
}
const SlideAnimationContent = () => {
return (
// <SlideContentDiv>
// <HeaderContainer>{/*marginBottom:10px*/}
// <AvatarDiv>
// <img className="img" src={userAvatar} alt="user avatar" />
// </AvatarDiv>
// <EnviadoPor>
// Enviado por:
// <br />
// <p>{props.author}</p>
// </EnviadoPor>
// </HeaderContainer>
// <TagContainer>
// {
// props.tags.map((tag) =>
// <span key={tag.id}>{tag.name}</span>
// )
// }
// </TagContainer>
// </SlideContentDiv>

Lucas Eduardo Schoenfelder
committed
<SlideContentDiv>
<div style={{ display: "flex", flex: "1" }}>{/*marginBottom:10px*/}

Lucas Eduardo Schoenfelder
committed
<SlideAvatarDiv>
<img className="img" src={userAvatar} alt="user avatar" />

Lucas Eduardo Schoenfelder
committed
</SlideAvatarDiv>
<EnviadoPor>
Enviado por:

Lucas Eduardo Schoenfelder
committed
<p>{props.author}</p>
</EnviadoPor>
</div>
{
props.tags &&
<TagContainer>
{
props.tags.map((tag) =>

Lucas Eduardo Schoenfelder
committed
<span key={tag.id}>{tag.name}</span>

Lucas Eduardo Schoenfelder
committed
}
</SlideContentDiv>
)
}
const handleSignUp = () => {
setSignUp(!signUpOpen)
}

Lucas Eduardo Schoenfelder
committed
const handleLogin = () => {
setLogin(!loginOpen)
}

Lucas Eduardo Schoenfelder
committed
function Alert(props) {
return <MuiAlert elevation={6} variant="filled" {...props} />;
}

Lucas Eduardo Schoenfelder
committed
function toggleLoginSnackbar(reason) {
if (reason === 'clickaway') {
return;
}
handleSuccessfulLogin(false);
}
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
return (
<>
<SignUpModal open={signUpOpen} handleClose={handleSignUp} openLogin={handleLogin}
/>
<LoginModal open={loginOpen} handleClose={() => setLogin(false)} openSignUp={handleSignUp}
openSnackbar={() => { handleSuccessfulLogin(true) }}
/>
<Snackbar open={successfulLoginOpen} autoHideDuration={1000} onClose={toggleLoginSnackbar}
anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
>
<Alert severity="success" style={{ backgroundColor: "#00acc1" }}>Você está conectado(a)!</Alert>
</Snackbar>
<StyledCard>
<CardDiv>
<CardReaDiv>
<Header onMouseEnter={controlSlide} onMouseLeave={controlSlide}>
{/*slide animation and content*/}
<Slide direction="right" in={slideIn} timeout={1000}>
<Link to={"/colecao-do-usuario/" + props.id} className="text">
{SlideAnimationContent()}
</Link>
</Slide>
<div className={`slideContentLinkBeforeActive${slideIn}`} style={{ width: '100%', height: '100%' }}>
<a href={"/colecao-do-usuario/" + props.id}>
<UserInfo>
<AvatarDiv>
<img src={userAvatar} alt="user avatar" />
</AvatarDiv>
<UserAndTitle>
<span>{props.author}</span>
<span className={"col-name"}>{props.name}</span>
</UserAndTitle>
</UserInfo>
<StyledGrid container direction="row">
{
props.thumbnails.map((thumb) =>
<Grid item xs={props.thumbnails <= 4 && props.thumbnails > 0 ? 12 / props.thumbnails.length : 6}>
<div style={{ backgroundImage: `url(${`${apiDomain}` + thumb})`, height: "100%", width: "100%", backgroundSize: "cover", backgroundPosition: "center" }} />
</Grid>
)
}
</StyledGrid>
</a>
</div>
</Header>

Lucas Eduardo Schoenfelder
committed
<Description> {/*renders rating, number of learning objects and likes count*/}
{
props.authorID !== state.currentUser.id &&
<Rating
name="customized-empty"
value={props.rating * 10}
precision={0.5}
readOnly
style={{ color: "#666" }}
emptyIcon={<StarBorderIcon fontSize="inherit" />}
/>
}

Lucas Eduardo Schoenfelder
committed
<Footer>
<Type>
<FolderIcon />
<span style={{ fontWeight: "bold" }}>{props.collections.length} </span>
<span>{props.collections.length !== 1 ? "Recursos" : "Recurso"}</span>
</Type>
<LikeCounter>
<span>{likesCount}</span>
<ButtonNoWidth onClick={handleLike}>
<FavoriteIcon style={{ color: liked ? "red" : "#666" }} />
</ButtonNoWidth>
</LikeCounter>
</Footer>
</Description>

Lucas Eduardo Schoenfelder
committed

Lucas Eduardo Schoenfelder
committed
<CardReaFooter style={{ justifyContent: "space-between" }}> {/*renders following/unfollow and follow button*/}
<Grid container>
<Grid item xs={6} style={{ display: "flex", justifyContent: "center" }}>
{
props.privacy === 'private' &&
<LockIcon style={{ color: "#666" }} />
}
</Grid>
<Grid item xs={6} style={{ display: "flex", justifyContent: "flex-end" }}>
<ColCardOwnerOptions
id={props.id}
/>
</Grid>
</Grid>
</CardReaFooter>
<CardReaFooter> {/*renders following/unfollow and follow button*/}
{
userFollowingCol ?
(
[
RenderFollowingButton()
]
)
:
(
[
RenderFollowButton()
]
)
}
<ColCardPublicOptions
id={props.id}
/>
</CardReaFooter>

Lucas Eduardo Schoenfelder
committed
)
}
const SlideAvatarDiv = styled.div`
vertical-align : middle;
border : 0;
height : 40px !important;
width : 40px !important;

Lucas Eduardo Schoenfelder
committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
border : 0;
vertical-align : middle;
border-radius : 50%;
}
`
const SlideContentDiv = styled.div`
background-color : #7e57c2;
padding : 10px;
width : 100%;
height : 100%;
position : absolute;
display : flex;
flex-direction : column;
`
const UserAndTitle = styled.div`
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
width: 100%;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
padding: 30px 10px 10px 10px;
margin-top: -25px;
background-color: rgba(0,0,0,.5);
color: #fff;
.col-name {
padding-top: 5px;
font-size: 17px;
font-weight: 500;
text-overflow: ellipsis;
overflow: hidden;
text-align: center;
width: 100%;
}
`
const AvatarDiv = styled.div`
z-index : 5;
overflow : hidden;
img {
border-radius : 50%;
height : 70px;
width : 70px;
border : 2px solid #fff !important;

Lucas Eduardo Schoenfelder
committed
background-color : #fff;
box-shadow : 0 1px 3px rgba(0,0,0,.45);
}
`
const UserInfo = styled.div`
position : absolute;
display : flex;
flex-direction : column;
width : 100%;
height : 100%;
justify-content : flex-end;
align-items : center;
`
const CardReaFooter = styled.div`
height : 60px;
display : flex;
justify-content : center;
align-items : center;

Lucas Eduardo Schoenfelder
committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
.MuiSvgIcon-root {
font-family: 'Material Icons';
color : inherit;
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
}
`
const Type = styled.div`
line-height : 1;
font-size : .85em;
.MuiSvgIcon-root {
font-size : 21px;
color : #673ab7;
vertical-align : middle;
padding-right : .4em;
}
`
const StyledGrid = styled(Grid)`
.MuiGrid-item {
height : 136px;
width 130px;
background : #f9f9f9;
border : 1px solid #f4f4f4;
border-color : #f4f4f4;
}
`
const Header = styled.div`
display : flex;
height : 230px;
position : relative;
overflow : hidden;
`
const Description = styled.div`
display : flex;
height : 60px
background-color : #fff;
flex-direction : column;
justify-content: space-between;
padding : 15px;
`
const FollowButton = styled(Button)`
border : 2px solid #503096 !important;
border-radius : 5px !important;
margin : 10px !important;
color :#503096 !important;
min-width : 150px !important;
min-height : 36px !important;
text-align : center !important;
vertical-align : middle !important;
background : transparent !important;
text-transform : uppercase !important;
font-family : inherit !important;
overflow : hidden !important;
font-weight : normal !important;
text-decoration : none !important;
letter-spacing: .01em !important;
white-space : nowrap !important;
font-weight: 500;
&:hover {
background-color : rgba(158,158,158,0.2) !important;
}
`
const FollowingButton = styled(Button)`
border : 2px solid #503096 !important;
border-radius : 5px !important;
margin : 10px !important;
color :#fff !important;
min-width : 150px !important;
min-height : 36px !important;
text-align : center !important;
vertical-align : middle !important;
background-color : #503096 !important;
text-transform : uppercase !important;
font-family : inherit !important;
overflow : hidden !important;
font-weight : normal !important;
text-decoration : none !important;
letter-spacing: .01em !important;
white-space : nowrap !important;
font-weight: 500;
&:hover {
background-color : rgba(158,158,158,0.2) !important;
color : #503096 !important;
box-shadow : none !important;
}
`