Skip to content

Commit 24d0caf

Browse files
committed
dry actions
1 parent d3137f1 commit 24d0caf

23 files changed

+236
-239
lines changed

src/Register/Register.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Register extends Component {
3131
<a className="home-link" href="/">Home</a>
3232
<h2>Register</h2>
3333
</div>
34-
{ alert && <p className="alert alert-error">{alert.msg}</p> }
34+
{ alert.msg && <p className="alert alert-error">{alert.msg}</p> }
3535
<form className="form form-login" onSubmit={this.handleForm}>
3636
<input onChange={(e) => this.setState({username: e.target.value})} value={username} type="text" name="username" placeholder="Username" required />
3737
<input onChange={(e) => this.setState({password: e.target.value})} value={password} type="password" name="password" placeholder="Password" required />

src/actions/clearAlert.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { removeAlert } from './helpers/handleAlert';
2+
13
export const clearAlert = () => {
2-
return dispatch => dispatch({
3-
type: 'CLEAR'
4-
})
4+
return dispatch => removeAlert(dispatch);
55
}

src/actions/deletePhone.js

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,22 @@
1-
import axios from 'axios';
1+
import { setAlert } from './helpers/handleAlert';
2+
import sendRequest from './helpers/sendRequest';
3+
import { phoneDelete } from './helpers/phoneBook';
4+
import { loadPhone } from './helpers/phone';
25

36
export const deletePhone = (id, history) => {
4-
const token = JSON.parse(window.localStorage.getItem('jwt-token'));
57
return async dispatch => {
6-
dispatch({
7-
type: 'PHONE_LOADING'
8-
});
8+
loadPhone(dispatch);
99
try {
10-
const { data } = await axios({
11-
url: '//phonebookpca.herokuapp.com/graphql',
12-
method: 'post',
13-
headers: {
14-
'Authorization': `Bearer ${token.token}`
15-
},
16-
data: {
17-
query: `
18-
mutation {
19-
deletephone(id: ${id})
20-
}
21-
`
10+
const { data } = await sendRequest(`
11+
mutation {
12+
deletephone(id: ${id})
2213
}
23-
});
14+
`);
2415
if(data.data.deletephone) {
25-
return dispatch({
26-
type: 'DELETE_PHONEBOOK',
27-
id
28-
})
16+
return phoneDelete(dispatch, id);
2917
}
3018
} catch(e) {
31-
return dispatch({
32-
type: 'ERROR',
33-
msg: 'Cannot Delete'
34-
});
19+
return setAlert(dispatch, 'ERROR', 'Cannot Delete');
3520
}
3621
}
3722
}

src/actions/getLogin.js

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,32 @@
1-
import axios from 'axios';
1+
import { setAlert } from './helpers/handleAlert';
2+
import sendRequest from './helpers/sendRequest';
3+
import { doLogin, loadLogin, notLoggedIn } from './helpers/login';
24

35
export const getLogin = (username, password, history) => {
46
return async dispatch => {
5-
dispatch({
6-
type: 'LOGIN_LOADING',
7-
data: {
8-
loading: true
9-
}
10-
});
7+
loadLogin(dispatch, true);
118
try {
12-
const { data } = await axios({
13-
url: '//phonebookpca.herokuapp.com/graphql',
14-
method: 'post',
15-
data: {
16-
query: `
17-
query {
18-
login(username: "${username}", password: "${password}") {
19-
id
20-
username
21-
token
22-
}
9+
const { data } = await sendRequest(`
10+
query {
11+
login(username: "${username}", password: "${password}") {
12+
id
13+
username
14+
token
2315
}
24-
`
2516
}
26-
})
17+
`, false);
2718
const { data: { login } } = data;
28-
console.log(login);
2919
if(!login.username) {
30-
dispatch({
31-
type: 'ERROR',
32-
msg: 'Check username and password again'
33-
})
34-
return dispatch ({
35-
type: 'NOT_LOGGED_IN'
36-
})
20+
setAlert(dispatch, 'ERROR', 'Check username and password again');
21+
return notLoggedIn(dispatch);
3722
}
3823
window.localStorage.setItem('jwt-token', JSON.stringify(login));
39-
dispatch({
40-
type: 'GET_LOGIN',
41-
data: login
42-
})
43-
dispatch({
44-
type: 'SUCCESS',
45-
msg: 'Sucessfully Logged In'
46-
})
24+
doLogin(dispatch, login);
25+
setAlert(dispatch, 'SUCCESS', 'Sucessfully Logged In');
4726
history.push('/phonebook');
4827
} catch(e) {
49-
dispatch({
50-
type: 'ERROR',
51-
msg: 'Cannot log you in'
52-
})
53-
dispatch({
54-
type: 'NOT_LOGGED_IN'
55-
})
28+
setAlert(dispatch, 'ERROR', 'Cannot log you in');
29+
return notLoggedIn(dispatch);
5630
}
5731
}
5832
}

src/actions/getLogout.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { doLogout } from './helpers/login';
2+
13
export const getLogout = history => {
24
window.localStorage.clear();
35
return dispatch => {
4-
dispatch({
5-
type: 'LOGOUT'
6-
})
7-
history.push('/login');
6+
doLogout(dispatch);
7+
return history.push('/login');
88
}
99
}

src/actions/getPhone.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
1-
import axios from 'axios';
1+
import { setAlert } from './helpers/handleAlert';
2+
import sendRequest from './helpers/sendRequest';
3+
import { loadPhone, getThePhone } from './helpers/phone';
24

35
export const getPhone = (id) => {
4-
const token = JSON.parse(window.localStorage.getItem('jwt-token'));
56
return async dispatch => {
6-
dispatch({
7-
type: 'PHONE_LOADING'
8-
});
7+
loadPhone(dispatch);
98
try {
10-
const { data } = await axios({
11-
url: '//phonebookpca.herokuapp.com/graphql',
12-
method: 'post',
13-
headers: {
14-
'Authorization': `Bearer ${token.token}`
15-
},
16-
data: {
17-
query: `
18-
query {
19-
phone(id: ${id}) {
20-
id
21-
phone
22-
name
23-
}
9+
const { data } = await sendRequest(`
10+
query {
11+
phone(id: ${id}) {
12+
id
13+
phone
14+
name
2415
}
25-
`
2616
}
27-
})
17+
`);
2818
const { data: { phone } } = data;
29-
dispatch({
30-
type: 'GET_PHONE',
31-
data: phone
32-
})
19+
getThePhone(dispatch, phone);
3320
} catch(e) {
34-
return dispatch({
35-
type: 'ERROR',
36-
msg: 'Something went wrong'
37-
})
21+
return setAlert(dispatch, 'ERROR', 'Something went wrong');
3822
}
3923
}
4024
}

src/actions/getPhonebook.js

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,26 @@
1-
import axios from 'axios';
1+
import { setAlert } from './helpers/handleAlert';
2+
import sendRequest from './helpers/sendRequest';
3+
import { phoneBookLoading, getThePhoneBook } from './helpers/phoneBook';
24

35
export const getPhonebook = () => {
4-
const token = JSON.parse(window.localStorage.getItem('jwt-token'));
5-
66
return async dispatch => {
7-
dispatch({
8-
type: 'PHONEBOOK_LOADING',
9-
data: {
10-
loading: true
11-
}
12-
});
13-
7+
phoneBookLoading(dispatch, true);
148
try {
15-
const { data } = await axios({
16-
url: '//phonebookpca.herokuapp.com/graphql',
17-
method: 'post',
18-
headers: {
19-
'Authorization': `Bearer ${token.token}`
20-
},
21-
data: {
22-
query: `
23-
query {
24-
phonebook {
25-
id
26-
name
27-
phone
28-
}
9+
const { data } = await sendRequest(`
10+
query {
11+
phonebook {
12+
id
13+
name
14+
phone
2915
}
30-
`
3116
}
32-
})
17+
`);
3318

3419
const { data: { phonebook } } = data;
3520

36-
dispatch({
37-
type: 'GET_PHONEBOOK',
38-
data: phonebook
39-
});
21+
getThePhoneBook(dispatch, phonebook);
4022
} catch(e) {
41-
return dispatch({
42-
type: 'ERROR',
43-
msg: 'Something went wrong!'
44-
})
23+
return setAlert(dispatch, 'ERROR', 'Something went wrong!');
4524
}
4625
}
4726
}

src/actions/getRegister.js

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,34 @@
1-
import axios from 'axios';
1+
import { setAlert } from './helpers/handleAlert';
2+
import sendRequest from './helpers/sendRequest';
3+
import { registerLoading, notRegistered } from './helpers/register';
24

35
export const getRegister = (username, password, history) => {
46
return async dispatch => {
5-
dispatch({
6-
type: 'REGISTER_LOADING',
7-
data: {
8-
loading: true
9-
}
10-
})
7+
registerLoading(dispatch, true);
118
try {
12-
const { data } = await axios({
13-
url: '//phonebookpca.herokuapp.com/graphql',
14-
method: 'post',
15-
data: {
16-
query: `
17-
mutation {
18-
register(username: "${username}", password: "${password}") {
19-
id,
20-
username,
21-
token
22-
}
9+
const { data } = await sendRequest(`
10+
mutation {
11+
register(username: "${username}", password: "${password}") {
12+
id,
13+
username,
14+
token
2315
}
24-
`
2516
}
26-
})
17+
`, false);
2718
const { data: { register } } = data;
2819

2920
if(!register.username) {
30-
dispatch({
31-
type: 'ERROR',
32-
msg: 'You are already Registered'
33-
})
34-
dispatch({
35-
type: 'NOT_REGISTERED'
36-
})
21+
setAlert(dispatch, 'ERROR', 'You are already Registered');
22+
notRegistered(dispatch);
3723
return history.push('/login');
3824
}
39-
40-
dispatch({
41-
type: 'SUCCESS',
42-
msg: 'Sucessfully Registered, Please Log In'
43-
})
25+
setAlert(dispatch, 'SUCCESS', 'Sucessfully Registered, Please Log In');
4426

4527
return history.push('/login');
4628
} catch(e) {
47-
dispatch({
48-
type: 'ERROR',
49-
msg: 'Cannot register'
50-
})
29+
setAlert(dispatch, 'ERROR', 'Cannot register');
5130

52-
return dispatch({
53-
type: 'NOT_REGISTERED'
54-
})
31+
return notRegistered(dispatch);
5532
}
5633
}
5734
}

src/actions/helpers/handleAlert.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const setAlert = (dispatch, type, msg) => {
2+
dispatch({
3+
type,
4+
msg
5+
})
6+
}
7+
8+
export const removeAlert = (dispatch) => {
9+
dispatch({
10+
type: 'CLEAR'
11+
})
12+
}

src/actions/helpers/login.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export const doLogin = (dispatch, data) => {
2+
dispatch({
3+
type: 'GET_LOGIN',
4+
data
5+
})
6+
}
7+
8+
export const loadLogin = (dispatch, loading) => {
9+
dispatch({
10+
type: 'LOGIN_LOADING',
11+
data: {
12+
loading
13+
}
14+
});
15+
}
16+
17+
export const doLogout = (dispatch) => {
18+
dispatch({
19+
type: 'LOGOUT'
20+
})
21+
}
22+
23+
export const notLoggedIn = (dispatch) => {
24+
dispatch({
25+
type: 'NOT_LOGGED_IN'
26+
})
27+
}

0 commit comments

Comments
 (0)