123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- import { FETCH_CONFIG } from './helper.js'
- import {
- TIMEZONE,
- setTimezone,
- LANG,
- updateLangList,
- USER_LOGIN,
- USER_LOGOUT,
- USER_ROLE,
- USER_CONNECTED,
- setUserRole,
- WORKSPACE,
- WORKSPACE_LIST,
- FOLDER,
- setFolderData,
- APP_LIST,
- CONTENT_TYPE_LIST
- } from './action-creator.sync.js'
-
- /*
- * fetchWrapper(obj)
- *
- * Params:
- * An Object with the following attributes :
- * url - string - url of the end point to call
- * param - object - param to send with fetch call (eg. header)
- * param.method - string - REQUIRED - method of the http call
- * actionName - string - name of the action to dispatch with 'PENDING' and 'SUCCESS' respectively before and after the http request
- * dispatch - func - redux dispatcher function
- *
- * Returns:
- * An object Response generated by whatwg-fetch with a new property 'json' containing the data received or informations in case of failure
- *
- * This function create a http async request using whatwg-fetch while dispatching a PENDING and a SUCCESS redux action.
- * 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
- */
- const fetchWrapper = async ({url, param, actionName, dispatch, debug = false}) => {
- dispatch({type: `${param.method}/${actionName}/PENDING`})
-
- const fetchResult = await fetch(url, param)
- fetchResult.json = await (async () => {
- switch (fetchResult.status) {
- case 200:
- case 304:
- return fetchResult.json()
- case 204:
- case 400:
- case 404:
- case 409:
- case 500:
- case 501:
- case 502:
- case 503:
- case 504:
- return '' // @TODO : handle errors
- }
- })()
- if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)
-
- if ([200, 204, 304].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
- else if ([400, 404, 500].includes(fetchResult.status)) dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
-
- return fetchResult
- }
-
- export const getLangList = () => async dispatch => {
- const fetchGetLangList = await fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/lang`,
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'GET'
- },
- actionName: LANG,
- dispatch
- })
- if (fetchGetLangList.status === 200) dispatch(updateLangList(fetchGetLangList.json))
- }
-
- export const getTimezone = () => async dispatch => {
- const fetchGetTimezone = await fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/timezone`,
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'GET'
- },
- actionName: TIMEZONE,
- dispatch
- })
- if (fetchGetTimezone.status === 200) dispatch(setTimezone(fetchGetTimezone.json))
- }
-
- export const postUserLogin = (login, password, rememberMe) => async dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/sessions/login`, // FETCH_CONFIG.apiUrl
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'POST',
- body: JSON.stringify({
- email: login,
- password: password
- // remember_me: rememberMe
- })
- },
- actionName: USER_LOGIN,
- dispatch
- })
- }
-
- export const postUserLogout = () => async dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/sessions/logout`, // FETCH_CONFIG.apiUrl
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'POST'
- },
- actionName: USER_LOGOUT,
- dispatch
- })
- }
-
- export const getUserIsConnected = user => async dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/sessions/whoami`, // FETCH_CONFIG.apiUrl
- param: {
- headers: {
- ...FETCH_CONFIG.headers,
- 'Authorization': 'Basic ' + user.auth
- },
- method: 'GET'
- },
- actionName: USER_CONNECTED,
- dispatch
- })
- }
-
- export const getUserRole = user => async dispatch => {
- const fetchGetUserRole = await fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/roles`,
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'GET'
- },
- actionName: USER_ROLE,
- dispatch
- })
- if (fetchGetUserRole.status === 200) dispatch(setUserRole(fetchGetUserRole.json))
- }
-
- export const getWorkspaceList = user => dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/users/${user.user_id}/workspaces`,
- param: {
- headers: {
- ...FETCH_CONFIG.headers,
- 'Authorization': 'Basic ' + user.auth
- },
- method: 'GET'
- },
- actionName: WORKSPACE_LIST,
- dispatch
- })
- }
-
- export const getWorkspaceContentList = (user, idWorkspace, idParent) => dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents?parent_id=${idParent}`,
- param: {
- headers: {
- ...FETCH_CONFIG.headers,
- 'Authorization': 'Basic ' + user.auth
- },
- method: 'GET'
- },
- actionName: WORKSPACE,
- dispatch
- })
- }
-
- export const getWorkspaceContent = (idWorkspace, idContent) => dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/${idContent}`,
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'GET'
- },
- actionName: WORKSPACE,
- dispatch
- })
- }
-
- export const getFolderContent = (idWorkspace, idFolder) => async dispatch => {
- const fetchGetFolderContent = await fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/contents/?parent_id=${idFolder}`,
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'GET'
- },
- actionName: `${WORKSPACE}/${FOLDER}`,
- dispatch
- })
- if (fetchGetFolderContent.status === 200) dispatch(setFolderData(idFolder, fetchGetFolderContent.json))
- }
-
- export const getAppList = user => dispatch => {
- console.log(user)
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/system/applications`,
- param: {
- headers: {
- ...FETCH_CONFIG.headers,
- 'Authorization': 'Basic ' + user.auth
- },
- method: 'GET',
- 'Authorization': 'Basic ' + user.auth
- },
- actionName: APP_LIST,
- dispatch
- })
- }
-
- export const getContentTypeList = user => dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/system/content_types`,
- param: {
- headers: {
- ...FETCH_CONFIG.headers,
- 'Authorization': 'Basic ' + user.auth
- },
- method: 'GET'
- },
- actionName: CONTENT_TYPE_LIST,
- dispatch
- })
- }
|