state.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /** @module swisstennis/state */
  2. import { call, put, takeEvery } from 'redux-saga/effects'
  3. import { SZTM_API } from '../local-constants'
  4. import { normalizePhone } from '../helpers'
  5. import api from '../api'
  6. /**
  7. * state.js
  8. *
  9. * Collection of everything which has to do with state changes.
  10. **/
  11. /** actionTypes define what actions are handeled by the reducer. */
  12. export const actions = {
  13. changeForm: (data) => {
  14. return {
  15. type: 'SMS/CHANGE_FORM',
  16. data
  17. }
  18. },
  19. setRecipients: (data) => {
  20. return {
  21. type: 'SMS/SET_RECIPIENTS',
  22. data
  23. }
  24. },
  25. addRecipient: (data) => {
  26. return {
  27. type: 'SMS/ADD_RECIPIENT',
  28. data
  29. }
  30. },
  31. sendSMSRequest: (data) => {
  32. return {
  33. type: 'SMS/SEND_REQUEST',
  34. data
  35. }
  36. },
  37. sendSMSSuccess: (data) => {
  38. return {
  39. type: 'SMS/SEND_SUCCESS',
  40. data
  41. }
  42. },
  43. sendSMSFailure: (error) => {
  44. return {
  45. type: 'SMS/SEND_FAILURE',
  46. error
  47. }
  48. },
  49. getCreditRequest: (data) => {
  50. return {
  51. type: 'SMS/CREDIT_REQUEST',
  52. data
  53. }
  54. },
  55. getCreditSuccess: (data) => {
  56. return {
  57. type: 'SMS/CREDIT_SUCCESS',
  58. data
  59. }
  60. },
  61. getCreditFailure: (error) => {
  62. return {
  63. type: 'SMS/CREDIT_FAILURE',
  64. error
  65. }
  66. }
  67. }
  68. console.log('State actions', actions)
  69. /** state definition */
  70. export const state = {
  71. recipients: [],
  72. sender: 'SZTM',
  73. body: '',
  74. sending: false,
  75. newRecipient: '',
  76. message: '',
  77. credit: '?'
  78. }
  79. console.log('State state', state)
  80. /** reducer is called by the redux dispatcher and handles all component actions */
  81. export function reducer (state = [], action) {
  82. switch (action.type) {
  83. case 'SMS/CHANGE_FORM':
  84. return { ...state, ...action.data }
  85. case 'SMS/SET_RECIPIENTS':
  86. const recipients = action.data.map(player => player.phone).filter(number => !!number)
  87. console.log(recipients)
  88. return { ...state, recipients }
  89. case 'SMS/ADD_RECIPIENT':
  90. const number = normalizePhone(action.data)
  91. if (number) {
  92. return { ...state, recipients: [ number, ...state.recipients ], newRecipient: '' }
  93. } else {
  94. return state
  95. }
  96. case 'SMS/SEND_REQUEST':
  97. return { ...state, sending: true }
  98. case 'SMS/SEND_SUCCESS':
  99. return { ...state, sending: false, recipients: [], newRecipient: '', body: '' }
  100. case 'SMS/SEND_FAILURE':
  101. return { ...state, sending: false, }
  102. case 'SMS/CREDIT_REQUEST':
  103. return { ...state, credit: '...' }
  104. case 'SMS/CREDIT_SUCCESS':
  105. return { ...state, credit: action.data }
  106. case 'SMS/CREDIT_FAILURE':
  107. return { ...state, credit: '?' }
  108. default:
  109. return state
  110. }
  111. }
  112. function * sendSMS (action) {
  113. try {
  114. const token = localStorage.getItem('accessToken')
  115. console.log('Send SMS requested', action, token)
  116. const { body, sender, recipients } = action.data
  117. const response = yield call(fetch, `${SZTM_API}/api/sms/send`, {
  118. method: 'POST',
  119. headers: {
  120. 'Content-Type': 'application/json',
  121. 'x-access-token': token
  122. },
  123. body: JSON.stringify({
  124. recipients,
  125. body,
  126. sender
  127. })
  128. })
  129. if (response.status !== 200) {
  130. console.log(response)
  131. throw Error(yield response.json())
  132. } else {
  133. const responseJson = yield response.json()
  134. yield put(actions.sendSMSSuccess(responseJson))
  135. }
  136. } catch (error) {
  137. console.log('Config failure!', actions.sendSMSFailure(error.toString()))
  138. yield put(actions.sendSMSFailure(error))
  139. }
  140. }
  141. function * getCredit (action) {
  142. console.log('getting credit.')
  143. yield put(api.actions.apiRequest({
  144. path: 'api/sms/credits',
  145. onSuccess: actions.getCreditSuccess,
  146. onFailure: actions.getCreditFailure
  147. }))
  148. }
  149. /** sagas are asynchronous workers (JS generators) to handle the state. */
  150. export function * saga () {
  151. console.log('SMS saga started.')
  152. yield takeEvery('SMS/SEND_REQUEST', sendSMS)
  153. yield takeEvery('SMS/CREDIT_REQUEST', getCredit)
  154. }