Skip to content

Commit fe03ec2

Browse files
authored
Merge pull request #132 from CreateThrive/refactor/fetch-user
Refactor: Fetch User
2 parents 43167c9 + 8046a7c commit fe03ec2

File tree

11 files changed

+154
-185
lines changed

11 files changed

+154
-185
lines changed

functions/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ node_modules/
1010
lib/
1111

1212
#Necessary config for testing
13-
env.json
13+
env.json
14+
15+
service-account-key.json

src/components/UserForm/UserForm.test.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import * as actions from 'state/actions/users';
55
import UserForm from '.';
66

77
describe('<UserForm /> rendering', () => {
8-
let userData;
8+
const setUser = jest.fn();
99

10+
let userData;
1011
beforeEach(() => {
1112
userData = {
1213
email: 'mkrukuy@gmail.com',
@@ -24,7 +25,7 @@ describe('<UserForm /> rendering', () => {
2425
const user = { ...userData, createdAt: '11/21/2020' };
2526

2627
const { component } = shallowWithProviders(
27-
<UserForm user={user} action={actions.createUser} />
28+
<UserForm user={user} action={actions.createUser} setUser={setUser} />
2829
)({
2930
users: {},
3031
});
@@ -34,7 +35,7 @@ describe('<UserForm /> rendering', () => {
3435

3536
it('should display user name preview', () => {
3637
const { component } = mountWithProviders(
37-
<UserForm user={userData} action={actions.createUser} />
38+
<UserForm user={userData} action={actions.createUser} setUser={setUser} />
3839
)({
3940
users: {},
4041
});
@@ -46,7 +47,7 @@ describe('<UserForm /> rendering', () => {
4647

4748
it('should display email preview if it is creating a new user', () => {
4849
const { component } = mountWithProviders(
49-
<UserForm user={userData} action={actions.createUser} />
50+
<UserForm user={userData} action={actions.createUser} setUser={setUser} />
5051
)({
5152
users: {},
5253
});
@@ -58,7 +59,12 @@ describe('<UserForm /> rendering', () => {
5859

5960
it('should display location preview', () => {
6061
const { component } = mountWithProviders(
61-
<UserForm user={userData} isEditing action={actions.createUser} />
62+
<UserForm
63+
user={userData}
64+
isEditing
65+
action={actions.createUser}
66+
setUser={setUser}
67+
/>
6268
)({
6369
users: {},
6470
});
@@ -70,7 +76,12 @@ describe('<UserForm /> rendering', () => {
7076

7177
it('should display admin preview', () => {
7278
const { component } = mountWithProviders(
73-
<UserForm user={userData} isEditing action={actions.createUser} />
79+
<UserForm
80+
user={userData}
81+
isEditing
82+
action={actions.createUser}
83+
setUser={setUser}
84+
/>
7485
)({
7586
users: {},
7687
});
@@ -80,7 +91,12 @@ describe('<UserForm /> rendering', () => {
8091

8192
it('should display created preview', () => {
8293
const { component } = mountWithProviders(
83-
<UserForm user={userData} isEditing action={actions.createUser} />
94+
<UserForm
95+
user={userData}
96+
isEditing
97+
action={actions.createUser}
98+
setUser={setUser}
99+
/>
84100
)({
85101
users: {},
86102
});
@@ -90,8 +106,9 @@ describe('<UserForm /> rendering', () => {
90106

91107
describe('<LoginForm /> actions', () => {
92108
const dispatchMock = jest.fn();
93-
let userData;
109+
const setUser = jest.fn();
94110

111+
let userData;
95112
beforeEach(() => {
96113
jest
97114
.spyOn(reactRedux, 'useDispatch')
@@ -112,7 +129,7 @@ describe('<LoginForm /> actions', () => {
112129

113130
it('should dispatch createUser action when creating a new user', () => {
114131
const { component } = mountWithProviders(
115-
<UserForm user={userData} action={actions.createUser} />
132+
<UserForm user={userData} action={actions.createUser} setUser={setUser} />
116133
)({
117134
users: {},
118135
});
@@ -124,7 +141,12 @@ describe('<LoginForm /> actions', () => {
124141

125142
it('should dispatch modifyUser action when editing a user', () => {
126143
const { component } = mountWithProviders(
127-
<UserForm user={userData} isEditing action={actions.modifyUser} />
144+
<UserForm
145+
user={userData}
146+
isEditing
147+
action={actions.modifyUser}
148+
setUser={setUser}
149+
/>
128150
)({
129151
users: {},
130152
});

src/components/UserForm/__snapshots__/UserForm.test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ exports[`<UserForm /> rendering should render without crashing 1`] = `
143143
>
144144
<UserForm
145145
action={[Function]}
146+
isEditing={false}
147+
isProfile={false}
148+
setUser={[MockFunction]}
146149
user={
147150
Object {
148151
"createdAt": "11/21/2020",

src/components/UserForm/index.jsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,24 @@ const UserForm = ({ isEditing, isProfile, user, setUser, action }) => {
396396
};
397397

398398
UserForm.propTypes = {
399-
isEditing: PropTypes.bool,
400-
userData: PropTypes.shape({
399+
user: PropTypes.shape({
401400
id: PropTypes.string,
402401
isAdmin: PropTypes.bool.isRequired,
403402
name: PropTypes.string.isRequired,
404403
location: PropTypes.string,
405404
logoUrl: PropTypes.string,
406405
createdAt: PropTypes.string.isRequired,
407-
}),
406+
email: PropTypes.string.isRequired,
407+
}).isRequired,
408+
setUser: PropTypes.func.isRequired,
408409
action: PropTypes.func.isRequired,
410+
isEditing: PropTypes.bool,
411+
isProfile: PropTypes.bool,
412+
};
413+
414+
UserForm.defaultProps = {
415+
isEditing: false,
416+
isProfile: false,
409417
};
410418

411419
export default UserForm;

src/pages/Login/index.jsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
authCleanUp,
1212
authFacebook,
1313
authGoogle,
14-
authMicrosoft
14+
authMicrosoft,
1515
} from 'state/actions/auth';
1616
import { useChangeHandler, useFormatMessage } from 'hooks';
1717
import { inputValidations } from 'utils';
@@ -20,11 +20,11 @@ import classes from './Login.module.scss';
2020

2121
const Login = () => {
2222
const { error, isAuth, loading, locale } = useSelector(
23-
state => ({
23+
(state) => ({
2424
error: state.auth.error,
2525
isAuth: !!state.auth.userData.id,
2626
loading: state.auth.loading,
27-
locale: state.preferences.locale
27+
locale: state.preferences.locale,
2828
}),
2929
shallowEqual
3030
);
@@ -33,7 +33,7 @@ const Login = () => {
3333

3434
const [authData, setAuthData] = useState({
3535
email: '',
36-
password: ''
36+
password: '',
3737
});
3838

3939
const onChangeHandler = useChangeHandler(setAuthData);
@@ -56,7 +56,7 @@ const Login = () => {
5656
.auth()
5757
.isSignInWithEmailLink(window.location.href);
5858

59-
const onSubmitHandler = event => {
59+
const onSubmitHandler = (event) => {
6060
event.preventDefault();
6161

6262
if (isEmailLink) {
@@ -72,28 +72,28 @@ const Login = () => {
7272

7373
const onFacebookHandler = useCallback(() => {
7474
dispatch(authFacebook());
75-
}, [dispatch, authFacebook]);
75+
}, [dispatch]);
7676

7777
const onGoogleHandler = useCallback(() => {
7878
dispatch(authGoogle());
79-
}, [dispatch, authGoogle]);
79+
}, [dispatch]);
8080

8181
const onMicrosoftHandler = useCallback(() => {
8282
dispatch(authMicrosoft());
83-
}, [dispatch, authMicrosoft]);
83+
}, [dispatch]);
8484

8585
const inputs = isEmailLink
8686
? inputValidations(authData.email, authData.password, locale)
8787
: {
8888
email: {
8989
modifier: null,
90-
message: null
90+
message: null,
9191
},
9292
password: {
9393
modifier: null,
94-
message: null
94+
message: null,
9595
},
96-
canSubmit: false
96+
canSubmit: false,
9797
};
9898

9999
const redirect = isAuth && <Redirect to={paths.ROOT} />;
@@ -180,7 +180,7 @@ const Login = () => {
180180
<button
181181
type="submit"
182182
className={classNames('button', 'is-black', {
183-
'is-loading': loading
183+
'is-loading': loading,
184184
})}
185185
disabled={isEmailLink ? !inputs.canSubmit : false}
186186
>

src/pages/User/User.test.js

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@ describe('<User /> rendering', () => {
88
it('should render without crashing', () => {
99
const { component } = shallowWithProviders(<User />)({
1010
users: {
11-
user: {
12-
name: '',
13-
email: '',
14-
location: '',
15-
isAdmin: false,
16-
file: null,
17-
createdAt: new Date().toDateString(),
18-
},
11+
data: [],
1912
},
2013
});
2114

@@ -25,14 +18,7 @@ describe('<User /> rendering', () => {
2518
it('should not show the spinner when creating a user', () => {
2619
const { component } = mountWithProviders(<User />)({
2720
users: {
28-
user: {
29-
name: '',
30-
email: '',
31-
location: '',
32-
isAdmin: false,
33-
file: null,
34-
createdAt: new Date().toDateString(),
35-
},
21+
data: [],
3622
},
3723
});
3824

@@ -42,14 +28,7 @@ describe('<User /> rendering', () => {
4228
it('should render the UserForm component when creating a user', () => {
4329
const { component } = mountWithProviders(<User />)({
4430
users: {
45-
user: {
46-
name: '',
47-
email: '',
48-
location: '',
49-
isAdmin: false,
50-
file: null,
51-
createdAt: new Date().toDateString(),
52-
},
31+
data: [],
5332
},
5433
});
5534

src/pages/User/index.jsx

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useMemo } from 'react';
22
import { useParams, Redirect } from 'react-router-dom';
33
import { useSelector, shallowEqual, useDispatch } from 'react-redux';
44
import ClipLoader from 'react-spinners/ClipLoader';
@@ -11,48 +11,42 @@ import { useFormatMessage } from 'hooks';
1111
const User = () => {
1212
const { id } = useParams();
1313

14-
const { success, usersList, userData, error } = useSelector(
14+
const isEditing = useMemo(() => !!id, [id]);
15+
16+
const { success, userData, error } = useSelector(
1517
(state) => ({
1618
success: state.users.success,
17-
usersList: state.users.list,
18-
userData: state.users.user,
19+
userData: state.users.data.find((user) => user.id === id),
1920
error: state.users.error,
2021
}),
2122
shallowEqual
2223
);
2324

24-
const [user, setUser] = useState(userData || {});
25+
const [user, setUser] = useState(
26+
isEditing
27+
? userData
28+
: {
29+
name: '',
30+
email: '',
31+
location: '',
32+
createdAt: new Date().toDateString(),
33+
isAdmin: false,
34+
}
35+
);
2536

2637
const dispatch = useDispatch();
2738

2839
useEffect(() => {
29-
if (id) {
30-
const userFetched = usersList.find(
31-
(fetchedUser) => fetchedUser.id === id
32-
);
33-
if (userFetched) {
34-
setUser(userFetched);
35-
} else if (userData.id === id) {
36-
setUser(userData);
37-
} else {
40+
if (isEditing) {
41+
if (!userData) {
3842
dispatch(fetchUsers(id));
3943
}
40-
}
41-
}, [id, userData]);
42-
43-
const isEditing = !!id;
4444

45-
const userForm =
46-
!user && id ? (
47-
<ClipLoader />
48-
) : (
49-
<UserForm
50-
isEditing={isEditing}
51-
user={user}
52-
setUser={setUser}
53-
action={isEditing ? modifyUser : createUser}
54-
/>
55-
);
45+
if (userData && !user) {
46+
setUser(userData);
47+
}
48+
}
49+
}, [isEditing, id, userData, user, dispatch]);
5650

5751
const redirect = (error || success) && <Redirect to={paths.USERS} />;
5852

@@ -70,7 +64,18 @@ const User = () => {
7064
</h1>
7165
</div>
7266
</section>
73-
<section className="section is-main-section">{userForm}</section>
67+
<section className="section is-main-section">
68+
{isEditing && !user ? (
69+
<ClipLoader />
70+
) : (
71+
<UserForm
72+
isEditing={isEditing}
73+
user={user}
74+
setUser={setUser}
75+
action={isEditing ? modifyUser : createUser}
76+
/>
77+
)}
78+
</section>
7479
</>
7580
);
7681
};

0 commit comments

Comments
 (0)