action-creator.async.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. WORKSPACE,
  11. WORKSPACE_LIST,
  12. WORKSPACE_DETAIL,
  13. WORKSPACE_MEMBER_LIST,
  14. WORKSPACE_MEMBER_ADD,
  15. FOLDER,
  16. setFolderData,
  17. APP_LIST,
  18. CONTENT_TYPE_LIST,
  19. WORKSPACE_CONTENT_ARCHIVED,
  20. WORKSPACE_CONTENT_DELETED,
  21. WORKSPACE_RECENT_ACTIVITY,
  22. WORKSPACE_READ_STATUS,
  23. USER_WORKSPACE_DO_NOTIFY
  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 401:
  74. case 404:
  75. case 500:
  76. dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  77. break
  78. }
  79. return fetchResult
  80. }
  81. export const postUserLogin = (login, password, rememberMe) => async dispatch => {
  82. return fetchWrapper({
  83. url: `${FETCH_CONFIG.apiUrl}/sessions/login`, // FETCH_CONFIG.apiUrl
  84. param: {
  85. headers: {...FETCH_CONFIG.headers},
  86. method: 'POST',
  87. body: JSON.stringify({
  88. email: login,
  89. password: password
  90. // remember_me: rememberMe
  91. })
  92. },
  93. actionName: USER_LOGIN,
  94. dispatch
  95. })
  96. }
  97. export const postUserLogout = () => async dispatch => {
  98. return fetchWrapper({
  99. url: `${FETCH_CONFIG.apiUrl}/sessions/logout`, // FETCH_CONFIG.apiUrl
  100. param: {
  101. headers: {...FETCH_CONFIG.headers},
  102. method: 'POST'
  103. },
  104. actionName: USER_LOGOUT,
  105. dispatch
  106. })
  107. }
  108. export const getUserIsConnected = user => async dispatch => {
  109. return fetchWrapper({
  110. url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`, // FETCH_CONFIG.apiUrl
  111. param: {
  112. headers: {
  113. ...FETCH_CONFIG.headers,
  114. 'Authorization': 'Basic ' + user.auth
  115. },
  116. method: 'GET'
  117. },
  118. actionName: USER_CONNECTED,
  119. dispatch
  120. })
  121. }
  122. export const getUserKnownMember = (user, userNameToSearch) => dispatch => {
  123. return fetchWrapper({
  124. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/known_members?acp=${userNameToSearch}`,
  125. param: {
  126. headers: {
  127. ...FETCH_CONFIG.headers,
  128. 'Authorization': 'Basic ' + user.auth
  129. },
  130. method: 'GET'
  131. },
  132. actionName: USER_KNOWN_MEMBER_LIST,
  133. dispatch
  134. })
  135. }
  136. export const putUserName = (user, newName) => dispatch => {
  137. return fetchWrapper({
  138. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}`,
  139. param: {
  140. headers: {
  141. ...FETCH_CONFIG.headers,
  142. 'Authorization': 'Basic ' + user.auth
  143. },
  144. method: 'PUT',
  145. body: JSON.stringify({
  146. public_name: newName,
  147. timezone: user.timezone
  148. })
  149. },
  150. actionName: USER_NAME,
  151. dispatch
  152. })
  153. }
  154. export const putUserEmail = (user, newEmail, checkPassword) => dispatch => {
  155. return fetchWrapper({
  156. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/email`,
  157. param: {
  158. headers: {
  159. ...FETCH_CONFIG.headers,
  160. 'Authorization': 'Basic ' + user.auth
  161. },
  162. method: 'PUT',
  163. body: JSON.stringify({
  164. email: newEmail,
  165. loggedin_user_password: checkPassword
  166. })
  167. },
  168. actionName: USER_EMAIL,
  169. dispatch
  170. })
  171. }
  172. export const putUserPassword = (user, oldPassword, newPassword, newPassword2) => dispatch => {
  173. return fetchWrapper({
  174. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/password`,
  175. param: {
  176. headers: {
  177. ...FETCH_CONFIG.headers,
  178. 'Authorization': 'Basic ' + user.auth
  179. },
  180. method: 'PUT',
  181. body: JSON.stringify({
  182. loggedin_user_password: oldPassword,
  183. new_password: newPassword,
  184. new_password2: newPassword2
  185. })
  186. },
  187. actionName: USER_PASSWORD,
  188. dispatch
  189. })
  190. }
  191. export const putUserWorkspaceRead = (user, idWorkspace) => dispatch => {
  192. return fetchWrapper({
  193. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/read`,
  194. param: {
  195. headers: {
  196. ...FETCH_CONFIG.headers,
  197. 'Authorization': 'Basic ' + user.auth
  198. },
  199. method: 'PUT'
  200. },
  201. actionName: USER_KNOWN_MEMBER_LIST,
  202. dispatch
  203. })
  204. }
  205. export const putUserWorkspaceDoNotify = (user, idWorkspace, doNotify) => dispatch => {
  206. return fetchWrapper({
  207. // @TODO Côme - 2018/08/23 - this is the wrong endpoint, but backend hasn't implemented it yet
  208. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members/${user.user_id}`,
  209. param: {
  210. headers: {
  211. ...FETCH_CONFIG.headers,
  212. 'Authorization': 'Basic ' + user.auth
  213. },
  214. method: 'PUT',
  215. body: JSON.stringify({
  216. do_notify: doNotify
  217. })
  218. },
  219. actionName: USER_WORKSPACE_DO_NOTIFY,
  220. dispatch
  221. })
  222. }
  223. export const getWorkspaceList = user => dispatch => {
  224. return fetchWrapper({
  225. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces`,
  226. param: {
  227. headers: {
  228. ...FETCH_CONFIG.headers,
  229. 'Authorization': 'Basic ' + user.auth
  230. },
  231. method: 'GET'
  232. },
  233. actionName: WORKSPACE_LIST,
  234. dispatch
  235. })
  236. }
  237. export const getWorkspaceDetail = (user, idWorkspace) => dispatch => {
  238. return fetchWrapper({
  239. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}`,
  240. param: {
  241. headers: {
  242. ...FETCH_CONFIG.headers,
  243. 'Authorization': 'Basic ' + user.auth
  244. },
  245. method: 'GET'
  246. },
  247. actionName: WORKSPACE_DETAIL,
  248. dispatch
  249. })
  250. }
  251. export const getWorkspaceMemberList = (user, idWorkspace) => dispatch => {
  252. return fetchWrapper({
  253. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
  254. param: {
  255. headers: {
  256. ...FETCH_CONFIG.headers,
  257. 'Authorization': 'Basic ' + user.auth
  258. },
  259. method: 'GET'
  260. },
  261. actionName: WORKSPACE_MEMBER_LIST,
  262. dispatch
  263. })
  264. }
  265. export const getWorkspaceContentList = (user, idWorkspace, idParent) => dispatch => {
  266. return fetchWrapper({
  267. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents?parent_id=${idParent}`,
  268. param: {
  269. headers: {
  270. ...FETCH_CONFIG.headers,
  271. 'Authorization': 'Basic ' + user.auth
  272. },
  273. method: 'GET'
  274. },
  275. actionName: WORKSPACE,
  276. dispatch
  277. })
  278. }
  279. export const getWorkspaceRecentActivityList = (user, idWorkspace, beforeId = null) => dispatch => {
  280. return fetchWrapper({
  281. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/contents/recently_active?limit=10${beforeId ? `&before_content_id=${beforeId}` : ''}`,
  282. param: {
  283. headers: {
  284. ...FETCH_CONFIG.headers,
  285. 'Authorization': 'Basic ' + user.auth
  286. },
  287. method: 'GET'
  288. },
  289. actionName: WORKSPACE_RECENT_ACTIVITY,
  290. dispatch
  291. })
  292. }
  293. export const getWorkspaceReadStatusList = (user, idWorkspace) => dispatch => {
  294. return fetchWrapper({
  295. url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces/${idWorkspace}/contents/read_status`,
  296. param: {
  297. headers: {
  298. ...FETCH_CONFIG.headers,
  299. 'Authorization': 'Basic ' + user.auth
  300. },
  301. method: 'GET'
  302. },
  303. actionName: WORKSPACE_READ_STATUS,
  304. dispatch
  305. })
  306. }
  307. export const postWorkspaceMember = (user, idWorkspace, newMember) => dispatch => {
  308. return fetchWrapper({
  309. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
  310. param: {
  311. headers: {
  312. ...FETCH_CONFIG.headers,
  313. 'Authorization': 'Basic ' + user.auth
  314. },
  315. method: 'POST',
  316. body: JSON.stringify({
  317. user_id: newMember.id,
  318. user_email_or_public_name: newMember.name,
  319. role: newMember.role
  320. })
  321. },
  322. actionName: WORKSPACE_MEMBER_ADD,
  323. dispatch
  324. })
  325. }
  326. export const getFolderContent = (idWorkspace, idFolder) => async dispatch => {
  327. const fetchGetFolderContent = await fetchWrapper({
  328. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/?parent_id=${idFolder}`,
  329. param: {
  330. headers: {...FETCH_CONFIG.headers},
  331. method: 'GET'
  332. },
  333. actionName: `${WORKSPACE}/${FOLDER}`,
  334. dispatch
  335. })
  336. if (fetchGetFolderContent.status === 200) dispatch(setFolderData(idFolder, fetchGetFolderContent.json))
  337. }
  338. export const getAppList = user => dispatch => {
  339. return fetchWrapper({
  340. url: `${FETCH_CONFIG.apiUrl}/system/applications`,
  341. param: {
  342. headers: {
  343. ...FETCH_CONFIG.headers,
  344. 'Authorization': 'Basic ' + user.auth
  345. },
  346. method: 'GET'
  347. },
  348. actionName: APP_LIST,
  349. dispatch
  350. })
  351. }
  352. export const getContentTypeList = user => dispatch => {
  353. return fetchWrapper({
  354. url: `${FETCH_CONFIG.apiUrl}/system/content_types`,
  355. param: {
  356. headers: {
  357. ...FETCH_CONFIG.headers,
  358. 'Authorization': 'Basic ' + user.auth
  359. },
  360. method: 'GET'
  361. },
  362. actionName: CONTENT_TYPE_LIST,
  363. dispatch
  364. })
  365. }
  366. export const putWorkspaceContentArchived = (user, idWorkspace, idContent) => dispatch => {
  367. return fetchWrapper({
  368. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/archive`,
  369. param: {
  370. headers: {
  371. ...FETCH_CONFIG.headers,
  372. 'Authorization': 'Basic ' + user.auth
  373. },
  374. method: 'PUT'
  375. },
  376. actionName: WORKSPACE_CONTENT_ARCHIVED,
  377. dispatch
  378. })
  379. }
  380. export const putWorkspaceContentDeleted = (user, idWorkspace, idContent) => dispatch => {
  381. return fetchWrapper({
  382. url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}/delete`,
  383. param: {
  384. headers: {
  385. ...FETCH_CONFIG.headers,
  386. 'Authorization': 'Basic ' + user.auth
  387. },
  388. method: 'PUT'
  389. },
  390. actionName: WORKSPACE_CONTENT_DELETED,
  391. dispatch
  392. })
  393. }