action-creator.async.js 4.6KB

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