action-creator.async.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { FETCH_CONFIG } from './helper.js'
  2. import {
  3. USER_LOGIN,
  4. USER_DATA,
  5. USER_CONNECTED,
  6. updateUserConnected,
  7. updateUserData
  8. } from './action-creator.sync.js'
  9. /*
  10. * fetchWrapper(obj)
  11. *
  12. * Params:
  13. * An Object with the following attributes :
  14. * url - string - url of the end point to call
  15. * param - object - param to send with fetch call (eg. header)
  16. * param.method - string - REQUIRED - method of the http call
  17. * actionName - string - name of the action to dispatch with 'PENDING' and 'SUCCESS' respectively before and after the http request
  18. * dispatch - func - redux dispatcher function
  19. *
  20. * Returns:
  21. * An object Response generated by whatwg-fetch with a new property 'json' containing the data received or informations in case of failure
  22. *
  23. * This function create a http async request using whatwg-fetch while dispatching a PENDING and a SUCCESS redux action.
  24. * It also adds, to the Response of the fetch request, the json value so that the redux action have access to the status and the data
  25. */
  26. const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) => {
  27. dispatch({type: `${param.method}/${actionName}/PENDING`})
  28. const fetchResult = await fetch(url, param)
  29. fetchResult.json = await (async () => {
  30. switch (fetchResult.status) {
  31. case 200:
  32. case 304:
  33. return fetchResult.json()
  34. case 204:
  35. case 400:
  36. case 404:
  37. case 409:
  38. case 500:
  39. case 501:
  40. case 502:
  41. case 503:
  42. case 504:
  43. return '' // @TODO : handle errors
  44. }
  45. })()
  46. if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)
  47. if ([200, 204, 304].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  48. else if ([400, 404, 500].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  49. return fetchResult
  50. }
  51. export const userLogin = (login, password) => async dispatch => {
  52. const fetchUserLogin = await fetchWrapper({
  53. url: 'http://localhost:3001/user/login',
  54. param: {...FETCH_CONFIG, method: 'POST', body: JSON.stringify({login, password})},
  55. actionName: USER_LOGIN,
  56. dispatch
  57. })
  58. if (fetchUserLogin.status === 200) dispatch(updateUserConnected(fetchUserLogin.json))
  59. }
  60. export const getUserConnected = () => async dispatch => {
  61. const fetchUserLogged = await fetchWrapper({
  62. url: 'http://localhost:3001/user_logged',
  63. param: {...FETCH_CONFIG, method: 'GET'},
  64. actionName: USER_CONNECTED,
  65. dispatch
  66. })
  67. if (fetchUserLogged.status === 200) dispatch(updateUserConnected(fetchUserLogged.json))
  68. }
  69. export const updateUserLang = newLang => async dispatch => {
  70. const fetchUpdateUserLang = await fetchWrapper({
  71. url: 'http://localhost:3001/user',
  72. param: {...FETCH_CONFIG, method: 'PATCH', body: JSON.stringify({lang: newLang})},
  73. actionName: USER_DATA,
  74. dispatch
  75. })
  76. if (fetchUpdateUserLang.status === 200) dispatch(updateUserData({lang: fetchUpdateUserLang.json.lang}))
  77. }
  78. // export const testResponseNoData = () => async dispatch => {
  79. // const fetchResponseNoData = await fetchWrapper({
  80. // url: 'http://localhost:3001/deletenodata',
  81. // param: {...FETCH_CONFIG, method: 'DELETE'},
  82. // actionName: 'TestNoData',
  83. // dispatch
  84. // })
  85. // console.log('jsonResponseNoData', fetchResponseNoData)
  86. // }