action-creator.async.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 = user => async dispatch => {
  113. return fetchWrapper({
  114. url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`, // FETCH_CONFIG.apiUrl
  115. param: {
  116. headers: {
  117. ...FETCH_CONFIG.headers,
  118. 'Authorization': 'Basic ' + user.auth
  119. },
  120. method: 'GET'
  121. },
  122. actionName: USER_CONNECTED,
  123. dispatch
  124. })
  125. }
  126. export const getUserRole = user => async dispatch => {
  127. const fetchGetUserRole = await fetchWrapper({
  128. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/roles`,
  129. param: {
  130. headers: {...FETCH_CONFIG.headers},
  131. method: 'GET'
  132. },
  133. actionName: USER_ROLE,
  134. dispatch
  135. })
  136. if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
  137. }
  138. export const getWorkspaceList = user => dispatch => {
  139. return fetchWrapper({
  140. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces`,
  141. param: {
  142. headers: {
  143. ...FETCH_CONFIG.headers,
  144. 'Authorization': 'Basic ' + user.auth
  145. },
  146. method: 'GET'
  147. },
  148. actionName: WORKSPACE_LIST,
  149. dispatch
  150. })
  151. }
  152. export const getWorkspaceContentList = (user, idWorkspace, idParent) => dispatch => {
  153. return fetchWrapper({
  154. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents?parent_id=${idParent}`,
  155. param: {
  156. headers: {
  157. ...FETCH_CONFIG.headers,
  158. 'Authorization': 'Basic ' + user.auth
  159. },
  160. method: 'GET'
  161. },
  162. actionName: WORKSPACE,
  163. dispatch
  164. })
  165. }
  166. export const getWorkspaceContent = (idWorkspace, idContent) => dispatch => {
  167. return fetchWrapper({
  168. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}`,
  169. param: {
  170. headers: {...FETCH_CONFIG.headers},
  171. method: 'GET'
  172. },
  173. actionName: WORKSPACE,
  174. dispatch
  175. })
  176. }
  177. export const getFolderContent = (idWorkspace, idFolder) => async dispatch => {
  178. const fetchGetFolderContent = await fetchWrapper({
  179. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/?parent_id=${idFolder}`,
  180. param: {
  181. headers: {...FETCH_CONFIG.headers},
  182. method: 'GET'
  183. },
  184. actionName: `${WORKSPACE}/${FOLDER}`,
  185. dispatch
  186. })
  187. if (fetchGetFolderContent.status === 200) dispatch(setFolderData(idFolder, fetchGetFolderContent.json))
  188. }
  189. export const getAppList = user => dispatch => {
  190. console.log(user)
  191. return fetchWrapper({
  192. url: `${FETCH_CONFIG.apiUrl}/system/applications`,
  193. param: {
  194. headers: {
  195. ...FETCH_CONFIG.headers,
  196. 'Authorization': 'Basic ' + user.auth
  197. },
  198. method: 'GET',
  199. 'Authorization': 'Basic ' + user.auth
  200. },
  201. actionName: APP_LIST,
  202. dispatch
  203. })
  204. }
  205. export const getContentTypeList = user => dispatch => {
  206. return fetchWrapper({
  207. url: `${FETCH_CONFIG.apiUrl}/system/content_types`,
  208. param: {
  209. headers: {
  210. ...FETCH_CONFIG.headers,
  211. 'Authorization': 'Basic ' + user.auth
  212. },
  213. method: 'GET'
  214. },
  215. actionName: CONTENT_TYPE_LIST,
  216. dispatch
  217. })
  218. }