action-creator.async.js 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_ROLE,
  10. USER_CONNECTED,
  11. setUserRole,
  12. WORKSPACE,
  13. WORKSPACE_LIST,
  14. FOLDER,
  15. setFolderData,
  16. APP_LIST,
  17. CONTENT_TYPE_LIST
  18. } from './action-creator.sync.js'
  19. /*
  20. * fetchWrapper(obj)
  21. *
  22. * Params:
  23. * An Object with the following attributes :
  24. * url - string - url of the end point to call
  25. * param - object - param to send with fetch call (eg. header)
  26. * param.method - string - REQUIRED - method of the http call
  27. * actionName - string - name of the action to dispatch with 'PENDING' and 'SUCCESS' respectively before and after the http request
  28. * dispatch - func - redux dispatcher function
  29. *
  30. * Returns:
  31. * An object Response generated by whatwg-fetch with a new property 'json' containing the data received or informations in case of failure
  32. *
  33. * This function create a http async request using whatwg-fetch while dispatching a PENDING and a SUCCESS redux action.
  34. * 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
  35. */
  36. const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) => {
  37. dispatch({type: `${param.method}/${actionName}/PENDING`})
  38. const fetchResult = await fetch(url, param)
  39. fetchResult.json = await (async () => {
  40. switch (fetchResult.status) {
  41. case 200:
  42. case 304:
  43. return fetchResult.json()
  44. case 204:
  45. case 400:
  46. case 404:
  47. case 409:
  48. case 500:
  49. case 501:
  50. case 502:
  51. case 503:
  52. case 504:
  53. return '' // @TODO : handle errors
  54. }
  55. })()
  56. if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)
  57. if ([200, 204, 304].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  58. else if ([400, 404, 500].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  59. return fetchResult
  60. }
  61. export const getLangList = () => async dispatch => {
  62. const fetchGetLangList = await fetchWrapper({
  63. url: `${FETCH_CONFIG.apiUrl}/lang`,
  64. param: {
  65. headers: {...FETCH_CONFIG.headers},
  66. method: 'GET'
  67. },
  68. actionName: LANG,
  69. dispatch
  70. })
  71. if (fetchGetLangList.status === 200) dispatch(updateLangList(fetchGetLangList.json))
  72. }
  73. export const getTimezone = () => async dispatch => {
  74. const fetchGetTimezone = await fetchWrapper({
  75. url: `${FETCH_CONFIG.apiUrl}/timezone`,
  76. param: {
  77. headers: {...FETCH_CONFIG.headers},
  78. method: 'GET'
  79. },
  80. actionName: TIMEZONE,
  81. dispatch
  82. })
  83. if (fetchGetTimezone.status === 200) dispatch(setTimezone(fetchGetTimezone.json))
  84. }
  85. export const postUserLogin = (login, password, rememberMe) => async dispatch => {
  86. return fetchWrapper({
  87. url: `${FETCH_CONFIG.apiUrl}/sessions/login`, // FETCH_CONFIG.apiUrl
  88. param: {
  89. headers: {...FETCH_CONFIG.headers},
  90. method: 'POST',
  91. body: JSON.stringify({
  92. email: login,
  93. password: password,
  94. remember_me: rememberMe
  95. })
  96. },
  97. actionName: USER_LOGIN,
  98. dispatch
  99. })
  100. }
  101. export const postUserLogout = () => async dispatch => {
  102. return fetchWrapper({
  103. url: `${FETCH_CONFIG.apiUrl}/sessions/logout`, // FETCH_CONFIG.apiUrl
  104. param: {
  105. headers: {...FETCH_CONFIG.headers},
  106. method: 'POST'
  107. },
  108. actionName: USER_LOGOUT,
  109. dispatch
  110. })
  111. }
  112. export const getUserIsConnected = () => async dispatch => {
  113. return fetchWrapper({
  114. url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`, // FETCH_CONFIG.apiUrl
  115. param: {
  116. headers: {...FETCH_CONFIG.headers},
  117. method: 'GET'
  118. },
  119. actionName: USER_CONNECTED,
  120. dispatch
  121. })
  122. }
  123. export const getUserRole = user => async dispatch => {
  124. const fetchGetUserRole = await fetchWrapper({
  125. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/roles`,
  126. param: {
  127. headers: {...FETCH_CONFIG.headers},
  128. method: 'GET'
  129. },
  130. actionName: USER_ROLE,
  131. dispatch
  132. })
  133. if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
  134. }
  135. export const getWorkspaceList = idUser => dispatch => {
  136. return fetchWrapper({
  137. url: `${FETCH_CONFIG.apiUrl}/users/${idUser}/workspaces`,
  138. param: {
  139. headers: {...FETCH_CONFIG.headers},
  140. method: 'GET'
  141. },
  142. actionName: WORKSPACE_LIST,
  143. dispatch
  144. })
  145. }
  146. export const getWorkspaceContentList = idWorkspace => dispatch => {
  147. return fetchWrapper({
  148. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents`,
  149. param: {
  150. headers: {...FETCH_CONFIG.headers},
  151. method: 'GET'
  152. },
  153. actionName: WORKSPACE,
  154. dispatch
  155. })
  156. }
  157. export const getWorkspaceContent = (idWorkspace, idContent) => dispatch => {
  158. return fetchWrapper({
  159. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}`,
  160. param: {
  161. headers: {...FETCH_CONFIG.headers},
  162. method: 'GET'
  163. },
  164. actionName: WORKSPACE,
  165. dispatch
  166. })
  167. }
  168. export const getFolderContent = (idWorkspace, idFolder) => async dispatch => {
  169. const fetchGetFolderContent = await fetchWrapper({
  170. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/?parent_id=${idFolder}`,
  171. param: {
  172. headers: {...FETCH_CONFIG.headers},
  173. method: 'GET'
  174. },
  175. actionName: `${WORKSPACE}/${FOLDER}`,
  176. dispatch
  177. })
  178. if (fetchGetFolderContent.status === 200) dispatch(setFolderData(idFolder, fetchGetFolderContent.json))
  179. }
  180. export const getAppList = () => dispatch => {
  181. return fetchWrapper({
  182. url: `${FETCH_CONFIG.apiUrl}/system/applications`,
  183. param: {
  184. headers: {...FETCH_CONFIG.headers},
  185. method: 'GET'
  186. },
  187. actionName: APP_LIST,
  188. dispatch
  189. })
  190. }
  191. export const getContentTypeList = () => dispatch => {
  192. return fetchWrapper({
  193. url: `${FETCH_CONFIG.apiUrl}/system/content_types`,
  194. param: {
  195. headers: {...FETCH_CONFIG.headers},
  196. method: 'GET'
  197. },
  198. actionName: CONTENT_TYPE_LIST,
  199. dispatch
  200. })
  201. }