action-creator.async.js 6.3KB

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