|
@@ -1,51 +1,148 @@
|
|
|
/** @module users/state */
|
|
|
+import { call, put, takeEvery } from 'redux-saga/effects'
|
|
|
+import jwt from 'jwt-simple'
|
|
|
|
|
|
/**
|
|
|
- * state.js
|
|
|
- *
|
|
|
- * Collection of everything which has to do with state changes.
|
|
|
- **/
|
|
|
+* state.js
|
|
|
+*
|
|
|
+* Collection of everything which has to do with state changes.
|
|
|
+**/
|
|
|
|
|
|
/** actionTypes define what actions are handeled by the reducer. */
|
|
|
export const actions = {
|
|
|
- addUser: page => {
|
|
|
- return {
|
|
|
- type: 'TABLE_CHANGE_PAGINATION_PAGE',
|
|
|
- page
|
|
|
- }
|
|
|
- },
|
|
|
- deleteUser: items => {
|
|
|
- return {
|
|
|
- type: 'TABLE_CHANGE_PAGINATION_ITEMS',
|
|
|
- items
|
|
|
- }
|
|
|
+ loginRequest: (data) => {
|
|
|
+ return {
|
|
|
+ type: 'USER/LOGIN_REQUEST',
|
|
|
+ data
|
|
|
+ }
|
|
|
+ },
|
|
|
+ loginSuccess: (token) => {
|
|
|
+ return {
|
|
|
+ type: 'USER/LOGIN_SUCCESS',
|
|
|
+ token
|
|
|
+ }
|
|
|
+ },
|
|
|
+ loginFailure: () => {
|
|
|
+ return {
|
|
|
+ type: 'USER/LOGIN_FAILURE'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getUserList: () => {
|
|
|
+ return {
|
|
|
+ type: 'USER/GET_USER_LIST'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ userListSuccess: (users) => {
|
|
|
+ return {
|
|
|
+ type: 'USER/GET_USER_LIST_SUCCESS',
|
|
|
+ users
|
|
|
+ }
|
|
|
+ },
|
|
|
+ userListFailure: () => {
|
|
|
+ return {
|
|
|
+ type: 'USER/GET_USER_LIST_FAILURE'
|
|
|
+ }
|
|
|
+ },
|
|
|
+ addUserRequest: (data) => {
|
|
|
+ return {
|
|
|
+ type: 'USER/ADD_REQUEST',
|
|
|
+ data
|
|
|
+ }
|
|
|
+ },
|
|
|
+ addUserSuccess: (data) => {
|
|
|
+ return {
|
|
|
+ type: 'USER/ADD_SUCCESS',
|
|
|
+ data
|
|
|
+ }
|
|
|
+ },
|
|
|
+ addUserFailure: () => {
|
|
|
+ return {
|
|
|
+ type: 'USER/ADD_FAILURE'
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- console.log('State actions', actions)
|
|
|
-
|
|
|
- /** state definition */
|
|
|
- export const state = {
|
|
|
- users: {}
|
|
|
- }
|
|
|
- console.log('State state', state)
|
|
|
-
|
|
|
- /** reducer is called by the redux dispatcher and handles all component actions */
|
|
|
- export function reducer (state = [], action) {
|
|
|
- let pagination
|
|
|
+}
|
|
|
+console.log('State actions', actions)
|
|
|
+
|
|
|
+/** state definition */
|
|
|
+export const state = {
|
|
|
+ loginRequested: false,
|
|
|
+ users: {},
|
|
|
+ token: null,
|
|
|
+ tokenData: null
|
|
|
+}
|
|
|
+console.log('State state', state)
|
|
|
+
|
|
|
+/** reducer is called by the redux dispatcher and handles all component actions */
|
|
|
+export function reducer (state = [], action) {
|
|
|
switch (action.type) {
|
|
|
- case 'TABLE_CHANGE_PAGINATION_PAGE':
|
|
|
- pagination = { ...state.pagination }
|
|
|
- pagination.activePage = action.page
|
|
|
- return { ...state, pagination }
|
|
|
- case 'TABLE_CHANGE_PAGINATION_ITEMS':
|
|
|
- pagination = { ...state.pagination }
|
|
|
- pagination.items = action.items
|
|
|
- return { ...state, pagination }
|
|
|
- default:
|
|
|
- return state
|
|
|
+ case 'USER/LOGIN_REQUEST':
|
|
|
+ return { ...state, loginRequested: true }
|
|
|
+ case 'USER/LOGIN_SUCCESS':
|
|
|
+ const tokenData = action.token ? jwt.decode(action.token, null, true) : null
|
|
|
+ console.log(tokenData)
|
|
|
+ return { ...state, loginRequested: false, token: action.token, tokenData }
|
|
|
+ case 'USER/LOGIN_FAILURE':
|
|
|
+ return { ...state, loginRequested: false }
|
|
|
+ case 'USER/GET_USER_LIST':
|
|
|
+ return { ...state }
|
|
|
+ case 'USER/GET_USER_LIST_SUCCESS':
|
|
|
+ return { ...state, users: action.users }
|
|
|
+ case 'USER/GET_USER_LIST_FAILURE':
|
|
|
+ return { ...state }
|
|
|
+ default:
|
|
|
+ return state
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function * login (action) {
|
|
|
+ try {
|
|
|
+ console.log('User login requested', action.data)
|
|
|
+ const response = yield call(fetch, 'http://localhost:3002/authenticate/login', {
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+ },
|
|
|
+ body: JSON.stringify(action.data)
|
|
|
+ })
|
|
|
+ const responseJson = yield response.json()
|
|
|
+ if (response.status != 200) {
|
|
|
+ console.log(responseJson.msg)
|
|
|
+ throw new Error(responseJson.msg)
|
|
|
+ }
|
|
|
+ console.log('User login success!', actions.loginSuccess(responseJson.token))
|
|
|
+ yield put(actions.loginSuccess(responseJson.token))
|
|
|
+ } catch (error) {
|
|
|
+ console.log('User login failure!', actions.loginFailure(error))
|
|
|
+ yield put(actions.loginFailure(error))
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- /** sagas are asynchronous workers (JS generators) to handle the state. */
|
|
|
- export function * saga () {}
|
|
|
-
|
|
|
+}
|
|
|
+
|
|
|
+function * getUserList (action) {
|
|
|
+ try {
|
|
|
+ console.log('User list requested')
|
|
|
+ const response = yield call(fetch, 'http://localhost:3002/api/users', {
|
|
|
+ method: 'GET',
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
+ 'x-access-token': action.token
|
|
|
+ },
|
|
|
+ })
|
|
|
+ const responseJson = yield response.json()
|
|
|
+ if (response.status != 200) {
|
|
|
+ console.log(responseJson.msg)
|
|
|
+ throw new Error(responseJson.msg)
|
|
|
+ }
|
|
|
+ console.log('Get user list success!', actions.userListFailure(responseJson.users))
|
|
|
+ yield put(actions.userListFailure(responseJson.users))
|
|
|
+ } catch (error) {
|
|
|
+ console.log('Get user list failure!', actions.userListFailure(error))
|
|
|
+ yield put(actions.userListFailure(error.toString()))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/** sagas are asynchronous workers (JS generators) to handle the state. */
|
|
|
+export function * saga () {
|
|
|
+ console.log('User saga started.')
|
|
|
+ yield takeEvery('USER/LOGIN_REQUEST', login)
|
|
|
+ yield takeEvery('USER/GET_USER_LIST', getUserList)
|
|
|
+}
|