action-creator.async.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { FETCH_CONFIG } from './helper.js'
  2. import {
  3. TIMEZONE,
  4. setTimezone,
  5. LANG,
  6. updateLangList,
  7. USER_LOGIN,
  8. USER_LOGOUT,
  9. USER_DATA,
  10. USER_ROLE,
  11. USER_CONNECTED,
  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.mockApiUrl}/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.mockApiUrl}/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 postUserLogin = (login, password, rememberMe) => async dispatch => {
  85. return fetchWrapper({
  86. url: `${FETCH_CONFIG.mockApiUrl}/sessions/login`,
  87. param: {
  88. headers: {...FETCH_CONFIG.headers},
  89. method: 'POST',
  90. body: JSON.stringify({
  91. email: login,
  92. password: password,
  93. remember_me: rememberMe
  94. })
  95. },
  96. actionName: USER_LOGIN,
  97. dispatch
  98. })
  99. }
  100. export const postUserLogout = () => async dispatch => {
  101. return fetchWrapper({
  102. url: `${FETCH_CONFIG.mockApiUrl}/sessions/logout`,
  103. param: {
  104. headers: {...FETCH_CONFIG.headers},
  105. method: 'POST'
  106. },
  107. actionName: USER_LOGOUT,
  108. dispatch
  109. })
  110. }
  111. export const getUserIsConnected = () => async dispatch => {
  112. return fetchWrapper({
  113. url: `${FETCH_CONFIG.mockApiUrl}/sessions/whoami`,
  114. param: {...FETCH_CONFIG.header, method: 'GET'},
  115. actionName: USER_CONNECTED,
  116. dispatch
  117. })
  118. }
  119. export const getUserRole = user => async dispatch => {
  120. const fetchGetUserRole = await fetchWrapper({
  121. url: `${FETCH_CONFIG.mockApiUrl}/user/${user.id}/roles`,
  122. param: {...FETCH_CONFIG.header, method: 'GET'},
  123. actionName: USER_ROLE,
  124. dispatch
  125. })
  126. if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
  127. }
  128. export const updateUserLang = newLang => async dispatch => { // unused
  129. const fetchUpdateUserLang = await fetchWrapper({
  130. url: `${FETCH_CONFIG.mockApiUrl}/user`,
  131. param: {...FETCH_CONFIG.header, method: 'PATCH', body: JSON.stringify({lang: newLang})},
  132. actionName: USER_DATA,
  133. dispatch
  134. })
  135. if (fetchUpdateUserLang.status === 200) dispatch(updateUserData({lang: fetchUpdateUserLang.json.lang}))
  136. }
  137. // export const testResponseNoData = () => async dispatch => {
  138. // const fetchResponseNoData = await fetchWrapper({
  139. // url: 'http://localhost:3001/deletenodata',
  140. // param: {...FETCH_CONFIG.header, method: 'DELETE'},
  141. // actionName: 'TestNoData',
  142. // dispatch
  143. // })
  144. // console.log('jsonResponseNoData', fetchResponseNoData)
  145. // }
  146. export const getWorkspaceList = (userId, workspaceIdToOpen) => async dispatch => {
  147. const fetchGetWorkspaceList = await fetchWrapper({
  148. url: `${FETCH_CONFIG.mockApiUrl}/user/${userId}/workspace`,
  149. param: {...FETCH_CONFIG.header, method: 'GET'},
  150. actionName: WORKSPACE_LIST,
  151. dispatch
  152. })
  153. if (fetchGetWorkspaceList.status === 200) {
  154. dispatch(updateWorkspaceListData(fetchGetWorkspaceList.json))
  155. dispatch(setWorkspaceListIsOpenInSidebar(workspaceIdToOpen, true))
  156. }
  157. }
  158. export const getWorkspaceContent = workspaceId => dispatch => {
  159. return fetchWrapper({
  160. url: `${FETCH_CONFIG.mockApiUrl}/workspace/${workspaceId}`,
  161. param: {...FETCH_CONFIG.header, method: 'GET'},
  162. actionName: WORKSPACE,
  163. dispatch
  164. })
  165. }
  166. export const getFolderContent = (workspaceId, folderId) => async dispatch => {
  167. const fetchGetFolderContent = await fetchWrapper({
  168. url: `${FETCH_CONFIG.mockApiUrl}/workspace/${workspaceId}/folder/${folderId}`,
  169. param: {...FETCH_CONFIG.header, method: 'GET'},
  170. actionName: `${WORKSPACE}/${FOLDER}`,
  171. dispatch
  172. })
  173. if (fetchGetFolderContent.status === 200) dispatch(setFolderData(folderId, fetchGetFolderContent.json))
  174. }
  175. export const getAppList = () => async dispatch => {
  176. const fetchGetAppList = await fetchWrapper({
  177. url: `${FETCH_CONFIG.mockApiUrl}/app/config`,
  178. param: {...FETCH_CONFIG.header, method: 'GET'},
  179. actionName: APP_LIST,
  180. dispatch
  181. })
  182. if (fetchGetAppList.status === 200) dispatch(setAppList(fetchGetAppList.json))
  183. }