Skip to content
Snippets Groups Projects
GuardarModal.js 14.3 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/>.*/

lfr20's avatar
lfr20 committed
import React, { useContext, useState } from 'react';
import { Button } from '@material-ui/core';
import Modal from '@material-ui/core/Modal';
import Backdrop from '@material-ui/core/Backdrop';
import Fade from '@material-ui/core/Fade';
lfr20's avatar
lfr20 committed
import styled from 'styled-components' 
import { Store } from '../Store.js'
import { apiDomain } from '../env';
import CloseIcon from '@material-ui/icons/Close';
import PublicIcon from '@material-ui/icons/Public';
import LockIcon from '@material-ui/icons/Lock';
import LoadingSpinner from './LoadingSpinner.js'
import CriarColecaoForm from './CriarColecaoForm.js'
import SnackbarComponent from './SnackbarComponent'
lfr20's avatar
lfr20 committed
import { getRequest, postRequest } from './HelperFunctions/getAxiosConfig'
lfr20's avatar
lfr20 committed
function CloseModalButton(props) {
    return (
        <StyledCloseModalButton onClick={props.handleClose}>
lfr20's avatar
lfr20 committed
            <CloseIcon />
export default function GuardarModal(props) {
lfr20's avatar
lfr20 committed
    const { state } = useContext(Store)
    const [collsArr, setcolls] = useState([])
    const [loading, toggleLoading] = useState(true)
    const [creatingCol, setCreating] = useState(false)
lfr20's avatar
lfr20 committed
    const [snackbarOpen, setSnackOpen] = useState({
        open: false,
        severity: "",
        text: "",
        color: ""
    })
lfr20's avatar
lfr20 committed
    function handleSuccessGetCols(data) {
lfr20's avatar
lfr20 committed

    function handleError(error) {
        toggleLoading(false)
        setSnackOpen({
            open: true,
            severity: "error",
            text: "Error ao guardar o recurso",
            color: "red",
        })
    }

    const getCols = () => {
        if (collsArr.length === 0) {
            const id = state.currentUser.id

            const url = `/users/${id}/collections/`

lfr20's avatar
lfr20 committed
            getRequest(url, handleSuccessGetCols, handleError)
lfr20's avatar
lfr20 committed
    function handleSuccessPostToCol(data) {
        setSnackOpen({
            open: true,
            severity: "info",
            text: "O Recurso foi guardado na coleção!",
            color: ""
        })
lfr20's avatar
lfr20 committed

        const url = `/collections/${colId}/items`
lfr20's avatar
lfr20 committed
            "collection": {
                "items": [{ "id": props.recursoId, "type": "LearningObject" }]
lfr20's avatar
lfr20 committed
        postRequest(url, payload, handleSuccessPostToCol, handleError)
lfr20's avatar
lfr20 committed
        <React.Fragment>
            <SnackbarComponent
                snackbarOpen={snackbarOpen.open}
                severity={snackbarOpen.severity}
                handleClose={() => {
                    setSnackOpen({
                        open: false,
                        severity: "",
                        text: "",
                        color: ""
                    })
                }}
                text={snackbarOpen.text}
                color={snackbarOpen.color}
lfr20's avatar
lfr20 committed
            <StyledModal
                aria-labelledby="transition-modal-title"
                aria-describedby="transition-modal-description"
                open={props.open}

                centered="true"
                onClose={props.handleClose}
                closeAfterTransition
                BackdropComponent={Backdrop}
                BackdropProps={{
                    timeout: 500,
                }}
                onRendered={() => { getCols() }}
            >
                <Fade in={props.open}>
                    <Container>
                        <Header>
                            <span style={{ width: "32px" }} />
                            <h2>Guardar recurso</h2>
                            <CloseModalButton handleClose={props.handleClose} />
                        </Header>
                        <Content style={{ paddingTop: "0" }}>
                            <ResourceInfo>
                                <img src={props.thumb ? apiDomain + props.thumb : require('../img/logo_small.svg')} alt="thumbnail recurso" />
lfr20's avatar
lfr20 committed
                                <div className="text">
                                    <h3>{props.title}</h3>
                                </div>
                            </ResourceInfo>
                            {
                                loading ?
                                    (
                                        <LoadingSpinner text="Carregando coleções" />
                                    )
                                    :
                                    (
                                        <ChooseColContainer>
                                            {
                                                creatingCol ?
                                                    (
                                                        <CriarColecaoForm
                                                            handleClose={() => setCreating(false)}
                                                            finalize={postToCol}
                                                        />
                                                    )
                                                    :
                                                    (
                                                        collsArr.length === 0 ?
                                                            (
                                                                <>
                                                                    <div classname="no-cols">
                                                                        <h2>Você não possui coleções ainda.</h2>
                                                                    </div>
lfr20's avatar
lfr20 committed
                                                            )
                                                            :
                                                            (
                                                                <>
                                                                    <ChooseCol>
                                                                        <h2>Escolha uma Coleção: </h2>
                                                                        <div className="cols-list">
lfr20's avatar
lfr20 committed
                                                                                collsArr.map(collection =>
                                                                                    <div className="row" key={collection.id}>
                                                                                        <div style={{ display: "flex", flexDirection: "row", justifyContent: "space-between", alignItems: "center" }}>
                                                                                            {
                                                                                                collection.privacy === "public" ?
                                                                                                    <PublicIcon className="icon" />
                                                                                                    : <LockIcon className="icon" />
                                                                                            }
                                                                                            <h5>{collection.name}</h5>
                                                                                        </div>
                                                                                        <GuardarBotao onClick={() => { postToCol(collection.id) }}>GUARDAR</GuardarBotao>
                                                                                    </div>
                                                                                )
lfr20's avatar
lfr20 committed
                                                                    </ChooseCol>
                                                                </>
                                                            )

                                                    )
                                            }
                                        </ChooseColContainer>
lfr20's avatar
lfr20 committed
                                    )
                            }                                     
                            <div style={{ display: "flex", justifyContent: "center" }}>
                                <CriarColButton onClick={() => { setCreating(true) }}>CRIAR COLEÇÃO</CriarColButton>
                            </div>
lfr20's avatar
lfr20 committed
                        </Content>
                    </Container>
                </Fade>
            </StyledModal>
        </React.Fragment>
    )
}

const GuardarBotao = styled(Button)`
    &:hover {
        background-color : #503096 !important;
    }
    max-height : 36px !important;
    background-color : #673ab7 !important;
    color : #fff !important;
    box-shadow : 0 2px 5px 0 rgba(0,0,0,.26) !important;
    padding-left : 32px !important;
    padding-right : 32px !important;
`

const ChooseCol = styled.div`
    display : flex;
    flex-direction : column;
    h2 {
        margin-top : 20px;
        margin-bottom : 10px;
        font-size : 24px;
        font-weight : 500;
    }

    .icon {
        vertical-align : middle !important;
        font-weight : normal !important;
        font-style : normal !important;
        font-size : 24px !important;
        line-height : 1 !important;
        letter-spacing : normal !important;
        text-transform : none !important;
        display : inline-block !important;
        white-space : nowrap !important;
        word-wrap : normal !important;
        direction : ltr !important;
        padding-right : 2px;
    }

    .cols-list {
        overflow-x : hidden;
        overflow-y : auto;

        .row {
            display : flex;
            flex-direction : row;
            padding : 10px;
            border-radius : 5px;
            max-height : 60px;
            align-items : center;
            align-content : center;
            max-width : 100%;
            justify-content : space-between;

            h5 {
                padding : 0 20px;
                margin : 0;
                font-size : 14px;
                font-weight : 600;
            }
        }
    }
`

const CriarColButton = styled(Button)`
    width : 200px !important;
    margin-top : 16px !important;
    background-color : #673ab7 !important;
    margin-left : auto !important;
    margin-right : auto !important;
    color : #fff !important;
    font-weight : 600 !important;
    box-shadow : !important;
    padding-left : 0 2px 5px 0 rgba(0,0,0,.26) 16px !important;
    padding-right : 16px !important;
`

const ChooseColContainer = styled.div`
    display : flex;
    flex-direction : column;
    color : #666;

    .no-cols {
        align-self : center;
        margin-top : 20px;
        margin-bottom : 30px;
        font-size : 24px;
        font-weight : 500;
    }
`

const ResourceInfo = styled.div`
    margin-top : 0;
    background-color : #f4f4f4;
    overflow : hidden;
    border-radius : 5px;
    display : flex;
    align-items : center;
    align-content : center;
    max-wdith : 100%;
    justify-content : space-between;

    .text {
        max-height : 100%;
        max-width : 66.66%;
        display : flex;
        flex-direction : column;
        text-align : center;

        h3 {
            font-size : 20px;
            font-weight : 500;
            padding : 10px;
        }
    }

    img {
        object-fit : cover;
        height : 115px;
        max-width : 165px;
        background-color : #e5e5e5;
        float : left;
        padding : 0;

        @media screen and (min-width : 600px) {
            margin-right : 20px;
            margin-bottom : 0;
        }
        @media screen and (max-width : 768px) {
            width : 100%;
        }
    }

`

const Content = styled.div`
    padding : 20px 30px;
    overflow-y : scroll;

`

const Header = styled.div`
    display : flex;
    flex-direction : row;
    padding : 10px 26px 0 26px;
    align-items : center;
    justify-content : space-between;
    height : 64px;

    h2 {
        font-size : 26px;
        font-weight : lighter;
        color : #666
    }
`

const StyledCloseModalButton = styled(Button)`
    display : inline-block;
    position : relative;
    float : right !important;
    margin-right : -8px !important;
    background : transparent !important;
    min-width: 0 !important;
    width : 40px;
`

const StyledModal = styled(Modal)`
    .djXaxP{
        margin : 0 !important;
    }
    display : flex;
    align-items: center;
    justify-content : center;
    text-align : center;
    padding : 10px !important;
    max-width : none;
    max-height : none;
`

const Container = styled.div`
    box-sizing : border-box;
    box-shadow : 0 7px 8px -4px rgba(0,0,0,.2),0 13px 19px 2px rgba(0,0,0,.14),0 5px 24px 4px rgba(0,0,0,.12);
    background-color : white;
    align : center;
    display : flex;
    flex-direction : column;
    min-width : 240px;
    max-height : none;
    position : relative;
    padding : 10px;
    border-radius : 4px;

    @media screen and (min-width : 700px) {
        max-width : 600px;
        max-height : 600px;
    }

    @media screen and (max-width : 699px) {
        width : 100%;
        height : 100%;
    }
`