Skip to content

Commit 159c62f

Browse files
feat: add function for close/hide alerts when have callback errors.
1 parent 954abb0 commit 159c62f

File tree

8 files changed

+46
-20
lines changed

8 files changed

+46
-20
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ Caso tenho algum projeto aberto na porta 3000, no terminal o react irá te pergu
2222
* [x] Login com sucesso.
2323
* [x] Validação de erros.
2424
* [x] Redirecionamento baseado no perfil do usuário.
25-
* [ ] Fechar alertas/msg de erros.
25+
* [x] Fechar alertas/msg de erros.
2626

2727
* [ ] Cadastrar Usuários
2828
* [x] Cadastrar usuários com sucesso.
2929
* [x] Validação de erros.
3030
* [x] Ao cadastrar ser redirecionado conforme for o perfil.
31-
* [ ] Fechar alertas/msg de erros.
31+
* [x] Fechar alertas/msg de erros.
3232

3333
* [ ] Administrador
3434

@@ -41,13 +41,13 @@ Caso tenho algum projeto aberto na porta 3000, no terminal o react irá te pergu
4141
* [x] Cadastrar usuários com sucesso.
4242
* [x] Validação de erros.
4343
* [x] Ao cadastrar ser redirecionado para lista de usuários.
44-
* [ ] Fechar alertas/msg de erros.
44+
* [x] Fechar alertas/msg de erros.
4545

4646
* [ ] Cadastrar Produtos
4747
* [x] Cadastrar produtos com sucesso.
4848
* [x] Validação de erros.
4949
* [x] Ao cadastrar ser redirecionado para lista de produtos.
50-
* [ ] Fechar alertas/msg de erros.
50+
* [x] Fechar alertas/msg de erros.
5151

5252
* [ ] Listar Usuários
5353
* [x] Listar usuários com sucesso.

src/component/errorAlert.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
11
import React, { Component } from 'react';
22
import 'bootswatch/dist/minty/bootstrap.min.css';
3+
import '../styles/errorAlert.css'
34

45
export default class ErrorAlert extends Component {
5-
constructor(props) {
6-
super(props);
6+
7+
constructor(props, context) {
8+
super(props, context);
79
this.state = {
810
display: true,
911
};
1012
}
1113

14+
shouldComponentUpdate() {
15+
if (!this.state.display) {
16+
this.setState({
17+
display: true
18+
})
19+
}
20+
return true
21+
}
22+
23+
handleClose() {
24+
if(this.state.display) {
25+
this.setState({
26+
display: false
27+
})
28+
}
29+
}
30+
1231
render() {
13-
return (
14-
<div>
15-
{ this.state.display
16-
&& <div className="alert alert-dismissible alert-secondary">
17-
<b>{ this.props.name }</b>
18-
</div>}
19-
</div>
20-
);
32+
if (this.state.display) {
33+
return (
34+
<div className="alert alert-secondary alert-dismissible" role="alert" key={this.state.key}>
35+
<button type="button" className="close btn-close-error-alert" data-dismiss="alert" aria-label="Close" onClick={() => this.handleClose()}>
36+
<span aria-hidden="true">&times;</span>
37+
</button>
38+
<span>{ this.props.name }</span>
39+
</div>
40+
);
41+
}
42+
return <div/>
2143
}
2244
}

src/styles/errorAlert.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.btn-close-error-alert {
2+
opacity: 1;
3+
box-shadow: 0px 0px 0px 0px rgb(221 221 221);
4+
}

src/views/admin/registerProducts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class RegisterProducts extends React.Component {
8686
<Navbar></Navbar>
8787
<form className="jumbotron" onSubmit={ this.submitHandler }>
8888
{ this.state.msg_error.map((item , index)=> {
89-
return <ErrorAlert name={ item } key={ index } display={ this.state.display }></ErrorAlert>;
89+
return <ErrorAlert name={ item } key={ index }></ErrorAlert>;
9090
})}
9191
<h1>Cadastro de Produtos</h1>
9292
<hr className="my-4"></hr>

src/views/admin/registerUser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class RegisterUsers extends React.Component {
6363
<Navbar></Navbar>
6464
<form className="jumbotron" onSubmit={ this.submitHandler }>
6565
{ this.state.msg_error.map((item , index)=> {
66-
return <ErrorAlert name={ item } key={ index } display={ this.state.display }></ErrorAlert>;
66+
return <ErrorAlert name={ item } key={ index }></ErrorAlert>;
6767
}) }
6868
<h1>Cadastro de usuários</h1>
6969
<hr className="my-4"></hr>

src/views/admin/showUsers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ShowUsers extends React.Component {
7171
<>
7272
<div className="col">
7373
{ this.state.msg_error.map((item, index) => {
74-
return <ErrorAlert name={ item } key={ index } display={ this.state.display }></ErrorAlert>;
74+
return <ErrorAlert name={ item } key={ index }></ErrorAlert>;
7575
})}
7676
</div>
7777
<table className="table table-striped">

src/views/login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Login extends React.Component {
145145
<h2 className="font-robot">Login</h2>
146146
<br />
147147
{ this.state.msg_error.map((item, index) => {
148-
return <ErrorAlert name={ item } key={ index } display={ this.state.display }></ErrorAlert>;
148+
return <ErrorAlert name={ item } key={ index }></ErrorAlert>;
149149
})}
150150
<input
151151
type="email"

src/views/registerUser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class RegisterUser extends React.Component {
7878
<img className="imagem" src={ logo1 } width="200" height="200" />
7979
<h2 className="font-robot">Cadastro </h2>
8080
{ msg_success !== "Cadastro realizado com sucesso" ? null : <SuccessAlert name={ msg_success } display={ this.state.display }></SuccessAlert>}
81-
{ this.state.msg_error.map((item, index)=> {
82-
return <ErrorAlert name={ item } key={index} display={ this.state.display }></ErrorAlert>;
81+
{ this.state.msg_error.map((item, index) => {
82+
return <ErrorAlert name={ item } key={ index }></ErrorAlert>;
8383
}) }
8484
<input
8585
type="text"

0 commit comments

Comments
 (0)