From 4369be7b171f849a15febf924a6caeae789a6a72 Mon Sep 17 00:00:00 2001 From: Jared Scheib Date: Tue, 13 Jun 2017 16:25:12 -0700 Subject: [PATCH] Load external links from /chronograf/v1 into Redux --- ui/src/index.js | 5 ++++- ui/src/shared/actions/links.js | 6 ++++++ ui/src/shared/constants/actionTypes.js | 2 ++ ui/src/shared/reducers/index.js | 2 ++ ui/src/shared/reducers/links.js | 21 +++++++++++++++++++++ ui/src/utils/ajax.js | 19 +++++++++---------- 6 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 ui/src/shared/actions/links.js create mode 100644 ui/src/shared/reducers/links.js diff --git a/ui/src/index.js b/ui/src/index.js index 4afdd49520..7b656caffc 100644 --- a/ui/src/index.js +++ b/ui/src/index.js @@ -36,6 +36,7 @@ import { meReceived, logoutLinkReceived, } from 'shared/actions/auth' +import {linksReceived} from 'shared/actions/links' import {errorThrown} from 'shared/actions/errors' import 'src/style/chronograf.scss' @@ -87,11 +88,13 @@ const Root = React.createClass({ async startHeartbeat({shouldDispatchResponse}) { try { - const {data: me, auth, logoutLink} = await getMe() + // These non-me objects are added to every response by some AJAX trickery + const {data: me, auth, logoutLink, external} = await getMe() if (shouldDispatchResponse) { dispatch(authReceived(auth)) dispatch(meReceived(me)) dispatch(logoutLinkReceived(logoutLink)) + dispatch(linksReceived({external})) } setTimeout(() => { diff --git a/ui/src/shared/actions/links.js b/ui/src/shared/actions/links.js new file mode 100644 index 0000000000..3aa840a31b --- /dev/null +++ b/ui/src/shared/actions/links.js @@ -0,0 +1,6 @@ +import * as actionTypes from 'shared/constants/actionTypes' + +export const linksReceived = links => ({ + type: actionTypes.LINKS_RECEIVED, + payload: {links}, +}) diff --git a/ui/src/shared/constants/actionTypes.js b/ui/src/shared/constants/actionTypes.js index 8c8f62840e..763fc6841d 100644 --- a/ui/src/shared/constants/actionTypes.js +++ b/ui/src/shared/constants/actionTypes.js @@ -1 +1,3 @@ export const TEMPLATE_VARIABLE_SELECTED = 'TEMPLATE_VARIABLE_SELECTED' + +export const LINKS_RECEIVED = 'LINKS_RECEIVED' diff --git a/ui/src/shared/reducers/index.js b/ui/src/shared/reducers/index.js index c598cd0390..cfb032a7de 100644 --- a/ui/src/shared/reducers/index.js +++ b/ui/src/shared/reducers/index.js @@ -1,6 +1,7 @@ import app from './app' import auth from './auth' import errors from './errors' +import links from './links' import notifications from './notifications' import sources from './sources' @@ -8,6 +9,7 @@ export default { app, auth, errors, + links, notifications, sources, } diff --git a/ui/src/shared/reducers/links.js b/ui/src/shared/reducers/links.js new file mode 100644 index 0000000000..02c17352f8 --- /dev/null +++ b/ui/src/shared/reducers/links.js @@ -0,0 +1,21 @@ +import * as actionTypes from 'shared/constants/actionTypes' + +const initialState = { + external: {}, +} + +const linksReducer = (state = initialState, action) => { + switch (action.type) { + case actionTypes.LINKS_RECEIVED: { + const {links} = action.payload + + return links + } + + default: { + return state + } + } +} + +export default linksReducer diff --git a/ui/src/utils/ajax.js b/ui/src/utils/ajax.js index 9278c09fbd..409e6dc280 100644 --- a/ui/src/utils/ajax.js +++ b/ui/src/utils/ajax.js @@ -2,6 +2,13 @@ import axios from 'axios' let links +const generateResponseWithLinks = (response, {auth, logout, external}) => ({ + ...response, + auth: {links: auth}, + logoutLink: logout, + external, +}) + const AJAX = async ({ url, resource, @@ -39,19 +46,11 @@ const AJAX = async ({ headers, }) - const {auth} = links - - return { - ...response, - auth: {links: auth}, - logoutLink: links.logout, - } + return generateResponseWithLinks(response, links) } catch (error) { const {response} = error - const {auth} = links - - throw {...response, auth: {links: auth}, logoutLink: links.logout} // eslint-disable-line no-throw-literal + throw generateResponseWithLinks(response, links) // eslint-disable-line no-throw-literal } }