Skip to content

Commit

Permalink
Merge pull request #85 from fga-eps-mds/187-edicao-de-departamentos
Browse files Browse the repository at this point in the history
187 edicao de departamentos
  • Loading branch information
ericoBandeira authored Nov 10, 2021
2 parents 2c222a2 + 99e868a commit 60f2089
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Components/Departments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const Departments = ({ departments, searchTerm }) => {
}
})
.map((post) => (
<PocketDepartment key={post.id} name={post.name} />
<PocketDepartment key={post.id} id={post.id} name={post.name} />
))}
</StyledListGroup>
);
Expand Down
12 changes: 10 additions & 2 deletions src/Components/PocketDepartment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import React from "react";

import { StyledBigDiv } from "./styles";

const PocketDepartment = ({ name }) => (
const PocketDepartment = ({ name, id }) => (
<StyledBigDiv>
<button class="registerNumber">{name}</button>
<button
onClick={(e) => {
e.preventDefault();
window.location.href = `/editar-departamento/${id}`;
}}
class="registerNumber"
>
{name}
</button>
</StyledBigDiv>
);

Expand Down
93 changes: 93 additions & 0 deletions src/Pages/EditDepartment/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, { useState } from "react";
import toast, { Toaster } from "react-hot-toast";
import { BiBuildings } from "react-icons/bi";
import HeaderWithButtons from "../../Components/HeaderWithButtons";
import {
editDepartmentById,
getDepartments,
} from "../../Services/Axios/profileService";

import {
StyledBlueRectangle,
StyledButtonsDiv,
StyledBackButton,
StyledRegisterButton,
StyledForms,
StyledViewProfile,
StyledWhiteRectangle,
} from "../CreateDepartment/styles.js";
import { useParams } from "react-router";

const EditDepartment = () => {
const { id } = useParams();
const [department, setDepartment] = useState("");

async function setAll() {
const allDepart = await getDepartments();

for (const element of allDepart) {
console.log(element);
if (element.id == id) {
setDepartment(element.name);
break;
}
}
}

window.onload = function () {
setAll();
};

async function handleClick(event) {
return editDepartmentById(department, id, toast);
}

return (
<>
<HeaderWithButtons />
<div>
<Toaster />
<StyledViewProfile>
{/* Create Department image */}
<StyledBlueRectangle>
<BiBuildings size="20rem" color="white" />
</StyledBlueRectangle>

<StyledWhiteRectangle>
<StyledForms>
<form>
<div>
<h1>Editar Departamento</h1>
<input
id="email"
type="text"
required
placeholder="Nome do departamento"
value={department}
onChange={(event) => setDepartment(event.target.value)}
/>
</div>
</form>
</StyledForms>
<StyledButtonsDiv>
<StyledBackButton
type="button"
onClick={() => window.history.back()}
>
Voltar
</StyledBackButton>
<StyledRegisterButton
type="button"
onClick={(event) => handleClick(event)}
>
Editar
</StyledRegisterButton>
</StyledButtonsDiv>
</StyledWhiteRectangle>
</StyledViewProfile>
</div>
</>
);
};

export default EditDepartment;
7 changes: 7 additions & 0 deletions src/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import ViewAllFields from "./Pages/ViewAllFields";
import CreateDepartment from "./Pages/CreateDepartment";
import ViewAllUsers from "./Pages/ViewAllUsers";
import EditRecord from "./Pages/EditRecord";
import EditDepartment from "./Pages/EditDepartment";
import GenericBlueButton from "./Components/GenericBlueButton";


const PrivateRoutes = ({ component: Component, ...prop }) => (
<Route
{...prop}
Expand Down Expand Up @@ -112,6 +114,11 @@ const Routes = () => (
path="/editar-registro/:id"
component={() => <EditRecord />}
/>
<PrivateRoutes
exact
path="/editar-departamento/:id"
component={() => <EditDepartment />}
/>
<Route exact path="/" component={() => <LoginScreen />} />
</Switch>
</BrowserRouter>
Expand Down
24 changes: 24 additions & 0 deletions src/Services/Axios/profileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,27 @@ export async function registerSection(name, toast) {
toast.error("Não foi possível cadastrar o departamento!");
}
}

export async function editDepartmentById(departmentInfo, id, toast) {
try {
// Edit record with the id and the new information
const temp = await APIProfile.post(`/departments/change-department/${id}`, {
name: departmentInfo,
});
toast.success((t) => (
<span style={{ textAlign: "center" }}>
<p>Departamento editado com sucesso!</p>
</span>
));
return temp;
} catch (err) {
const status = err.response?.status;
if (status === 500) {
toast.error("Não foi possível editar o departamento");
}
if (status === 400) {
toast.error("Nome de departamento inválido");
}
return err;
}
}

0 comments on commit 60f2089

Please sign in to comment.