state.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /** @module users/state */
  2. import { call, put, takeEvery } from 'redux-saga/effects'
  3. import jwt from 'jwt-simple'
  4. /**
  5. * state.js
  6. *
  7. * Collection of everything which has to do with state changes.
  8. **/
  9. /** actionTypes define what actions are handeled by the reducer. */
  10. export const actions = {
  11. loginRequest: (data) => {
  12. return {
  13. type: 'USER/LOGIN_REQUEST',
  14. data
  15. }
  16. },
  17. loginSuccess: (token, tokenData) => {
  18. return {
  19. type: 'USER/LOGIN_SUCCESS',
  20. token,
  21. tokenData
  22. }
  23. },
  24. loginFailure: () => {
  25. return {
  26. type: 'USER/LOGIN_FAILURE'
  27. }
  28. },
  29. getUserList: () => {
  30. return {
  31. type: 'USER/GET_USER_LIST'
  32. }
  33. },
  34. userListSuccess: (users) => {
  35. return {
  36. type: 'USER/GET_USER_LIST_SUCCESS',
  37. users
  38. }
  39. },
  40. userListFailure: () => {
  41. return {
  42. type: 'USER/GET_USER_LIST_FAILURE'
  43. }
  44. },
  45. addUserRequest: (data) => {
  46. return {
  47. type: 'USER/ADD_REQUEST',
  48. data
  49. }
  50. },
  51. addUserSuccess: (data) => {
  52. return {
  53. type: 'USER/ADD_SUCCESS',
  54. data
  55. }
  56. },
  57. addUserFailure: () => {
  58. return {
  59. type: 'USER/ADD_FAILURE'
  60. }
  61. }
  62. }
  63. console.log('State actions', actions)
  64. /** state definition */
  65. export const state = {
  66. loginRequested: false,
  67. userListRequested: false,
  68. userListInitialized: false,
  69. users: [],
  70. token: null,
  71. tokenData: null
  72. }
  73. console.log('State state', state)
  74. /** reducer is called by the redux dispatcher and handles all component actions */
  75. export function reducer (state = [], action) {
  76. switch (action.type) {
  77. case 'USER/LOGIN_REQUEST':
  78. return { ...state, loginRequested: true }
  79. case 'USER/LOGIN_SUCCESS':
  80. return { ...state, loginRequested: false, token: action.token, tokenData: action.tokenData }
  81. case 'USER/LOGIN_FAILURE':
  82. return { ...state, loginRequested: false }
  83. case 'USER/GET_USER_LIST':
  84. return { ...state, userListRequested: true, userListInitialized: true }
  85. case 'USER/GET_USER_LIST_SUCCESS':
  86. return { ...state, userListRequested: false, users: action.users }
  87. case 'USER/GET_USER_LIST_FAILURE':
  88. return { ...state, userListRequested: false }
  89. default:
  90. return state
  91. }
  92. }
  93. function * login (action) {
  94. try {
  95. console.log('User login requested', action.data)
  96. const response = yield call(fetch, 'http://localhost:3002/authenticate/login', {
  97. method: 'POST',
  98. headers: {
  99. 'Content-Type': 'application/json'
  100. },
  101. body: JSON.stringify(action.data)
  102. })
  103. const responseJson = yield response.json()
  104. if (response.status != 200) {
  105. console.log(responseJson.msg)
  106. throw new Error(responseJson.msg)
  107. }
  108. const { token } = responseJson
  109. const tokenData = jwt.decode(token, null, true)
  110. localStorage.setItem('accessToken', token)
  111. localStorage.setItem('accessTokenData', JSON.stringify(tokenData))
  112. console.log('User login success!', token, tokenData)
  113. yield put(actions.loginSuccess(token, tokenData))
  114. } catch (error) {
  115. console.log('User login failure!', error)
  116. yield put(actions.loginFailure(error))
  117. }
  118. }
  119. function * getUserList (action) {
  120. try {
  121. const token = localStorage.getItem('accessToken')
  122. console.log('User list requested', action, token)
  123. const response = yield call(fetch, 'http://localhost:3002/api/users', {
  124. method: 'GET',
  125. headers: new Headers({
  126. 'Content-Type': 'application/json',
  127. 'x-access-token': token
  128. })
  129. })
  130. console.log('Received response')
  131. const responseJson = yield response.json()
  132. if (response.status != 200) {
  133. console.log('User list received error', responseJson.msg)
  134. throw new Error(responseJson.msg)
  135. }
  136. console.log('Get user list success!', responseJson.users)
  137. yield put(actions.userListSuccess(responseJson.users))
  138. } catch (error) {
  139. console.log('Get user list failure!', error)
  140. yield put(actions.userListFailure(error.toString()))
  141. }
  142. }
  143. /** sagas are asynchronous workers (JS generators) to handle the state. */
  144. export function * saga () {
  145. console.log('User saga started.')
  146. yield takeEvery('USER/LOGIN_REQUEST', login)
  147. yield takeEvery('USER/GET_USER_LIST', getUserList)
  148. }