action-creator.async.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import { FETCH_CONFIG } from './helper.js'
  2. import {
  3. TIMEZONE,
  4. setTimezone,
  5. USER_LOGIN,
  6. USER_LOGOUT,
  7. USER_ROLE,
  8. USER_CONNECTED,
  9. USER_KNOWN_MEMBER_LIST,
  10. setUserRole,
  11. WORKSPACE,
  12. WORKSPACE_LIST,
  13. WORKSPACE_DETAIL,
  14. WORKSPACE_MEMBER_LIST,
  15. WORKSPACE_MEMBER_ADD,
  16. FOLDER,
  17. setFolderData,
  18. APP_LIST,
  19. CONTENT_TYPE_LIST,
  20. WORKSPACE_CONTENT_ARCHIVED,
  21. WORKSPACE_CONTENT_DELETED,
  22. WORKSPACE_RECENT_ACTIVITY,
  23. WORKSPACE_READ_STATUS
  24. } from './action-creator.sync.js'
  25. /*
  26. * fetchWrapper(obj)
  27. *
  28. * Params:
  29. * An Object with the following attributes :
  30. * url - string - url of the end point to call
  31. * param - object - param to send with fetch call (eg. header)
  32. * param.method - string - REQUIRED - method of the http call
  33. * actionName - string - name of the action to dispatch with 'PENDING' and 'SUCCESS' respectively before and after the http request
  34. * dispatch - func - redux dispatcher function
  35. *
  36. * Returns:
  37. * An object Response generated by whatwg-fetch with a new property 'json' containing the data received or informations in case of failure
  38. *
  39. * This function create a http async request using whatwg-fetch while dispatching a PENDING and a SUCCESS redux action.
  40. * 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
  41. */
  42. // Côme - 2018/08/02 - fetchWrapper should come from tracim_lib so that all apps uses the same
  43. const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) => {
  44. dispatch({type: `${param.method}/${actionName}/PENDING`})
  45. const fetchResult = await fetch(url, param)
  46. fetchResult.json = await (async () => {
  47. switch (fetchResult.status) {
  48. case 200:
  49. case 304:
  50. return fetchResult.json()
  51. case 204:
  52. case 400:
  53. case 404:
  54. case 409:
  55. case 500:
  56. case 501:
  57. case 502:
  58. case 503:
  59. case 504:
  60. return '' // @TODO : handle errors
  61. }
  62. })()
  63. if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)
  64. // if ([200, 204, 304].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  65. // else if ([400, 404, 500].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  66. switch (fetchResult.status) {
  67. case 200:
  68. case 204:
  69. case 304:
  70. dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  71. break
  72. case 400:
  73. case 404:
  74. case 500:
  75. dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  76. break
  77. }
  78. return fetchResult
  79. }
  80. export const getTimezone = () => async dispatch => {
  81. const fetchGetTimezone = await fetchWrapper({
  82. url: `${FETCH_CONFIG.apiUrl}/timezone`,
  83. param: {
  84. headers: {...FETCH_CONFIG.headers},
  85. method: 'GET'
  86. },
  87. actionName: TIMEZONE,
  88. dispatch
  89. })
  90. if (fetchGetTimezone.status === 200) dispatch(setTimezone(fetchGetTimezone.json))
  91. }
  92. export const postUserLogin = (login, password, rememberMe) => async dispatch => {
  93. return fetchWrapper({
  94. url: `${FETCH_CONFIG.apiUrl}/sessions/login`, // FETCH_CONFIG.apiUrl
  95. param: {
  96. headers: {...FETCH_CONFIG.headers},
  97. method: 'POST',
  98. body: JSON.stringify({
  99. email: login,
  100. password: password
  101. // remember_me: rememberMe
  102. })
  103. },
  104. actionName: USER_LOGIN,
  105. dispatch
  106. })
  107. }
  108. export const postUserLogout = () => async dispatch => {
  109. return fetchWrapper({
  110. url: `${FETCH_CONFIG.apiUrl}/sessions/logout`, // FETCH_CONFIG.apiUrl
  111. param: {
  112. headers: {...FETCH_CONFIG.headers},
  113. method: 'POST'
  114. },
  115. actionName: USER_LOGOUT,
  116. dispatch
  117. })
  118. }
  119. export const getUserIsConnected = user => async dispatch => {
  120. return fetchWrapper({
  121. url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`, // FETCH_CONFIG.apiUrl
  122. param: {
  123. headers: {
  124. ...FETCH_CONFIG.headers,
  125. 'Authorization': 'Basic ' + user.auth
  126. },
  127. method: 'GET'
  128. },
  129. actionName: USER_CONNECTED,
  130. dispatch
  131. })
  132. }
  133. export const getUserRole = user => async dispatch => {
  134. const fetchGetUserRole = await fetchWrapper({
  135. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/roles`,
  136. param: {
  137. headers: {...FETCH_CONFIG.headers},
  138. method: 'GET'
  139. },
  140. actionName: USER_ROLE,
  141. dispatch
  142. })
  143. if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
  144. }
  145. export const getUserKnownMember = (user, userNameToSearch) => dispatch => {
  146. return fetchWrapper({
  147. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/known_members?acp=${userNameToSearch}`,
  148. param: {
  149. headers: {
  150. ...FETCH_CONFIG.headers,
  151. 'Authorization': 'Basic ' + user.auth
  152. },
  153. method: 'GET'
  154. },
  155. actionName: USER_KNOWN_MEMBER_LIST,
  156. dispatch
  157. })
  158. }
  159. export const putUserWorkspaceRead = (user, idWorkspace) => dispatch => {
  160. return fetchWrapper({
  161. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/read`,
  162. param: {
  163. headers: {
  164. ...FETCH_CONFIG.headers,
  165. 'Authorization': 'Basic ' + user.auth
  166. },
  167. method: 'PUT'
  168. },
  169. actionName: USER_KNOWN_MEMBER_LIST,
  170. dispatch
  171. })
  172. }
  173. export const getWorkspaceList = user => dispatch => {
  174. return fetchWrapper({
  175. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces`,
  176. param: {
  177. headers: {
  178. ...FETCH_CONFIG.headers,
  179. 'Authorization': 'Basic ' + user.auth
  180. },
  181. method: 'GET'
  182. },
  183. actionName: WORKSPACE_LIST,
  184. dispatch
  185. })
  186. }
  187. export const getWorkspaceDetail = (user, idWorkspace) => dispatch => {
  188. return fetchWrapper({
  189. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}`,
  190. param: {
  191. headers: {
  192. ...FETCH_CONFIG.headers,
  193. 'Authorization': 'Basic ' + user.auth
  194. },
  195. method: 'GET'
  196. },
  197. actionName: WORKSPACE_DETAIL,
  198. dispatch
  199. })
  200. }
  201. export const getWorkspaceMemberList = (user, idWorkspace) => dispatch => {
  202. return fetchWrapper({
  203. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
  204. param: {
  205. headers: {
  206. ...FETCH_CONFIG.headers,
  207. 'Authorization': 'Basic ' + user.auth
  208. },
  209. method: 'GET'
  210. },
  211. actionName: WORKSPACE_MEMBER_LIST,
  212. dispatch
  213. })
  214. }
  215. export const getWorkspaceContentList = (user, idWorkspace, idParent) => dispatch => {
  216. return fetchWrapper({
  217. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents?parent_id=${idParent}`,
  218. param: {
  219. headers: {
  220. ...FETCH_CONFIG.headers,
  221. 'Authorization': 'Basic ' + user.auth
  222. },
  223. method: 'GET'
  224. },
  225. actionName: WORKSPACE,
  226. dispatch
  227. })
  228. }
  229. export const getWorkspaceRecentActivityList = (user, idWorkspace, beforeId = null) => dispatch => {
  230. return fetchWrapper({
  231. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/contents/recently_active?limit=10${beforeId ? `&before_content_id=${beforeId}` : ''}`,
  232. param: {
  233. headers: {
  234. ...FETCH_CONFIG.headers,
  235. 'Authorization': 'Basic ' + user.auth
  236. },
  237. method: 'GET'
  238. },
  239. actionName: WORKSPACE_RECENT_ACTIVITY,
  240. dispatch
  241. })
  242. }
  243. export const getWorkspaceReadStatusList = (user, idWorkspace) => dispatch => {
  244. return fetchWrapper({
  245. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/contents/read_status`,
  246. param: {
  247. headers: {
  248. ...FETCH_CONFIG.headers,
  249. 'Authorization': 'Basic ' + user.auth
  250. },
  251. method: 'GET'
  252. },
  253. actionName: WORKSPACE_READ_STATUS,
  254. dispatch
  255. })
  256. }
  257. export const postWorkspaceMember = (user, idWorkspace, newMember) => dispatch => {
  258. return fetchWrapper({
  259. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
  260. param: {
  261. headers: {
  262. ...FETCH_CONFIG.headers,
  263. 'Authorization': 'Basic ' + user.auth
  264. },
  265. method: 'POST',
  266. body: JSON.stringify({
  267. user_id: newMember.id,
  268. user_email_or_public_name: newMember.name,
  269. role: newMember.role
  270. })
  271. },
  272. actionName: WORKSPACE_MEMBER_ADD,
  273. dispatch
  274. })
  275. }
  276. export const getFolderContent = (idWorkspace, idFolder) => async dispatch => {
  277. const fetchGetFolderContent = await fetchWrapper({
  278. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/?parent_id=${idFolder}`,
  279. param: {
  280. headers: {...FETCH_CONFIG.headers},
  281. method: 'GET'
  282. },
  283. actionName: `${WORKSPACE}/${FOLDER}`,
  284. dispatch
  285. })
  286. if (fetchGetFolderContent.status === 200) dispatch(setFolderData(idFolder, fetchGetFolderContent.json))
  287. }
  288. export const getAppList = user => dispatch => {
  289. return fetchWrapper({
  290. url: `${FETCH_CONFIG.apiUrl}/system/applications`,
  291. param: {
  292. headers: {
  293. ...FETCH_CONFIG.headers,
  294. 'Authorization': 'Basic ' + user.auth
  295. },
  296. method: 'GET'
  297. },
  298. actionName: APP_LIST,
  299. dispatch
  300. })
  301. }
  302. export const getContentTypeList = user => dispatch => {
  303. return fetchWrapper({
  304. url: `${FETCH_CONFIG.apiUrl}/system/content_types`,
  305. param: {
  306. headers: {
  307. ...FETCH_CONFIG.headers,
  308. 'Authorization': 'Basic ' + user.auth
  309. },
  310. method: 'GET'
  311. },
  312. actionName: CONTENT_TYPE_LIST,
  313. dispatch
  314. })
  315. }
  316. export const putWorkspaceContentArchived = (user, idWorkspace, idContent) => dispatch => {
  317. return fetchWrapper({
  318. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/archive`,
  319. param: {
  320. headers: {
  321. ...FETCH_CONFIG.headers,
  322. 'Authorization': 'Basic ' + user.auth
  323. },
  324. method: 'PUT'
  325. },
  326. actionName: WORKSPACE_CONTENT_ARCHIVED,
  327. dispatch
  328. })
  329. }
  330. export const putWorkspaceContentDeleted = (user, idWorkspace, idContent) => dispatch => {
  331. return fetchWrapper({
  332. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/delete`,
  333. param: {
  334. headers: {
  335. ...FETCH_CONFIG.headers,
  336. 'Authorization': 'Basic ' + user.auth
  337. },
  338. method: 'PUT'
  339. },
  340. actionName: WORKSPACE_CONTENT_DELETED,
  341. dispatch
  342. })
  343. }