action-creator.async.js 4.2KB

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