action-creator.async.js 3.9KB

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