Skip to content
Snippets Groups Projects
ResourceCardFunction.js 12.2 KiB
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 React, {useState, useEffect} from 'react';
import Card from '@material-ui/core/Card';
import ResourceCardOptions from './ResourceCardOptions'
import noAvatar from "../img/default_profile.png";
// import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import styled from 'styled-components'
// import animacao from "../img/laranja/ANIMACAO_SIMULACAO.jpg";
// import apresentacao from "../img/laranja/APRESENTACAO.jpg";
// import aplicativo from "../img/laranja/APP.jpg";
// import audio from "../img/laranja/AUDIO.jpg";
// import vazio from "../img/laranja/EMPTY.jpg";
// import imagem from "../img/laranja/IMAGEM.jpg";
// import grafico from "../img/laranja/INFOGRAFICO.jpg";
// import jogo from "../img/laranja/JOGO.jpg";
// import livro from "../img/laranja/LIVRO_DIGITAL.jpg";
// import mapa from "../img/laranja/MAPA.jpg";
// import outros from "../img/laranja/OUTROS.jpg";
// import software from "../img/laranja/SOFTWARE.jpg";
// import texto from "../img/laranja/TEXTO.jpg";
// import video from "../img/laranja/VIDEO.jpg";
import Rating from '@material-ui/lab/Rating';
import StarBorderIcon from '@material-ui/icons/StarBorder';
// import AddIcon from '@material-ui/icons/CreateNewFolder';
// import Video from '@material-ui/icons/OndemandVideo';
// import MoreIcon from '@material-ui/icons/More';
import FavoriteIcon from '@material-ui/icons/Favorite';
import ButtonGuardarColecao from './ButtonGuardarColecao.js'
import Slide from '@material-ui/core/Slide';
import Grid from '@material-ui/core/Grid';
Lucas Eduardo Schoenfelder's avatar
Lucas Eduardo Schoenfelder committed
import {Link} from 'react-router-dom';
import {getDefaultThumbnail} from './HelperFunctions/getDefaultThumbnail'
import GetIconByName from './UploadPageComponents/GetIconByName'
import "./ResourceCard.css";
import {axiosPutRequest} from './HelperFunctions/getAxiosConfig'

export default function ResourceCardFunction (props) {
    const [thumbnail, setThumbnail] = useState(null)
    // eslint-disable-next-line
    const [label, setLabel] = useState(props.type)
    const [userAvatar, setUserAvatar] = useState(noAvatar)
    const [slideIn, setSlide] = useState(false)
    const controlSlide = () => {setSlide(!slideIn)}
    const [liked, toggleLiked] = useState(props.liked)
    const [likesCount, setLikesCount] = useState(props.likeCount)

    useEffect( () => {
        //decide which thumbnail to use
        if (props.thumbnail) {
            setThumbnail(`${apiDomain}` + props.thumbnail)
        }
        else {
            setThumbnail(getDefaultThumbnail(label))
        }

        if (props.avatar) {
            setUserAvatar(`${apiDomain}` + props.avatar)
        }

    }, [])

    function handleSuccessLike (data) {
        toggleLiked(!liked)
        setLikesCount(data.count)
    }
    const handleLike = () => {
        const url = `/learning_objects/${props.id}/like/`
        axiosPutRequest(url, {}, handleSuccessLike, (error) => {console.log(error)})
    const SlideAnimationContent = () => {
        return (
            <SlideContentDiv>
                <div style={{display:"flex", flex:"1"}}>{/*marginBottom:10px*/}
                    <AvatarDiv>
                        <img className="img" src={userAvatar} alt="user avatar"/>
                    </AvatarDiv>
                    <EnviadoPor>
                        Enviado por:
                        <br/>
                        <p>{props.author}</p>
                    </EnviadoPor>
                </div>
                <TagContainer>
                    <Grid container spacing={1} justify='safe' style={{height : "inherit"}}>
                    {
                        props.tags.map( (tag) =>
                            <Grid item key={tag.id}>
                                <span key={tag.id}>{tag.name}</span>
                            </Grid>
                        )
                    }
                    </Grid>
                </TagContainer>
            </SlideContentDiv>
        )
    }

    return (
        <StyledCard>
            <CardDiv>
                <CardReaDiv>
                    <Header onMouseEnter={controlSlide} onMouseLeave={controlSlide}>
                        {
                            <Slide  direction="left" in={slideIn} timeout={500}>
                                <div className={`slideContentLinkAfterActive${slideIn}`} style={{}}>
                                    <Link to={props.href} className="text" >
                                        {SlideAnimationContent()}
                                    </Link>

                                </div>
                        <div className={`slideContentLinkBeforeActive${slideIn}`} style={{}}>

                                <img className="img-cover" src={thumbnail} alt="learning object thumbnail"/>
                            </Link>
                        </div>
Lucas Eduardo Schoenfelder's avatar
Lucas Eduardo Schoenfelder committed
                        <Link to={props.href} className="text"> {/*add link to learningObject*/}
Lucas Eduardo Schoenfelder's avatar
Lucas Eduardo Schoenfelder committed
                        </Link>
                              value={props.rating}
                              precision={0.5}
                              style={{color:"#666"}}
                              emptyIcon={<StarBorderIcon fontSize="inherit" />}
                            />
                        }

                        <Footer>
                            <Type>
                                {GetIconByName(label)}
                                    <ButtonNoWidth onClick={handleLike}>
                                        <FavoriteIcon style={{color : liked ? "red" : "#666" }}/>
                                    </ButtonNoWidth>
                                </LikeCounter>
                            }
                        </Footer>
                    </Description>
                </CardReaDiv>

                {
                    props.published &&
                    <CardReaFooter>
                        <div style={{display : "flex", height : "100%"}}>
                            <ButtonGuardarColecao thumb={props.thumbnail} title={props.title} recursoId={props.id}
                                />
                        <ResourceCardOptions
                            learningObjectId={props.id}
                            downloadableLink = {props.downloadableLink}
                            thumb={props.thumbnail}
                            title={props.title}
                            />
                    </CardReaFooter>
                }
            </CardDiv>
        </StyledCard>
    )
}
/*---------- USED IN SLIDE DIV ONLY -----------*/
export const TagContainer = styled.div`
    display : flex;
    flex-direction : row;
    width : 100%;
    overflow : hidden;
    flex : 2;
    height : 120px;

    span {
        background-color : #fff;
        height : 20px;
        overflow : hidden;
        margin : 2px;
        padding : 3px 8px;
        border-radius : 10px;
        color : #666;
        font-size : 11px;
    }
`

export const EnviadoPor = styled.div`
    display : inline-block;
    padding-left : 10px;
    overflow : hidden;
    color : #fff;
    padding-right : 1em;

    p {
        text-overflow : ellipsis;
        overflow : hidden;
        white-space : nowrap;
        margin : 0;
    }
`
const AvatarDiv = styled.div`
    vertical-align : middle;
    border : 0;
    width : 45px;
        width : 45px;
        height : 40px !important;
        width : 40px !important;
        border : 0;
        vertical-align : middle;
        border-radius : 50%;
    }
`

const SlideContentDiv = styled.div`
    background-color : #ff9226;
    padding : 10px;
    width : 100%;
    height : 100%;
    // position : absolute;
    // display : flex;
    // flex-direction : column;
`
/*--------------------------------------------*/

const CardReaFooter = styled.div`
    height : 60px;
    display : flex;
    justify-content : space-between;
    border-top : 1px solid #e5e5e5;
    border-bottom : 1px solid #e5e5e5;
    align-items : center;
    padding : 0 15px 0 15px;
`

export const ButtonNoWidth = styled(Button)`
    max-width : 24px !important;
    width : 24px !important;
    min-width : 24px !important;
    max-height : 24px !important;
    padding : 0 !important;
    background-color : #fff !important;
    color : #a5a5a5 !important;
    border : 0 !important;

    .MuiButton-root {
        width : 24px !important;
        min-width : 12px !important;
    }

    .MuiSvgIcon-root {
        padding-right : 0 !important;
    }

    .MuiButton-label {
        padding-left : 4px !important;
    }
`

export const LikeCounter = styled.div`
    font-size : 14px;

    .btn-like {
        padding : 0 !important;
        background-color : #fff !important;
        border : 0 !important;
        min-width : min-content;
    }

    .MuiSvgIcon-root {
        font-size : 21px;
        vertical-align : middle;
        padding-right : .4em;
    }
`

const Type = styled.div`
    line-height : 1;

    .icon {
        height : 27px;
        width : 27px;
        vertical-align : middle
        align-self : center;

        .st1 {
            fill : #ff7f00;
        }
    }
`

export const Footer = styled.div`
    display : flex;
    flex-direction : row;
    justify-content : space-between;
`

const Description = styled.div`
    display : flex;
    flex : 1;
    background-color : #fff;
    flex-direction : column;
    justify-content: space-between;
    padding : 15px;

    a {
        text-decoration : none !important;
        color : inherit;
    }

    .title {
        text-overflow : ellipsis;
        overflow : hidden;
        margin-bottom : 10px;
        font-size : 1.2em;
        line-height : 1.1;
        max-height : 46px;
        color : #666;
    }
`

export const Header = styled.div`
    display : flex;
    flex : 2;
    position : relative;
    overflow : hidden;
`

export const CardReaDiv = styled.div`
    display : flex;
    flex-direction : column;
    height : 320px;
    width : 272.5px;
    margin : 0 auto;

    .img-cover {
        background-color : #e5e5e5;
        height : 100%;
        object-fit : cover;
        overflow : hidden;
        display : block;
        background-position : center;
        background-size : cover;
        width : 100%;
    }
`

export const CardDiv = styled.div`
    background-color : #fff;
    text-align : start;
    font-family : 'Roboto', sans serif;
    color : #666;
`

export const StyledCard = styled(Card)`
    width : 272.5px;
    max-height : 380px;
    margin-top : 10px;
    margin-bottom : 10px;
    max-width : 345px;
    border-radius : 0;
    box-shadow : 0 0 5px 0 rgba(0,0,0,.25);
`