Skip to content
Snippets Groups Projects
ModalAvaliarRecurso.js 12.9 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, { useState, useEffect } 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';
import styled from 'styled-components'
import Grid from '@material-ui/core/Grid';
import RadioGroup from '@material-ui/core/RadioGroup';
import Radio from '@material-ui/core/Radio';
import FormControl from '@material-ui/core/FormControl';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import CloseModalButton from './CloseModalButton'
import Snackbar from '@material-ui/core/Snackbar';
import { withStyles } from '@material-ui/core/styles';
lfr20's avatar
lfr20 committed
import { getRequest } from './HelperFunctions/getAxiosConfig'
import LoadingSpinner from '../Components/LoadingSpinner'
vgm18's avatar
vgm18 committed
import {StyledTextField, useStyles} from './ReportUserForm.js'

const StyledRadio = withStyles({
lfr20's avatar
lfr20 committed
    root: {
        color: '#666',
        '&$checked': {
            color: '#ff7f00',
        },
lfr20's avatar
lfr20 committed
    checked: {},
})((props) => <Radio color="default" {...props} />);
lfr20's avatar
lfr20 committed
export default function ModalAvaliarRecurso(props) {
vgm18's avatar
vgm18 committed
    const classes = useStyles();

lfr20's avatar
lfr20 committed
    const [options, setOptions] = useState([])
lfr20's avatar
lfr20 committed
    const [avaliacao, setAvaliacao] = useState([null, null, null, null])
    const handleRadios = (event, id) => {
        let newValue = avaliacao
        newValue[id - 1] = (event.target.value === "Sim" ? true : false)
        setAvaliacao(newValue)

lfr20's avatar
lfr20 committed
        if (avaliacao.indexOf(null) === -1) {
            toggleDisableButton(false)
        }
    }

    const [justificativa, setJustificativa] = useState("")
    const handleChangeJustificativa = (e) => {
        setJustificativa(e.target.value)
    }

    const [snackbarCancelar, toggleSnackbarCancelar] = useState(false)
    const handleSnackbarCancelar = (value) => toggleSnackbarCancelar(value)

    const handleCancel = () => {
        handleSnackbarCancelar(true)

        props.handleClose()
    }

    const [buttonDisabled, toggleDisableButton] = useState(true)

    const handleAvaliar = () => {
        let criteria = options
        avaliacao.map((criterium, index) =>
lfr20's avatar
lfr20 committed
            criteria[index]['accepted'] = !criterium
        props.confirm(criteria, justificativa, avaliacao.filter((item) => item === true).length > 0 ? false : true)
        returnToDefault()
    }

    const returnToDefault = () => {
lfr20's avatar
lfr20 committed
        setAvaliacao([null, null, null, null])
        setJustificativa('')
        toggleDisableButton(true)
lfr20's avatar
lfr20 committed
    useEffect(() => {
        getRequest(
            "/questions",
            (data, header) => {
                const cpyOption = []
                const cpyAvaliacao = []
                for (let index = 0; index < data.length; index++) {
                    const option = data[index];
                    cpyOption.push({
                        text: option.description,
                        id: option.id
                    })
                    cpyAvaliacao.push(null)
                }
                setOptions(cpyOption)
                setAvaliacao(cpyAvaliacao)
            },
            (error) => {

            }
        )
    }, [])

lfr20's avatar
lfr20 committed
            <Snackbar open={snackbarCancelar} autoHideDuration={1000} onClose={toggleSnackbarCancelar}
                anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
                message="Você cancelou a avaliação deste recurso."
lfr20's avatar
lfr20 committed
            {
                options ?
                    <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,
                        }}
                    >

                        <Fade in={props.open}>
vgm18's avatar
vgm18 committed
                            <Container contrast={props.contrast}>
                                <Header contrast={props.contrast}>
lfr20's avatar
lfr20 committed
                                    <h2>Você está avaliando o recurso
vgm18's avatar
vgm18 committed
                                        <span style={{ fontWeight: "bolder" }}> {props.title}</span>
lfr20's avatar
lfr20 committed
                                    </h2>
vgm18's avatar
vgm18 committed
                                    <CloseModalButton contrast={props.contrast} handleClose={props.handleClose} />
lfr20's avatar
lfr20 committed
                                </Header>

                                <Content>
                                    <Grid container>
lfr20's avatar
lfr20 committed
                                        <Grid item xs={12}>
                                            <p>Esse recurso está em conformidade com os critérios? Marque as opções e justifique aquela(s) que não estiverem em conformidade.</p>
                                        </Grid>

                                        <FormControl component="fieldset" style={{ display: "BlockRuby" }} fullWidth={true}>
                                            {
                                                options.map((option) =>
                                                    <Grid item xs={12} key={option.id} style={{ paddingTop: "10px" }}>
                                                        <Grid container>
                                                            <Grid item xs={10}>
                                                                <div style={{ height: "100%", display: "flex", alignItems: "center" }}>
                                                                    <p>{option.text}</p>
                                                                </div>
                                                            </Grid>
                                                            <Grid item xs={2}>
                                                                <RadioGroup row onChange={(e) => { handleRadios(e, option.id) }}>
vgm18's avatar
vgm18 committed
                                                                    <FormControlLabel className={`${props.contrast}LinkColor`} value={"Sim"} control={<StyledRadio style={props.contrast === "" ? {} : {color: "white"}}/>} label="Não" />
                                                                    <FormControlLabel className={`${props.contrast}LinkColor`} value={"Não"} control={<StyledRadio style={props.contrast === "" ? {} : {color: "white"}}/>} label="Sim" />
lfr20's avatar
lfr20 committed
                                                                </RadioGroup>
                                                            </Grid>
                                                        </Grid>
                                                    </Grid>
                                                )
                                            }
                                        </FormControl>

                                        <Grid item xs={12}>
                                            <FormControl style={{ width: "100%", height: "100px" }}>
                                                <StyledTextField
vgm18's avatar
vgm18 committed
                                                    contrast={props.contrast}
lfr20's avatar
lfr20 committed
                                                    id={"title-form"}
                                                    label={"Justificativa (opcional)"}
                                                    type={"text"}
                                                    value={justificativa}
vgm18's avatar
vgm18 committed
                                                    InputProps={props.contrast === "" ? { className: classes.lightTextField } : { className: classes.darkTextField }}
lfr20's avatar
lfr20 committed
                                                    onChange={e => { handleChangeJustificativa(e) }}
                                                    multiline
                                                    fullWidth
                                                />
                                            </FormControl>
                                        </Grid>

                                        <Grid item xs={12}>
                                            <ButtonsDiv>
vgm18's avatar
vgm18 committed
                                                <ButtonEnviarAvaliar contrast={props.contrast} disabled={buttonDisabled} onClick={() => { handleAvaliar() }}>ENVIAR AVALIAÇÂO</ButtonEnviarAvaliar>
                                                <GreyButton contrast={props.contrast} onClick={() => { handleCancel() }}>CANCELAR</GreyButton>
lfr20's avatar
lfr20 committed
                                            </ButtonsDiv>
                                        </Grid>
lfr20's avatar
lfr20 committed
                                </Content>

                            </Container>
                        </Fade>
                    </StyledModal>
                    :
                    <LoadingSpinner />
            }
        </>
    )
}

const Content = styled.div`
vgm18's avatar
vgm18 committed
    padding : 0 30px 20px 30px;
    overflow : visible;
    max-width : 100%;
    font-size : 16px;

    .main-content-text {
        font-size : 15px;
        line-height : 22px;
        text-align : justify;
    }

    p {
        text-align : start;
        margin : 0 0 10px;
    }
`

const Header = styled.div`
    display : flex;
    flex-direction : row;
vgm18's avatar
vgm18 committed
    padding : 20px 20px 20px 20px;
    align-items : center;
    justify-content : space-between;
vgm18's avatar
vgm18 committed
    color: ${props => props.contrast === "" ? "#666" : "white"} !important;

    @media screen and (min-width : 990px) {
        height : 64px;
    }
    @media screen and (max-width : 989px) {
        max-height : none;
    }

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

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);
vgm18's avatar
vgm18 committed
    background-color: ${props => props.contrast === "" ? "#fff" : "black"};
    border: ${props => props.contrast === "" ? "" : "1px solid white"} !important;
    color: ${props => props.contrast === "" ? "#666" : "white"};
    align : center;
    display : flex;
    flex-direction : column;
    min-width : 240px;
vgm18's avatar
vgm18 committed
    max-height : 90%;
    position : relative;
    border-radius : 4px;
vgm18's avatar
vgm18 committed
    max-width : 100%;
vgm18's avatar
vgm18 committed
    overflow-y : scroll;

const ButtonsDiv = styled.div`
    display : flex;
    max-width : 100%;
    text-align : start;
    align-items : center;
    padding : 0 0 20px 0;

    @media screen and (min-width : 990px) {
        flex-direction : row;
    }
    @media screen and (max-width : 989px) {
        flex-direction : column;
    }
`
const GreyButton = styled(Button)`
    &:hover {
vgm18's avatar
vgm18 committed
        background-color : ${props => props.contrast === "" ? "rgba(158,158,158,0.2)" : ""} !important;
    }
    max-height : 36px !important;

    background-color : transparent !important;
vgm18's avatar
vgm18 committed
    color: ${props => props.contrast === "" ? "#666" : "yellow"} !important;
    text-decoration : ${props => props.contrast === "" ? "none" : "underline"} !important;
    outline : none !important;
    text-align : center !important;
    font-weight : 600 !important;


`

const ButtonEnviarAvaliar = styled(Button)`
vgm18's avatar
vgm18 committed
    color : ${props => props.contrast === "" ? `${props.disabled ? `rgba(0,0,0,0.38)` : `rgba(255,255,255,0.87)`}` : `${props.disabled ? `white` : `yellow`}`} !important;
    box-shadow : ${(props) => props.disabled ? "none !important" : "0 2px 5px 0 rgba(0,0,0,.26) !important"};
    font-weight : 600 !important;
vgm18's avatar
vgm18 committed
    background-color : ${props => props.contrast === "" ? `${props.disabled ? "#e9e9e9 !important" : "#ff7f00 !important"}` : "black !important"};
    text-decoration: ${props => props.contrast === "" ? "none" : "underline"} !important;
    border: ${props => props.contrast === "" ? "" : "1px solid white"} !important;
    margin-left : 8px !important;
    margin-right : 8px !important;

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