action-creator.async.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { FETCH_CONFIG } from './helper.js'
  2. import {
  3. TIMEZONE,
  4. setTimezone,
  5. LANG,
  6. updateLangList,
  7. USER_LOGIN,
  8. USER_DATA,
  9. USER_ROLE,
  10. USER_CONNECTED,
  11. setUserConnected,
  12. updateUserData,
  13. setUserRole,
  14. WORKSPACE,
  15. setWorkspaceData,
  16. WORKSPACE_LIST,
  17. updateWorkspaceListData,
  18. APP_LIST,
  19. setAppList, setWorkspaceListIsOpenInSidebar
  20. } from './action-creator.sync.js'
  21. /*
  22. * fetchWrapper(obj)
  23. *
  24. * Params:
  25. * An Object with the following attributes :
  26. * url - string - url of the end point to call
  27. * param - object - param to send with fetch call (eg. header)
  28. * param.method - string - REQUIRED - method of the http call
  29. * actionName - string - name of the action to dispatch with 'PENDING' and 'SUCCESS' respectively before and after the http request
  30. * dispatch - func - redux dispatcher function
  31. *
  32. * Returns:
  33. * An object Response generated by whatwg-fetch with a new property 'json' containing the data received or informations in case of failure
  34. *
  35. * This function create a http async request using whatwg-fetch while dispatching a PENDING and a SUCCESS redux action.
  36. * 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
  37. */
  38. const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) => {
  39. dispatch({type: `${param.method}/${actionName}/PENDING`})
  40. const fetchResult = await fetch(url, param)
  41. fetchResult.json = await (async () => {
  42. switch (fetchResult.status) {
  43. case 200:
  44. case 304:
  45. return fetchResult.json()
  46. case 204:
  47. case 400:
  48. case 404:
  49. case 409:
  50. case 500:
  51. case 501:
  52. case 502:
  53. case 503:
  54. case 504:
  55. return '' // @TODO : handle errors
  56. }
  57. })()
  58. if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)
  59. if ([200, 204, 304].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  60. else if ([400, 404, 500].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  61. return fetchResult
  62. }
  63. export const getLangList = () => async dispatch => {
  64. const fetchGetLangList = await fetchWrapper({
  65. url: `${FETCH_CONFIG.apiUrl}/lang`,
  66. param: {...FETCH_CONFIG.header, method: 'GET'},
  67. actionName: LANG,
  68. dispatch
  69. })
  70. if (fetchGetLangList.status === 200) dispatch(updateLangList(fetchGetLangList.json))
  71. }
  72. export const getTimezone = () => async dispatch => {
  73. const fetchGetTimezone = await fetchWrapper({
  74. url: `${FETCH_CONFIG.apiUrl}/timezone`,
  75. param: {...FETCH_CONFIG.header, method: 'GET'},
  76. actionName: TIMEZONE,
  77. dispatch
  78. })
  79. if (fetchGetTimezone.status === 200) dispatch(setTimezone(fetchGetTimezone.json))
  80. }
  81. export const userLogin = (login, password, rememberMe) => async dispatch => {
  82. const fetchUserLogin = await fetchWrapper({
  83. url: `${FETCH_CONFIG.apiUrl}/user/login`,
  84. param: {
  85. ...FETCH_CONFIG.header,
  86. method: 'POST',
  87. body: JSON.stringify({
  88. login,
  89. password,
  90. remember_me: rememberMe
  91. })
  92. },
  93. actionName: USER_LOGIN,
  94. dispatch
  95. })
  96. if (fetchUserLogin.status === 200) dispatch(setUserConnected(fetchUserLogin.json))
  97. }
  98. export const getIsUserConnected = () => async dispatch => {
  99. const fetchUserLogged = await fetchWrapper({
  100. url: `${FETCH_CONFIG.apiUrl}/user/is_logged_in`,
  101. param: {...FETCH_CONFIG.header, method: 'GET'},
  102. actionName: USER_CONNECTED,
  103. dispatch
  104. })
  105. if (fetchUserLogged.status === 200) dispatch(setUserConnected(fetchUserLogged.json))
  106. }
  107. export const getUserRole = user => async dispatch => {
  108. const fetchGetUserRole = await fetchWrapper({
  109. url: `${FETCH_CONFIG.apiUrl}/user/${user.id}/roles`,
  110. param: {...FETCH_CONFIG.header, method: 'GET'},
  111. actionName: USER_ROLE,
  112. dispatch
  113. })
  114. if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
  115. }
  116. export const updateUserLang = newLang => async dispatch => { // unused
  117. const fetchUpdateUserLang = await fetchWrapper({
  118. url: `${FETCH_CONFIG.apiUrl}/user`,
  119. param: {...FETCH_CONFIG.header, method: 'PATCH', body: JSON.stringify({lang: newLang})},
  120. actionName: USER_DATA,
  121. dispatch
  122. })
  123. if (fetchUpdateUserLang.status === 200) dispatch(updateUserData({lang: fetchUpdateUserLang.json.lang}))
  124. }
  125. // export const testResponseNoData = () => async dispatch => {
  126. // const fetchResponseNoData = await fetchWrapper({
  127. // url: 'http://localhost:3001/deletenodata',
  128. // param: {...FETCH_CONFIG.header, method: 'DELETE'},
  129. // actionName: 'TestNoData',
  130. // dispatch
  131. // })
  132. // console.log('jsonResponseNoData', fetchResponseNoData)
  133. // }
  134. export const getWorkspaceList = (userId, workspaceIdToOpen) => async dispatch => {
  135. const fetchGetWorkspaceList = await fetchWrapper({
  136. url: `${FETCH_CONFIG.apiUrl}/user/${userId}/workspace`,
  137. param: {...FETCH_CONFIG.header, method: 'GET'},
  138. actionName: WORKSPACE_LIST,
  139. dispatch
  140. })
  141. if (fetchGetWorkspaceList.status === 200) {
  142. dispatch(updateWorkspaceListData(fetchGetWorkspaceList.json))
  143. dispatch(setWorkspaceListIsOpenInSidebar(workspaceIdToOpen, true))
  144. }
  145. }
  146. export const getWorkspaceContent = (workspaceId, filterStr) => async dispatch => {
  147. const fetchGetWorkspaceContent = await fetchWrapper({
  148. url: `${FETCH_CONFIG.apiUrl}/workspace/${workspaceId}`,
  149. param: {...FETCH_CONFIG.header, method: 'GET'},
  150. actionName: WORKSPACE,
  151. dispatch
  152. })
  153. if (fetchGetWorkspaceContent.status === 200) dispatch(setWorkspaceData(fetchGetWorkspaceContent.json, filterStr))
  154. }
  155. export const getAppList = () => async dispatch => {
  156. const fetchGetAppList = await fetchWrapper({
  157. url: `${FETCH_CONFIG.apiUrl}/app/config`,
  158. param: {...FETCH_CONFIG.header, method: 'GET'},
  159. actionName: APP_LIST,
  160. dispatch
  161. })
  162. if (fetchGetAppList.status === 200) dispatch(setAppList(fetchGetAppList.json))
  163. }