action-creator.async.js 5.9KB

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