/
registration.js
124 lines (112 loc) · 3.26 KB
/
registration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import DeviceInfo from 'react-native-device-info';
import api from '../services/api';
import namegen from '../services/namegen';
import _ from 'lodash';
import {createRequestActionTypes} from '.';
const {
CREATE_USER_REQUEST,
CREATE_USER_SUCCESS,
CREATE_USER_FAILURE
} = createRequestActionTypes('CREATE_USER');
const {
GET_USER_REQUEST,
GET_USER_SUCCESS,
GET_USER_FAILURE
} = createRequestActionTypes('GET_USER');
const OPEN_REGISTRATION_VIEW = 'OPEN_REGISTRATION_VIEW';
const CLOSE_REGISTRATION_VIEW = 'CLOSE_REGISTRATION_VIEW';
const UPDATE_NAME = 'UPDATE_NAME';
const RESET = 'RESET';
const SELECT_TEAM = 'SELECT_TEAM';
const CLOSE_TEAM_SELECTOR = 'CLOSE_TEAM_SELECTOR';
const DISMISS_INTRODUCTION = 'DISMISS_INTRODUCTION';
const openRegistrationView = () => {
return { type: OPEN_REGISTRATION_VIEW };
};
const closeRegistrationView = () => {
return { type: CLOSE_REGISTRATION_VIEW };
};
const dismissIntroduction = () => {
return { type: DISMISS_INTRODUCTION };
};
const putUser = () => {
return (dispatch, getStore) => {
dispatch({ type: CREATE_USER_REQUEST });
const uuid = DeviceInfo.getUniqueID();
const name = getStore().registration.get('name');
const team = getStore().registration.get('selectedTeam');
return api.putUser({ uuid, name, team })
.then(response => {
dispatch({ type: CREATE_USER_SUCCESS });
dispatch({ type: CLOSE_REGISTRATION_VIEW });
})
.catch(error => dispatch({ type: CREATE_USER_FAILURE, error: error }));
};
};
const selectTeam = team => {
return (dispatch, getStore) => {
const teams = getStore().team.get('teams').toJS();
const currentName = getStore().registration.get('name');
const currentTeam = _.find(teams, ['id', team]);
dispatch({ type: CLOSE_TEAM_SELECTOR });
dispatch({ type: SELECT_TEAM, payload: team });
// Generate new name if not given name
if (!currentName) {
dispatch({ type: UPDATE_NAME, payload: namegen.generateName(currentTeam.name) });
}
};
};
const updateName = name => {
return { type: UPDATE_NAME, payload: name };
};
const reset = () => {
return { type: RESET };
};
const generateName = () => {
return (dispatch, getStore) => {
const currentTeamId = getStore().registration.get('selectedTeam');
if (currentTeamId) {
const teams = getStore().team.get('teams').toJS();
const selectedTeam = _.find(teams, ['id', currentTeamId]);
if (selectedTeam) {
dispatch({ type: UPDATE_NAME, payload: namegen.generateName(selectedTeam.name) });
}
}
};
};
const getUser = () => {
return dispatch => {
dispatch({ type: GET_USER_REQUEST });
const uuid = DeviceInfo.getUniqueID();
return api.getUser(uuid)
.then(user => {
dispatch({ type: GET_USER_SUCCESS, payload: user });
})
.catch(error => {
dispatch({ type: GET_USER_FAILURE, error: error });
});
};
};
export {
CREATE_USER_REQUEST,
CREATE_USER_SUCCESS,
CREATE_USER_FAILURE,
OPEN_REGISTRATION_VIEW,
CLOSE_REGISTRATION_VIEW,
UPDATE_NAME,
GET_USER_REQUEST,
GET_USER_SUCCESS,
GET_USER_FAILURE,
SELECT_TEAM,
RESET,
DISMISS_INTRODUCTION,
putUser,
openRegistrationView,
closeRegistrationView,
updateName,
generateName,
getUser,
selectTeam,
reset,
dismissIntroduction
};