action-creator.async.js 12KB

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