Skip to content

Commit 338567e

Browse files
fix(user update): fixing bug in update user form
1 parent ac00369 commit 338567e

File tree

7 files changed

+51
-23
lines changed

7 files changed

+51
-23
lines changed

src/components/FormUpdate/index.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { Form } from '@unform/web';
55

66
import InputForm from 'components/InputForm';
77
import Button from 'components/Button/Button';
8+
import Input from 'components/Input/Input';
89

910
import { Container } from './styles';
1011

1112
interface FormUpdateProps {
13+
emailIsNotAllowed?: boolean;
1214
handleSubmit: any;
1315
formRef: any;
1416
hasPasswordField?: boolean;
@@ -30,6 +32,7 @@ interface ParamsProps {
3032
email: string;
3133
}
3234
function FormUpdate({
35+
emailIsNotAllowed,
3336
handleSubmit,
3437
formRef,
3538
inviteInfo,
@@ -64,12 +67,24 @@ function FormUpdate({
6467
title="Empresa"
6568
/>
6669

67-
<InputForm
68-
name="email"
69-
title="E-mail"
70-
defaultValue={paramsEmail || user?.email || ''}
71-
readOnly={!!paramsEmail}
72-
/>
70+
{emailIsNotAllowed ? (
71+
<Input
72+
name="email"
73+
title="E-mail"
74+
styleWidth="40rem"
75+
color="var(--black);"
76+
weight="bold"
77+
defaultValue={paramsEmail || user?.email || ''}
78+
readOnly={!!paramsEmail}
79+
/>
80+
) : (
81+
<InputForm
82+
name="email"
83+
title="E-mail"
84+
defaultValue={paramsEmail || user?.email || ''}
85+
readOnly={!!paramsEmail}
86+
/>
87+
)}
7388

7489
<InputForm defaultValue={user?.phone} name="phone" title="Telefone" />
7590

@@ -98,6 +113,7 @@ function FormUpdate({
98113
}
99114

100115
FormUpdate.defaultProps = {
116+
emailIsNotAllowed: false,
101117
hasPasswordField: false,
102118
user: {
103119
name: '',

src/components/Input/Input.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StyledInput } from './styles';
44
interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
55
title: string;
66
color?: string;
7+
weight?: string;
78
Size?: string;
89
styleWidth?: string;
910
Type?: string;
@@ -14,6 +15,7 @@ const Input: React.FC<InputProps> = ({
1415
color,
1516
Size,
1617
styleWidth,
18+
weight,
1719
Type,
1820
...rest
1921
}: InputProps) => {
@@ -22,6 +24,7 @@ const Input: React.FC<InputProps> = ({
2224
InputWidth={styleWidth || '23.75rem'}
2325
Color={color || 'var(--black)'}
2426
Size={Size || '1.5rem'}
27+
weight={weight}
2528
className="input-styled"
2629
>
2730
<h3>{title}</h3>
@@ -34,8 +37,9 @@ const Input: React.FC<InputProps> = ({
3437
Input.defaultProps = {
3538
Type: '',
3639
Size: '',
40+
weight: '',
3741
styleWidth: '23.75rem',
38-
color: '',
42+
color: 'var(--black)',
3943
};
4044

4145
export default Input;

src/components/Input/styles.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ interface Props {
44
InputWidth: string;
55
Color: string;
66
Size: string;
7+
weight?: string;
78
}
89

910
export const StyledInput = styled.div<Props>`
@@ -33,12 +34,13 @@ export const StyledInput = styled.div<Props>`
3334
font-family: Roboto;
3435
font-size: 1.3rem;
3536
font-style: normal;
36-
font-weight: 400;
37+
font-weight: ${props => props.weight || '400'};
3738
3839
outline: none;
3940
border: 0;
4041
border-radius: 1.25rem;
4142
43+
color: ${props => props.Color || 'var(--black)'};
4244
max-width: ${props => props.InputWidth || '23.75rem'};
4345
width: 100%;
4446
}

src/components/Route/index.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Route as ReactRoute } from 'react-router-dom';
2+
import { Route as ReactRoute, Redirect } from 'react-router-dom';
33

44
import { useAuth } from 'contexts/auth';
55
import Header from 'components/Header';
@@ -11,15 +11,26 @@ interface RouteProps {
1111
path: string;
1212
exact?: boolean;
1313
isPrivate?: boolean;
14+
onlyAdmin?: boolean;
1415
}
1516

16-
const Route = ({ component: Component, isPrivate, ...rest }: RouteProps) => {
17+
const Route = ({
18+
component: Component,
19+
isPrivate,
20+
onlyAdmin,
21+
...rest
22+
}: RouteProps) => {
1723
const { user: userContext } = useAuth();
1824
const user = getUser();
25+
const userIsAdmin = user?.type === 'Admin';
1926
const userToken = getUserToken();
2027

2128
const userIsAuthenticated = !!user && !!userToken;
2229

30+
if (onlyAdmin && !userIsAdmin) {
31+
return <Redirect to="404" />;
32+
}
33+
2334
return (
2435
<ReactRoute
2536
{...rest}
@@ -49,6 +60,7 @@ const Route = ({ component: Component, isPrivate, ...rest }: RouteProps) => {
4960

5061
Route.defaultProps = {
5162
exact: false,
63+
onlyAdmin: false,
5264
isPrivate: false,
5365
};
5466

src/pages/UserUpdate/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useUsers } from 'contexts/user';
1111
import { IUser } from 'DTOs/User';
1212

1313
import getValidationErrors from 'utils/getValidationErrors';
14-
import updateSchemaValidation from 'utils/updateSchemaValidation copy';
14+
import updateSchemaValidation from 'utils/updateSchemaValidation';
1515

1616
import ModalUpdateUser from './ModalUpdateUser';
1717

@@ -62,6 +62,7 @@ function UserUpdate() {
6262
inviteInfo={inviteInfo}
6363
handleSubmit={handleSubmit}
6464
user={user}
65+
emailIsNotAllowed
6566
/>
6667
</Container>
6768
</>

src/routes.tsx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import React from 'react';
2-
import { Switch, BrowserRouter } from 'react-router-dom';
2+
import { Switch, BrowserRouter, Redirect } from 'react-router-dom';
33

44
import Route from 'components/Route';
55

6-
import { useAuth } from 'contexts/auth';
7-
86
import Recovery from 'pages/Recovery';
97
import UserUpdate from 'pages/UserUpdate';
108
import ResetPassword from 'pages/ResetPassword';
@@ -17,9 +15,6 @@ import NotFound from 'pages/NotFound';
1715
import Users from 'pages/Users';
1816

1917
export default function Routes() {
20-
const { user } = useAuth();
21-
const userIsAdmin = user?.type === 'Admin';
22-
2318
return (
2419
<BrowserRouter>
2520
<Switch>
@@ -33,12 +28,10 @@ export default function Routes() {
3328
<Route path="/invite/:email" component={Register} />
3429
<Route path="/404" component={NotFound} />
3530

36-
{userIsAdmin && (
37-
<>
38-
<Route path="/invite" exact isPrivate component={Invite} />
39-
<Route path="/users" isPrivate component={Users} />
40-
</>
41-
)}
31+
<Route path="/invite" exact isPrivate onlyAdmin component={Invite} />
32+
<Route path="/users" isPrivate onlyAdmin component={Users} />
33+
34+
<Redirect to="/404" />
4235
</Switch>
4336
</BrowserRouter>
4437
);
File renamed without changes.

0 commit comments

Comments
 (0)