From fdd7f93d08100b75e04e785640742ea624ed4f9b Mon Sep 17 00:00:00 2001 From: Angelo Raimondi Date: Thu, 15 Jul 2021 23:57:13 -0300 Subject: [PATCH] default picture and formik component --- package.json | 2 +- src/components/FormikComponent.js | 260 +++++++++++++++++ .../ListarFuncionarios/ListarFuncionarios.js | 2 +- .../NovoFuncionario/NovoFuncionario.js | 268 +----------------- 4 files changed, 276 insertions(+), 256 deletions(-) create mode 100644 src/components/FormikComponent.js diff --git a/package.json b/package.json index 6730357..1e68026 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ }, "scripts": { "start": "react-scripts start", - "build": "react-scripts build", + "build": "react-scripts build && echo '/* /index.html 200' | cat >build/_redirects", "test": "react-scripts test", "eject": "react-scripts eject" }, diff --git a/src/components/FormikComponent.js b/src/components/FormikComponent.js new file mode 100644 index 0000000..0effe4d --- /dev/null +++ b/src/components/FormikComponent.js @@ -0,0 +1,260 @@ +import React from "react"; +import { InputFeedback, NovoFuncionarioSchema } from "./InputFeedback"; +import { Formik, Form, Field, ErrorMessage } from "formik"; +import api from "../api/axios.config"; + +function FormikComponent(props) { + return ( +
+ { + setSubmitting(true); + try { + let uploadedImageUrl = ""; + if (values.image) { + uploadedImageUrl = await props.handleFileUpload(values.image); + } + const valuesCopy = { ...values }; + delete valuesCopy.image; + let image_url = ""; + if (uploadedImageUrl) { + image_url = uploadedImageUrl; + } else { + image_url = + "https://res.cloudinary.com/angeloraimondi/image/upload/v1626280658/actionsys/file_li7mxl.png"; + } + + await api.post("/funcionario", { + ...valuesCopy, + image_url: image_url, + }); + setSubmitting(false); + + props.history.push("/"); + } catch (err) { + console.error(err); + setSubmitting(false); + } + }} + > + {({ isSubmitting, errors, touched, setFieldValue }) => ( +
+

Cadastre Novo Funcionario

+

Dados Pessoais do Funcionario

+
+
+
+ +
+ + setFieldValue("image", event.target.files[0]) + } + /> +
+
+ + + ( + + {msg} + + )} + /> +
+
+ + + ( + + {msg} + + )} + /> +
+
+ + + ( + + {msg} + + )} + /> +
+

Data de Admissão e Cargo

+
+
+ + + ( + + {msg} + + )} + /> +
+
+ + + + {props.setores.map((setor, i) => ( + + ))} + + ( + + {msg} + + )} + /> +
+
+ + + + {props.cargos.map((cargo, i) => ( + + ))} + + ( + + {msg} + + )} + /> +
+
+ + + + {props.niveis.map((nivel, i) => ( + ))} + + ( + + {msg} + + )} + /> +
+
+ +
+
+ )} +
+
+ ); +} + +export default FormikComponent; diff --git a/src/components/ListarFuncionarios/ListarFuncionarios.js b/src/components/ListarFuncionarios/ListarFuncionarios.js index fc9a090..33eb99a 100644 --- a/src/components/ListarFuncionarios/ListarFuncionarios.js +++ b/src/components/ListarFuncionarios/ListarFuncionarios.js @@ -62,7 +62,7 @@ function ListarFuncionarios() { {funcionarios && funcionarios.map((funcionario, i) => ( - + ))} diff --git a/src/components/NovoFuncionario/NovoFuncionario.js b/src/components/NovoFuncionario/NovoFuncionario.js index 84f3434..6890daa 100644 --- a/src/components/NovoFuncionario/NovoFuncionario.js +++ b/src/components/NovoFuncionario/NovoFuncionario.js @@ -1,20 +1,19 @@ import { useHistory } from "react-router-dom"; -import { Formik, Form, Field, ErrorMessage } from "formik"; - +import FormikComponent from "../FormikComponent"; import api from "../../api/axios.config"; -import { InputFeedback, NovoFuncionarioSchema } from "../InputFeedback"; function NovoFuncionario() { const history = useHistory(); - + const setores = ["auxiliar", "técnico", "engenheiro", "diretor"] + const cargos = ["engenharia", "compras", "vendas", "financeiro"] + const niveis = ["junior", "pleno", "senior", "estagiario"] async function handleFileUpload(file) { try { - const uploadData = new FormData(); uploadData.append("image", file); const response = await api.post("/image-upload", uploadData); - + return response.data.fileUrl; } catch (err) { console.error(err); @@ -22,254 +21,15 @@ function NovoFuncionario() { } return ( - { - setSubmitting(true); - - try { - - let uploadedImageUrl = ""; - if (values.image) { - uploadedImageUrl = await handleFileUpload(values.image); - } - - await api.post("/funcionario", { - ...values, - image_url: uploadedImageUrl, - image: "", - }); - - setSubmitting(false); - - history.push("/"); - } catch (err) { - console.error(err); - setSubmitting(false); - } - }} - > - {({ isSubmitting, errors, touched, setFieldValue }) => ( -
-

Cadastre Novo Funcionario

-

Dados Pessoais do Funcionario

-
-
-
- - setFieldValue("image", event.target.files[0])} - /> -
-
- - - ( - - {msg} - - )} - /> -
- -
- - - ( - - {msg} - - )} - /> -
- -
- - - ( - - {msg} - - )} - /> -
- -

Data de Admissão e Cargo

-
- -
- - - ( - - {msg} - - )} - /> -
- -
- - - - - - - - - - ( - - {msg} - - )} - /> -
- -
- - - - - - - - - ( - - {msg} - - )} - /> -
- -
- - - - - - - - - - ( - - {msg} - - )} - /> -
- -
-
- -
-
- )} -
+
+ +
); }