123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- import { FETCH_CONFIG } from './helper.js'
- import {
- TIMEZONE,
- setTimezone,
- LANG,
- updateLangList,
- USER_LOGIN,
- USER_LOGOUT,
- USER_ROLE,
- USER_CONNECTED,
- setUserRole,
- WORKSPACE,
- WORKSPACE_LIST,
- WORKSPACE_DETAIL,
- WORKSPACE_MEMBER_LIST,
- FOLDER,
- setFolderData,
- APP_LIST,
- CONTENT_TYPE_LIST
- } from './action-creator.sync.js'
-
-
- 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 ''
- }
- })()
- if (debug) console.log(`fetch ${param.method}/${actionName} result: `, fetchResult)
-
-
-
- switch (fetchResult.status) {
- case 200:
- case 204:
- case 304:
- dispatch({type: `${param.method}/${actionName}/SUCCESS`, data: fetchResult.json})
- break
- case 400:
- case 404:
- case 500:
- dispatch({type: `${param.method}/${actionName}/FAILED`, data: fetchResult.json})
- break
- }
-
- 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`,
- param: {
- headers: {...FETCH_CONFIG.headers},
- method: 'POST',
- body: JSON.stringify({
- email: login,
- password: password
-
- })
- },
- actionName: USER_LOGIN,
- dispatch
- })
- }
-
- export const postUserLogout = () => async dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/sessions/logout`,
- 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`,
- 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 getWorkspaceDetail = (user, idWorkspace) => dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}`,
- param: {
- headers: {
- ...FETCH_CONFIG.headers,
- 'Authorization': 'Basic ' + user.auth
- },
- method: 'GET'
- },
- actionName: WORKSPACE_DETAIL,
- dispatch
- })
- }
-
- export const getWorkspaceMemberList = (user, idWorkspace) => dispatch => {
- return fetchWrapper({
- url: `${FETCH_CONFIG.apiUrl}/workspaces/${idWorkspace}/members`,
- param: {
- headers: {
- ...FETCH_CONFIG.headers,
- 'Authorization': 'Basic ' + user.auth
- },
- method: 'GET'
- },
- actionName: WORKSPACE_MEMBER_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 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
- })
- }
|