action-creator.async.js 5.9KB

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