action-creator.async.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. WORKSPACE_DETAIL,
  15. WORKSPACE_MEMBER_LIST,
  16. FOLDER,
  17. setFolderData,
  18. APP_LIST,
  19. CONTENT_TYPE_LIST
  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. switch (fetchResult.status) {
  62. case 200:
  63. case 204:
  64. case 304:
  65. dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  66. break
  67. case 400:
  68. case 404:
  69. case 500:
  70. dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  71. break
  72. }
  73. return fetchResult
  74. }
  75. export const getLangList = () => async dispatch => {
  76. const fetchGetLangList = await fetchWrapper({
  77. url: `${FETCH_CONFIG.apiUrl}/lang`,
  78. param: {
  79. headers: {...FETCH_CONFIG.headers},
  80. method: 'GET'
  81. },
  82. actionName: LANG,
  83. dispatch
  84. })
  85. if (fetchGetLangList.status === 200) dispatch(updateLangList(fetchGetLangList.json))
  86. }
  87. export const getTimezone = () => async dispatch => {
  88. const fetchGetTimezone = await fetchWrapper({
  89. url: `${FETCH_CONFIG.apiUrl}/timezone`,
  90. param: {
  91. headers: {...FETCH_CONFIG.headers},
  92. method: 'GET'
  93. },
  94. actionName: TIMEZONE,
  95. dispatch
  96. })
  97. if (fetchGetTimezone.status === 200) dispatch(setTimezone(fetchGetTimezone.json))
  98. }
  99. export const postUserLogin = (login, password, rememberMe) => async dispatch => {
  100. return fetchWrapper({
  101. url: `${FETCH_CONFIG.apiUrl}/sessions/login`, // FETCH_CONFIG.apiUrl
  102. param: {
  103. headers: {...FETCH_CONFIG.headers},
  104. method: 'POST',
  105. body: JSON.stringify({
  106. email: login,
  107. password: password
  108. // remember_me: rememberMe
  109. })
  110. },
  111. actionName: USER_LOGIN,
  112. dispatch
  113. })
  114. }
  115. export const postUserLogout = () => async dispatch => {
  116. return fetchWrapper({
  117. url: `${FETCH_CONFIG.apiUrl}/sessions/logout`, // FETCH_CONFIG.apiUrl
  118. param: {
  119. headers: {...FETCH_CONFIG.headers},
  120. method: 'POST'
  121. },
  122. actionName: USER_LOGOUT,
  123. dispatch
  124. })
  125. }
  126. export const getUserIsConnected = user => async dispatch => {
  127. return fetchWrapper({
  128. url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`, // FETCH_CONFIG.apiUrl
  129. param: {
  130. headers: {
  131. ...FETCH_CONFIG.headers,
  132. 'Authorization': 'Basic ' + user.auth
  133. },
  134. method: 'GET'
  135. },
  136. actionName: USER_CONNECTED,
  137. dispatch
  138. })
  139. }
  140. export const getUserRole = user => async dispatch => {
  141. const fetchGetUserRole = await fetchWrapper({
  142. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/roles`,
  143. param: {
  144. headers: {...FETCH_CONFIG.headers},
  145. method: 'GET'
  146. },
  147. actionName: USER_ROLE,
  148. dispatch
  149. })
  150. if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
  151. }
  152. export const getWorkspaceList = user => dispatch => {
  153. return fetchWrapper({
  154. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces`,
  155. param: {
  156. headers: {
  157. ...FETCH_CONFIG.headers,
  158. 'Authorization': 'Basic ' + user.auth
  159. },
  160. method: 'GET'
  161. },
  162. actionName: WORKSPACE_LIST,
  163. dispatch
  164. })
  165. }
  166. export const getWorkspaceDetail = (user, idWorkspace) => dispatch => {
  167. return fetchWrapper({
  168. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}`,
  169. param: {
  170. headers: {
  171. ...FETCH_CONFIG.headers,
  172. 'Authorization': 'Basic ' + user.auth
  173. },
  174. method: 'GET'
  175. },
  176. actionName: WORKSPACE_DETAIL,
  177. dispatch
  178. })
  179. }
  180. export const getWorkspaceMemberList = (user, idWorkspace) => dispatch => {
  181. return fetchWrapper({
  182. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
  183. param: {
  184. headers: {
  185. ...FETCH_CONFIG.headers,
  186. 'Authorization': 'Basic ' + user.auth
  187. },
  188. method: 'GET'
  189. },
  190. actionName: WORKSPACE_MEMBER_LIST,
  191. dispatch
  192. })
  193. }
  194. export const getWorkspaceContentList = (user, idWorkspace, idParent) => dispatch => {
  195. return fetchWrapper({
  196. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents?parent_id=${idParent}`,
  197. param: {
  198. headers: {
  199. ...FETCH_CONFIG.headers,
  200. 'Authorization': 'Basic ' + user.auth
  201. },
  202. method: 'GET'
  203. },
  204. actionName: WORKSPACE,
  205. dispatch
  206. })
  207. }
  208. export const getFolderContent = (idWorkspace, idFolder) => async dispatch => {
  209. const fetchGetFolderContent = await fetchWrapper({
  210. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/?parent_id=${idFolder}`,
  211. param: {
  212. headers: {...FETCH_CONFIG.headers},
  213. method: 'GET'
  214. },
  215. actionName: `${WORKSPACE}/${FOLDER}`,
  216. dispatch
  217. })
  218. if (fetchGetFolderContent.status === 200) dispatch(setFolderData(idFolder, fetchGetFolderContent.json))
  219. }
  220. export const getAppList = user => dispatch => {
  221. console.log(user)
  222. return fetchWrapper({
  223. url: `${FETCH_CONFIG.apiUrl}/system/applications`,
  224. param: {
  225. headers: {
  226. ...FETCH_CONFIG.headers,
  227. 'Authorization': 'Basic ' + user.auth
  228. },
  229. method: 'GET',
  230. 'Authorization': 'Basic ' + user.auth
  231. },
  232. actionName: APP_LIST,
  233. dispatch
  234. })
  235. }
  236. export const getContentTypeList = user => dispatch => {
  237. return fetchWrapper({
  238. url: `${FETCH_CONFIG.apiUrl}/system/content_types`,
  239. param: {
  240. headers: {
  241. ...FETCH_CONFIG.headers,
  242. 'Authorization': 'Basic ' + user.auth
  243. },
  244. method: 'GET'
  245. },
  246. actionName: CONTENT_TYPE_LIST,
  247. dispatch
  248. })
  249. }