From 85a7c9c7359a1ca233dc2aa097c6642d1328fdc5 Mon Sep 17 00:00:00 2001 From: Luis Felipe Risch <lfr20@inf.ufpr.br> Date: Tue, 3 Nov 2020 12:37:44 -0300 Subject: [PATCH] File to create new language --- .../Components/Inputs/CreateLanguage.js | 220 ++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 src/Admin/Components/Components/Inputs/CreateLanguage.js diff --git a/src/Admin/Components/Components/Inputs/CreateLanguage.js b/src/Admin/Components/Components/Inputs/CreateLanguage.js new file mode 100644 index 00000000..c7edca9b --- /dev/null +++ b/src/Admin/Components/Components/Inputs/CreateLanguage.js @@ -0,0 +1,220 @@ +/*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 } from 'react'; +//imports material ui componets +import Card from "@material-ui/core/Card"; +import CardContent from "@material-ui/core/CardContent"; +import CardAction from '@material-ui/core/CardActions'; +import { Typography, TextField, Button, Grid } from '@material-ui/core'; +import CircularProgress from '@material-ui/core/CircularProgress'; +import AddRoundedIcon from '@material-ui/icons/AddRounded'; +import ListRoundedIcon from '@material-ui/icons/ListRounded'; +//imports local files +import { apiUrl } from '../../../../env'; +import SnackBar from '../../../../Components/SnackbarComponent'; +//imports services +import { Create } from '../../../Services'; +import { Url } from '../../../Filters'; + +const CreateLanguage = (props) => { + const [name , setName] = useState('Nova linguagem'); + const [code , setCode] = useState(''); + + const [isLoading, setIsLoading] = useState(false) + + // Handle error in name + const [errorInName, setErrorInName] = useState({ + error: false, + message: '', + }) + + // Handle error in Country + const [errorInCode, setErrorInCode] = useState({ + error: false, + message: '', + }) + + const [snackInfo, setSnackInfo] = useState({ + message: '', + icon: '', + open: false, + color: '', + }) + + const NameHandler = (e) => { + setName(e.target.value) + if (errorInName.error) { + setErrorInName({ + error: false, + message: '' + }) + } + } + + const CodeHandler = (e) => { + if (errorInCode.error) { + setErrorInCode({ + error: false, + message: '' + }) + } + setCode(e.target.value) + } + + // verify if the given text is empty + const isEmpty = (text) => { + return text.length === 0 ? true : false; + } + + // Handle snack infos + const HandleSnack = (message, state, icon, color) => { + setSnackInfo({ + message: message, + icon: icon, + open: state, + color: color + }) + } + + //Handle submit + async function onSubmit() { + setIsLoading(true) + if (!isEmpty(name) && !isEmpty(code)) { + const api = apiUrl + '/languages' + const body = { + "language": { + 'name': name, + 'code': code, + } + } + Create(api, body).then(res => { + if (res) { + HandleSnack('A linguagem foi criada com sucesso', true, 'success', '#228B22') + } else { + HandleSnack('Ocorreu algum erro', true, 'warning', '#FA8072') + } + setIsLoading(false) + }) + } else { + HandleSnack('Você precisa preencher algumas informações obrigatórias', true, 'warning', '#FFC125') + if (isEmpty(name)) { + setErrorInName({ + error: true, + message: 'Esse campo está vazio' + }) + } + if (isEmpty(code)) { + setErrorInCode({ + error: true, + message: 'Esse campo está vazio' + }) + } + setIsLoading(false) + } + } + + // Fields + const fields = [ + { + label: 'Nome', + value: name, + required: true, + error: errorInName.error, + errorMessage: errorInName.message, + onChange: (event) => NameHandler(event) + }, + { + label: 'Código', + value: code, + required: true, + error: errorInCode.error, + errorMessage: errorInCode.message, + onChange: (event) => CodeHandler(event) + } + ] + + return ( + <Card variant='outlined'> + <SnackBar + severity={snackInfo.icon} + text={snackInfo.message} + snackbarOpen={snackInfo.open} + color={snackInfo.color} + handleClose={() => setSnackInfo({ + message: '', + icon: '', + open: false, + color: '' + })} + /> + <CardContent> + <Grid container direction='row' justify='space-between'> + <Typography variant='h4'> + {name} + </Typography> + <Button + onClick={props.BackToList} + startIcon={<ListRoundedIcon />} + variant='outlined' + color='primary' + > + Listar + </Button> + </Grid> + + <div style={{height : '1em'}}></div> + + <form style={{ display: 'flex', flexDirection: 'column' }}> + {fields.map((field, index) => ( + <TextField + key={index} + required={field.required} + error={field.error} + helperText={field.error ? field.errorMessage : ''} + style={{ width: '250px', marginBottom: '1em' }} + label={field.label} + value={field.value} + onChange={field.onChange} + type="search" + multiline={true} + /> + ))} + </form> + </CardContent> + <CardAction> + <Button + onClick={() => { + onSubmit(); + setTimeout(function(){props.UpdateList(Url('languages', '', '0', 'DESC'))}, 3000) + }} + variant="contained" + color="primary" + disabled={isLoading} + startIcon={isLoading ? null : <AddRoundedIcon />} + > + { + isLoading ? <CircularProgress size={24} /> : 'Adicionar' + } + </Button> + </CardAction> + </Card> + ); +} + +export default CreateLanguage; \ No newline at end of file -- GitLab