Skip to content
This repository was archived by the owner on Jan 8, 2021. It is now read-only.

Commit c3fb68f

Browse files
author
andela-akhenda
committed
feat(models): create user model and enable use of fixturesAPI
1 parent 5b5bf12 commit c3fb68f

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

.cz-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ module.exports = {
2525
{ name: 'structure' },
2626
{ name: 'android' },
2727
{ name: 'permissions' },
28+
{ name: 'models' },
29+
{ name: 'services' },
2830
],
2931

3032
// override the messages, defaults are as follows

src/config/debug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default {
2-
useFixtures: false,
2+
useFixtures: true,
33
yellowBox: __DEV__,
44
useReactotron: __DEV__,
55
useReduxLogger: __DEV__,

src/models/user.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { Actions } from 'react-native-router-flux';
2+
import API from 'src/services/api';
3+
import DebugConfig from 'src/config/debug';
4+
import FixtureAPI from 'src/services/fixtureApi';
5+
6+
import {
7+
LOGIN_USER_SUCCESS,
8+
LOGIN_USER_FAILURE,
9+
FETCH_USER_FAILURE,
10+
SIGNUP_USER_FAILURE,
11+
SIGNOUT_USER_SUCCESS,
12+
} from 'src/state/types';
13+
14+
15+
const api = DebugConfig.useFixtures ? FixtureAPI : API.create();
16+
17+
const responseFailure = (dispatch, type, response) => {
18+
let message = 'An error occured! Please try again.';
19+
if (response.problem === 'TIMEOUT_ERROR') {
20+
message = 'A timeout error occured! Check your internet connection and then try again.';
21+
} else if (response.problem === 'NETWORK_ERROR') {
22+
message = 'A network error occured! Check your internet connection and then try again.';
23+
} else if (response.problem === 'CONNECTION_ERROR') {
24+
message = 'A connection error occured! Service is currently unavailable. Please try again later.';
25+
} else if (response.data) {
26+
dispatch({ type, payload: response.data });
27+
}
28+
29+
dispatch({ type, payload: { message } });
30+
};
31+
32+
export const isUserAuthenticated = (dispatch, token) => {
33+
api
34+
.validateToken(token)
35+
.then((res) => {
36+
if (res.ok) {
37+
fetchUserInfo(dispatch, token);
38+
} else {
39+
const payload = {};
40+
dispatch({ type: FETCH_USER_FAILURE, payload });
41+
}
42+
});
43+
};
44+
45+
export const signUpUser = (dispatch, data) => {
46+
const {
47+
email, username, firstName, lastName, password,
48+
} = data;
49+
50+
api
51+
.signUpUser(email, username, firstName, lastName, password)
52+
.then((res) => {
53+
if (res.status === 201) {
54+
signInUser(dispatch, email, password);
55+
} else {
56+
responseFailure(dispatch, SIGNUP_USER_FAILURE, res);
57+
}
58+
})
59+
.catch((error) => {
60+
dispatch({ type: SIGNUP_USER_FAILURE, payload: error });
61+
});
62+
};
63+
64+
export const signInUser = (dispatch, email, password) => {
65+
api
66+
.loginUser(email, password)
67+
.then((res) => {
68+
if (res.status === 200) {
69+
fetchUserInfo(dispatch, res.data.token);
70+
} else {
71+
responseFailure(dispatch, LOGIN_USER_FAILURE, res);
72+
}
73+
});
74+
};
75+
76+
export const fetchUserInfo = (dispatch, token) => {
77+
api.setHeader('Authorization', `Bearer ${token}`);
78+
api
79+
.getUser('edit')
80+
.then((res) => {
81+
if (res.status === 200) {
82+
dispatch({ type: LOGIN_USER_SUCCESS, payload: { token, user: res.data } });
83+
Actions.drawer({ type: 'reset' });
84+
} else {
85+
responseFailure(dispatch, FETCH_USER_FAILURE, res);
86+
}
87+
});
88+
};
89+
90+
export const signOutUser = (dispatch) => {
91+
// delete the token
92+
93+
dispatch({ type: SIGNOUT_USER_SUCCESS });
94+
Actions.auth({ type: 'reset' });
95+
};

src/services/fixtureApi.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@ export default {
33
signUpUser: () => {
44
return {
55
ok: true,
6+
status: 201,
67
data: require('src/services/fixtures/user.json'),
78
};
89
},
910
loginUser: () => {
1011
return {
1112
ok: true,
13+
status: 200,
1214
data: require('src/services/fixtures/loginUser.json'),
1315
};
1416
},
1517
validateToken: () => {
1618
return {
1719
ok: true,
20+
status: 200,
1821
data: require('src/services/fixtures/validToken.json'),
1922
};
2023
},
2124
getUser: () => {
2225
return {
2326
ok: true,
27+
status: 200,
2428
data: require('src/services/fixtures/user.json'),
2529
};
2630
},

0 commit comments

Comments
 (0)