action-creator.async.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { FETCH_CONFIG } from './helper.js'
  2. import {
  3. LANG,
  4. updateLangList,
  5. USER_LOGIN,
  6. USER_DATA,
  7. USER_CONNECTED,
  8. updateUserConnected,
  9. updateUserData,
  10. WORKSPACE,
  11. updateWorkspaceData,
  12. WORKSPACE_LIST,
  13. updateWorkspaceListData,
  14. APP_LIST,
  15. setAppList
  16. } from './action-creator.sync.js'
  17. /*
  18. * fetchWrapper(obj)
  19. *
  20. * Params:
  21. * An Object with the following attributes :
  22. * url - string - url of the end point to call
  23. * param - object - param to send with fetch call (eg. header)
  24. * param.method - string - REQUIRED - method of the http call
  25. * actionName - string - name of the action to dispatch with 'PENDING' and 'SUCCESS' respectively before and after the http request
  26. * dispatch - func - redux dispatcher function
  27. *
  28. * Returns:
  29. * An object Response generated by whatwg-fetch with a new property 'json' containing the data received or informations in case of failure
  30. *
  31. * This function create a http async request using whatwg-fetch while dispatching a PENDING and a SUCCESS redux action.
  32. * 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
  33. */
  34. const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) => {
  35. dispatch({type: `${param.method}/${actionName}/PENDING`})
  36. const fetchResult = await fetch(url, param)
  37. fetchResult.json = await (async () => {
  38. switch (fetchResult.status) {
  39. case 200:
  40. case 304:
  41. return fetchResult.json()
  42. case 204:
  43. case 400:
  44. case 404:
  45. case 409:
  46. case 500:
  47. case 501:
  48. case 502:
  49. case 503:
  50. case 504:
  51. return '' // @TODO : handle errors
  52. }
  53. })()
  54. if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)
  55. if ([200, 204, 304].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
  56. else if ([400, 404, 500].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
  57. return fetchResult
  58. }
  59. export const getLangList = () => async dispatch => {
  60. const fetchGetLangList = await fetchWrapper({
  61. url: `${FETCH_CONFIG.apiUrl}/lang`,
  62. param: {...FETCH_CONFIG.header, method: 'GET'},
  63. actionName: LANG,
  64. dispatch
  65. })
  66. if (fetchGetLangList.status === 200) dispatch(updateLangList(fetchGetLangList.json))
  67. }
  68. export const userLogin = (login, password, rememberMe) => async dispatch => {
  69. const fetchUserLogin = await fetchWrapper({
  70. url: `${FETCH_CONFIG.apiUrl}/user/login`,
  71. param: {
  72. ...FETCH_CONFIG.header,
  73. method: 'POST',
  74. body: JSON.stringify({
  75. login,
  76. password,
  77. remember_me: rememberMe
  78. })
  79. },
  80. actionName: USER_LOGIN,
  81. dispatch
  82. })
  83. if (fetchUserLogin.status === 200) dispatch(updateUserConnected(fetchUserLogin.json))
  84. }
  85. export const getIsUserConnected = () => async dispatch => {
  86. const fetchUserLogged = await fetchWrapper({
  87. url: `${FETCH_CONFIG.apiUrl}/user/is_logged_in`,
  88. param: {...FETCH_CONFIG.header, method: 'GET'},
  89. actionName: USER_CONNECTED,
  90. dispatch
  91. })
  92. if (fetchUserLogged.status === 200) dispatch(updateUserConnected(fetchUserLogged.json))
  93. }
  94. export const updateUserLang = newLang => async dispatch => { // unused
  95. const fetchUpdateUserLang = await fetchWrapper({
  96. url: `${FETCH_CONFIG.apiUrl}/user`,
  97. param: {...FETCH_CONFIG.header, method: 'PATCH', body: JSON.stringify({lang: newLang})},
  98. actionName: USER_DATA,
  99. dispatch
  100. })
  101. if (fetchUpdateUserLang.status === 200) dispatch(updateUserData({lang: fetchUpdateUserLang.json.lang}))
  102. }
  103. // export const testResponseNoData = () => async dispatch => {
  104. // const fetchResponseNoData = await fetchWrapper({
  105. // url: 'http://localhost:3001/deletenodata',
  106. // param: {...FETCH_CONFIG.header, method: 'DELETE'},
  107. // actionName: 'TestNoData',
  108. // dispatch
  109. // })
  110. // console.log('jsonResponseNoData', fetchResponseNoData)
  111. // }
  112. export const getWorkspaceList = userId => async dispatch => {
  113. const fetchGetWorkspaceList = await fetchWrapper({
  114. url: `${FETCH_CONFIG.apiUrl}/user/${userId}/workspace`,
  115. param: {...FETCH_CONFIG.header, method: 'GET'},
  116. actionName: WORKSPACE_LIST,
  117. dispatch
  118. })
  119. if (fetchGetWorkspaceList.status === 200) dispatch(updateWorkspaceListData(fetchGetWorkspaceList.json))
  120. }
  121. export const getWorkspaceContent = workspaceId => async dispatch => {
  122. const fetchGetWorkspaceContent = await fetchWrapper({
  123. url: `${FETCH_CONFIG.apiUrl}/workspace/${workspaceId}`,
  124. param: {...FETCH_CONFIG.header, method: 'GET'},
  125. actionName: WORKSPACE,
  126. dispatch
  127. })
  128. if (fetchGetWorkspaceContent.status === 200) dispatch(updateWorkspaceData(fetchGetWorkspaceContent.json))
  129. }
  130. export const getAppList = () => async dispatch => {
  131. const fetchGetAppList = await fetchWrapper({
  132. url: `${FETCH_CONFIG.apiUrl}/app/config`,
  133. param: {...FETCH_CONFIG.header, method: 'GET'},
  134. actionName: APP_LIST,
  135. dispatch
  136. })
  137. if (fetchGetAppList.status === 200) dispatch(setAppList(fetchGetAppList.json))
  138. }