From fa6b36c66a38d14acd1cc08906d12792c12cdaf8 Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Thu, 21 Feb 2019 23:07:07 -0600 Subject: [PATCH 1/9] fix(rest): add rest api for homepage chart --- data/config/default.json | 14 ++++ data/config/ndh.json | 19 ++++- src/Homepage/page.jsx | 4 +- src/Homepage/reducers.js | 74 ++++++++++++++++++- src/Index/page.jsx | 4 +- src/Index/reduxer.jsx | 5 +- src/Index/utils.js | 43 +++++++++++ src/Submission/MapDataModel.jsx | 4 +- src/Submission/MapDataModel.test.jsx | 6 +- src/components/charts/IndexBarChart/index.jsx | 2 - src/localconf.js | 3 + 11 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 src/Index/utils.js diff --git a/data/config/default.json b/data/config/default.json index fadb27b979..5552d3a051 100644 --- a/data/config/default.json +++ b/data/config/default.json @@ -71,6 +71,20 @@ "link": "/submission", "label": "Submit data" } + ], + "homepageChartNodes": [ + { + "node": "case", + "name": "Cases" + }, + { + "node": "experiment", + "name": "Experiments" + }, + { + "node": "aliquot", + "name": "Aliquots" + } ] }, "navigation": { diff --git a/data/config/ndh.json b/data/config/ndh.json index 04d6370116..3c5164b17b 100644 --- a/data/config/ndh.json +++ b/data/config/ndh.json @@ -121,7 +121,24 @@ "text": "The website combines government datasets from 3 divisions of NIAID to create clean, easy to navigate visualizations for data-driven discovery within Allergy and Infectious Diseases.", "contact": "If you have any questions about access or the registration process, please contact ", "email": "support@datacommons.io" - } + }, + "footerLogos": [ + { + "src": "/src/img/gen3.png", + "href": "https://ctds.uchicago.edu/gen3", + "alt": "Gen3 Data Commons" + }, + { + "src": "/src/img/createdby.png", + "href": "https://ctds.uchicago.edu/", + "alt": "Center for Translational Data Science at the University of Chicago" + }, + { + "src": "/src/img/sponsors/niaid.png", + "href": "https://niaid.bionimbus.org", + "alt": "NIAID Data Hub" + } + ] }, "featureFlags": { "explorer": true diff --git a/src/Homepage/page.jsx b/src/Homepage/page.jsx index b8c025e09a..23f820221e 100644 --- a/src/Homepage/page.jsx +++ b/src/Homepage/page.jsx @@ -1,12 +1,12 @@ import React from 'react'; import { ReduxProjectDashboard, ReduxTransaction } from './reduxer'; -import getProjectsList from '../Index/relayer'; +import getProjectNodeCounts from '../Index/utils'; import getTransactionList from './relayer'; class HomePage extends React.Component { constructor(props) { super(props); - getProjectsList(); + getProjectNodeCounts(); getTransactionList(); } diff --git a/src/Homepage/reducers.js b/src/Homepage/reducers.js index 39daa44b82..0a2fecbdff 100644 --- a/src/Homepage/reducers.js +++ b/src/Homepage/reducers.js @@ -1,3 +1,5 @@ +import { components } from '../params'; + const homepage = (state = {}, action) => { switch (action.type) { case 'RECEIVE_PROJECT_LIST': { @@ -15,7 +17,13 @@ const homepage = (state = {}, action) => { ); const lastestListUpdating = Date.now(); // const { error, ...state } = state; - return { ...state, projectsByName, summaryCounts, lastestListUpdating }; + return { + ...state, + projectsByName, + summaryCounts, + lastestListUpdating, + countNames: components.charts.indexChartNames + }; } case 'RECEIVE_PROJECT_DETAIL': { const projectsByName = Object.assign({}, state.projectsByName || {}); @@ -29,6 +37,70 @@ const homepage = (state = {}, action) => { case 'RECEIVE_RELAY_FAIL': { return { ...state, error: action.data }; } + case 'RECEIVE_PROJECT_NODE_DATASETS': { + const { projectNodeCounts, homepageChartNodes, fileNodes } = action; + const nodesForIndexChart = homepageChartNodes.map(item => item.node); + + // adding counts by node + let summaryCounts = nodesForIndexChart.reduce((acc, curNode, index) => { + Object.keys(projectNodeCounts).forEach((proj) => { + if (typeof projectNodeCounts[proj][curNode] !== 'undefined') { + acc[index] += projectNodeCounts[proj][curNode]; + } + }); + return acc; + }, nodesForIndexChart.map(() => 0)); + + // keep previous design: if less than 4 nodes, calculate all files number + if (nodesForIndexChart.length < 4) { + // add counts for all file type nodes, as the last count + const fileCount = fileNodes.reduce((acc, fileNode) => { + Object.keys(projectNodeCounts).forEach((proj) => { + if (typeof projectNodeCounts[proj][fileNode] !== 'undefined') { + acc += projectNodeCounts[proj][fileNode]; + } + }); + return acc; + }, 0); + summaryCounts.push(fileCount); + } + + // constructing projct counts for index bar chart + const projectsByName = {}; + Object.keys(projectNodeCounts).forEach((proj) => { + let code = proj; + const projCodeIndex = proj.indexOf('-'); + if (projCodeIndex !== -1) { + code = proj.substring(projCodeIndex + 1); + } + let counts = 0; + if (typeof projectNodeCounts[proj] !== 'undefined') { + counts = nodesForIndexChart.map(node => projectNodeCounts[proj][node]); + } + + if (nodesForIndexChart.length < 4) { + let fileCountsForProj = fileNodes.reduce((acc, fileNode) => { + if (typeof projectNodeCounts[proj][fileNode] !== 'undefined') { + acc += projectNodeCounts[proj][fileNode]; + } + return acc; + }, 0); + counts.push(fileCountsForProj); + } + + projectsByName[proj] = { + code, + counts, + name: proj, + }; + }); + + let countNames = homepageChartNodes.map(item => item.name); + if (countNames.length < 4) { + countNames.push('Files'); + } + return { ...state, projectsByName, summaryCounts, countNames }; + } default: return state; } diff --git a/src/Index/page.jsx b/src/Index/page.jsx index 67dbe07fe9..09580a49e0 100644 --- a/src/Index/page.jsx +++ b/src/Index/page.jsx @@ -3,13 +3,13 @@ import Introduction from '../components/Introduction'; import { ReduxIndexButtonBar, ReduxIndexBarChart } from './reduxer'; import dictIcons from '../img/icons'; import { components } from '../params'; -import getProjectsList from './relayer'; +import getProjectNodeCounts from './utils'; import './page.less'; class IndexPageComponent extends React.Component { constructor(props) { super(props); - getProjectsList(); + getProjectNodeCounts(); } render() { diff --git a/src/Index/reduxer.jsx b/src/Index/reduxer.jsx index f0f2b253fb..c11ca7f231 100644 --- a/src/Index/reduxer.jsx +++ b/src/Index/reduxer.jsx @@ -12,7 +12,10 @@ export const ReduxIndexBarChart = (() => { const projectList = Object.values( state.homepage.projectsByName, ).sort(sortCompare); - return { projectList, countNames: components.charts.indexChartNames }; + return { + projectList, + countNames: state.homepage.countNames, + }; } return {}; }; diff --git a/src/Index/utils.js b/src/Index/utils.js new file mode 100644 index 0000000000..2c05f9334e --- /dev/null +++ b/src/Index/utils.js @@ -0,0 +1,43 @@ +import { fetchWithCreds } from '../actions'; +import { homepageChartNodes, datasetUrl } from '../localconf'; +import getReduxStore from '../reduxStore'; +import getProjectsList from './relayer'; + +const updateRedux = async projectNodeCounts => getReduxStore().then( + (store) => { + store.dispatch({ + type: 'RECEIVE_PROJECT_NODE_DATASETS', + projectNodeCounts, + homepageChartNodes, + fileNodes: store.getState().submission.file_nodes, + }); + }, + (err) => { + console.error('WARNING: failed to load redux store', err); + return 'ERR'; + }, +); + +const getProjectNodeCounts = async () => { + if (typeof homepageChartNodes === 'undefined') { + getProjectsList(); + return; + } + + const store = await getReduxStore(); + const fileNodes = store.getState().submission.file_nodes; + const nodesForIndexChart = homepageChartNodes.map(item => item.node); + const nodesToRequest = _.union(fileNodes, nodesForIndexChart); + const url = `${datasetUrl}?nodes=${nodesToRequest.join(',')}`; + + fetchWithCreds({ + path: url, + }).then((res) => { + updateRedux(res.data); + }) + .catch((err) => { + console.log(err); + }); +}; + +export default getProjectNodeCounts; diff --git a/src/Submission/MapDataModel.jsx b/src/Submission/MapDataModel.jsx index acd4caf773..b91df9f744 100644 --- a/src/Submission/MapDataModel.jsx +++ b/src/Submission/MapDataModel.jsx @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'; import { fetchQuery } from 'relay-runtime'; import Button from '@gen3/ui-component/dist/components/Button'; import BackLink from '../components/BackLink'; -import getProjectsList from '../Index/relayer'; +import getProjectNodeCounts from '../Index/utils'; import CheckmarkIcon from '../img/icons/status_confirm.svg'; import InputWithIcon from '../components/InputWithIcon'; import { GQLHelper } from '../gqlHelper'; @@ -34,7 +34,7 @@ class MapDataModel extends React.Component { if (this.props.filesToMap.length === 0) { // redirect if no files this.props.history.push('/submission/files'); } - getProjectsList(); + getProjectNodeCounts(); } setRequiredProperties = () => { diff --git a/src/Submission/MapDataModel.test.jsx b/src/Submission/MapDataModel.test.jsx index a981cf3371..f30e5163b7 100644 --- a/src/Submission/MapDataModel.test.jsx +++ b/src/Submission/MapDataModel.test.jsx @@ -4,10 +4,10 @@ import { createMemoryHistory } from 'history'; import { StaticRouter } from 'react-router-dom'; import MapDataModel from './MapDataModel'; import * as testData from './__test__/data.json'; -import getProjectsList from '../Index/relayer'; +import getProjectNodeCounts from '../Index/utils'; -jest.mock('../Index/relayer'); -getProjectsList.mockImplementation(() => jest.fn()); +jest.mock('../Index/utils'); +getProjectNodeCounts.mockImplementation(() => jest.fn()); describe('MapDataModel', () => { const history = createMemoryHistory('/submission/map'); diff --git a/src/components/charts/IndexBarChart/index.jsx b/src/components/charts/IndexBarChart/index.jsx index bfde8f10ec..6ca1d1f42d 100644 --- a/src/components/charts/IndexBarChart/index.jsx +++ b/src/components/charts/IndexBarChart/index.jsx @@ -1,7 +1,6 @@ import { ResponsiveContainer, Legend, Tooltip, BarChart, Bar, XAxis, YAxis } from 'recharts'; import PropTypes from 'prop-types'; // see https://github.com/facebook/prop-types#prop-types import React from 'react'; -import { browserHistory } from 'react-router-dom'; import Spinner from '../../Spinner'; import TooltipCDIS from '../TooltipCDIS/.'; import Tick from '../Tick'; @@ -116,7 +115,6 @@ class IndexBarChart extends React.Component {
{ browserHistory.push(`/${e.activeLabel}`); window.location.reload(false); }} data={indexChart} margin={barChartStyle.margins} layout={barChartStyle.layout} diff --git a/src/localconf.js b/src/localconf.js index 032316722c..5ae462dcf1 100644 --- a/src/localconf.js +++ b/src/localconf.js @@ -52,6 +52,7 @@ function buildConfig(opts) { const graphqlSchemaUrl = `${hostname}data/schema.json`; const workspaceUrl = '/lw-workspace/'; const workspaceErrorUrl = '/no-workspace-access/'; + const datasetUrl = `${hostname}api/search/datasets`; const colorsForCharts = { categorical9Colors: [ @@ -111,6 +112,8 @@ function buildConfig(opts) { certs: components.certs, workspaceUrl, workspaceErrorUrl, + homepageChartNodes: components.index.homepageChartNodes, + datasetUrl, }; } From 04374037ac29da8710a864087c236863fff08a21 Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Thu, 21 Feb 2019 23:29:30 -0600 Subject: [PATCH 2/9] fix(access): add option for public index --- .eslintrc.js | 1 + data/config/default.json | 3 ++- src/Homepage/reducers.js | 36 +++++++++++++++++++----------------- src/Index/reduxer.jsx | 2 +- src/Index/utils.js | 7 ++++--- src/index.jsx | 10 ++++++++-- src/localconf.js | 10 ++++++++++ 7 files changed, 45 insertions(+), 24 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e60ba08171..8ab673b6d2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -59,6 +59,7 @@ module.exports = { "data/getTexts.js", "data/gqlSetup.js", "src/SessionMonitor/index.js", + "src/Index/utils.js", ], "rules": { "no-console": "off" // for logging errors diff --git a/data/config/default.json b/data/config/default.json index 5552d3a051..0a10059742 100644 --- a/data/config/default.json +++ b/data/config/default.json @@ -85,7 +85,8 @@ "node": "aliquot", "name": "Aliquots" } - ] + ], + "public": true }, "navigation": { "title": "Generic Data Commons", diff --git a/src/Homepage/reducers.js b/src/Homepage/reducers.js index 0a2fecbdff..2635dae22c 100644 --- a/src/Homepage/reducers.js +++ b/src/Homepage/reducers.js @@ -17,12 +17,12 @@ const homepage = (state = {}, action) => { ); const lastestListUpdating = Date.now(); // const { error, ...state } = state; - return { - ...state, - projectsByName, - summaryCounts, - lastestListUpdating, - countNames: components.charts.indexChartNames + return { + ...state, + projectsByName, + summaryCounts, + lastestListUpdating, + countNames: components.charts.indexChartNames, }; } case 'RECEIVE_PROJECT_DETAIL': { @@ -42,7 +42,7 @@ const homepage = (state = {}, action) => { const nodesForIndexChart = homepageChartNodes.map(item => item.node); // adding counts by node - let summaryCounts = nodesForIndexChart.reduce((acc, curNode, index) => { + const summaryCounts = nodesForIndexChart.reduce((acc, curNode, index) => { Object.keys(projectNodeCounts).forEach((proj) => { if (typeof projectNodeCounts[proj][curNode] !== 'undefined') { acc[index] += projectNodeCounts[proj][curNode]; @@ -52,20 +52,21 @@ const homepage = (state = {}, action) => { }, nodesForIndexChart.map(() => 0)); // keep previous design: if less than 4 nodes, calculate all files number - if (nodesForIndexChart.length < 4) { - // add counts for all file type nodes, as the last count + if (nodesForIndexChart.length < 4) { + // add counts for all file type nodes, as the last count const fileCount = fileNodes.reduce((acc, fileNode) => { + let newAcc = acc; Object.keys(projectNodeCounts).forEach((proj) => { if (typeof projectNodeCounts[proj][fileNode] !== 'undefined') { - acc += projectNodeCounts[proj][fileNode]; + newAcc += projectNodeCounts[proj][fileNode]; } }); - return acc; + return newAcc; }, 0); summaryCounts.push(fileCount); } - // constructing projct counts for index bar chart + // constructing projct counts for index bar chart const projectsByName = {}; Object.keys(projectNodeCounts).forEach((proj) => { let code = proj; @@ -79,13 +80,14 @@ const homepage = (state = {}, action) => { } if (nodesForIndexChart.length < 4) { - let fileCountsForProj = fileNodes.reduce((acc, fileNode) => { + const fileCountsForProj = fileNodes.reduce((acc, fileNode) => { + let newAcc = acc; if (typeof projectNodeCounts[proj][fileNode] !== 'undefined') { - acc += projectNodeCounts[proj][fileNode]; + newAcc += projectNodeCounts[proj][fileNode]; } - return acc; + return newAcc; }, 0); - counts.push(fileCountsForProj); + counts.push(fileCountsForProj); } projectsByName[proj] = { @@ -95,7 +97,7 @@ const homepage = (state = {}, action) => { }; }); - let countNames = homepageChartNodes.map(item => item.name); + const countNames = homepageChartNodes.map(item => item.name); if (countNames.length < 4) { countNames.push('Files'); } diff --git a/src/Index/reduxer.jsx b/src/Index/reduxer.jsx index c11ca7f231..0092fa111f 100644 --- a/src/Index/reduxer.jsx +++ b/src/Index/reduxer.jsx @@ -13,7 +13,7 @@ export const ReduxIndexBarChart = (() => { state.homepage.projectsByName, ).sort(sortCompare); return { - projectList, + projectList, countNames: state.homepage.countNames, }; } diff --git a/src/Index/utils.js b/src/Index/utils.js index 2c05f9334e..339dca7d06 100644 --- a/src/Index/utils.js +++ b/src/Index/utils.js @@ -1,3 +1,4 @@ +import _ from 'underscore'; import { fetchWithCreds } from '../actions'; import { homepageChartNodes, datasetUrl } from '../localconf'; import getReduxStore from '../reduxStore'; @@ -35,9 +36,9 @@ const getProjectNodeCounts = async () => { }).then((res) => { updateRedux(res.data); }) - .catch((err) => { - console.log(err); - }); + .catch((err) => { + console.log(err); + }); }; export default getProjectNodeCounts; diff --git a/src/index.jsx b/src/index.jsx index 965c90acfc..ce2c1bae85 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -32,7 +32,7 @@ import theme from './theme'; import getReduxStore from './reduxStore'; import { ReduxNavBar, ReduxTopBar, ReduxFooter } from './Layout/reduxer'; import ReduxQueryNode, { submitSearchForm } from './QueryNode/ReduxQueryNode'; -import { basename, dev, gaDebug, workspaceUrl, workspaceErrorUrl } from './localconf'; +import { basename, dev, gaDebug, workspaceUrl, workspaceErrorUrl, indexPublic } from './localconf'; import ReduxAnalysis from './Analysis/ReduxAnalysis.js'; import { gaTracking, components } from './params'; import GA, { RouteTracker } from './components/GoogleAnalytics'; @@ -99,7 +99,13 @@ async function init() { exact path='/' component={ - props => + props => ( + + ) } /> Date: Fri, 22 Feb 2019 17:12:27 -0600 Subject: [PATCH 3/9] fix(404): if get 404 from datasets endpoint, hit graphql --- package-lock.json | 10 +++++----- src/Index/utils.js | 7 ++++++- src/index.jsx | 4 ++-- src/localconf.js | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5629ae1c51..65830296a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -940,7 +940,7 @@ }, "json5": { "version": "1.0.1", - "resolved": "http://registry.npmjs.org/json5/-/json5-1.0.1.tgz", + "resolved": "https://registry.npmjs.org/json5/-/json5-1.0.1.tgz", "integrity": "sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==", "requires": { "minimist": "^1.2.0" @@ -1007,7 +1007,7 @@ }, "minimist": { "version": "1.2.0", - "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" }, "ms": { @@ -5366,7 +5366,7 @@ "dependencies": { "callsites": { "version": "2.0.0", - "resolved": "https://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", + "resolved": "http://registry.npmjs.org/callsites/-/callsites-2.0.0.tgz", "integrity": "sha1-BuuE8A7qQT2oav/vrL/7Ngk7PFA=" } } @@ -25098,7 +25098,7 @@ }, "onetime": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=", "dev": true }, @@ -30241,7 +30241,7 @@ }, "onetime": { "version": "1.1.0", - "resolved": "http://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=", "dev": true }, diff --git a/src/Index/utils.js b/src/Index/utils.js index 339dca7d06..17208093a7 100644 --- a/src/Index/utils.js +++ b/src/Index/utils.js @@ -34,7 +34,12 @@ const getProjectNodeCounts = async () => { fetchWithCreds({ path: url, }).then((res) => { - updateRedux(res.data); + if (res.status === 200) { + updateRedux(res.data); + } else if (res.status === 404) { + console.error(`REST endpoint ${datasetUrl} not enabled in Peregrine yet.`); + getProjectsList(); + } }) .catch((err) => { console.log(err); diff --git a/src/index.jsx b/src/index.jsx index ce2c1bae85..a43be0d9da 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -100,9 +100,9 @@ async function init() { path='/' component={ props => ( - ) diff --git a/src/localconf.js b/src/localconf.js index ca490ae13d..001d7df67b 100644 --- a/src/localconf.js +++ b/src/localconf.js @@ -55,7 +55,7 @@ function buildConfig(opts) { const datasetUrl = `${hostname}api/search/datasets`; // see index page without login - let indexPublic = typeof components.index.public === 'undefined' + let indexPublic = typeof components.index.public === 'undefined' ? false : components.index.public; // backward compatible: homepageChartNodes not set means using graphql query, // which will return 401 UNAUTHORIZED, thus not making public From f850fcda98e142b5963c43b7e9301dff61da8324 Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Tue, 26 Feb 2019 14:27:37 -0600 Subject: [PATCH 4/9] fix(login): check login status for public pages --- src/Login/ProtectedContent.jsx | 61 +++++++++++++++++----------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/Login/ProtectedContent.jsx b/src/Login/ProtectedContent.jsx index f0b2c448aa..28b09c5273 100644 --- a/src/Login/ProtectedContent.jsx +++ b/src/Login/ProtectedContent.jsx @@ -71,37 +71,36 @@ class ProtectedContent extends React.Component { * in the various ways we want it to be. */ componentDidMount() { - if (!this.props.public) { - getReduxStore().then( - store => - Promise.all( - [ - store.dispatch({ type: 'CLEAR_COUNTS' }), // clear some counters - store.dispatch({ type: 'CLEAR_QUERY_NODES' }), - ], - ).then( - () => this.checkLoginStatus(store, this.state) - .then(newState => this.checkQuizStatus(newState)) - .then(newState => this.checkApiToken(store, newState)), - ).then( - (newState) => { - const filterPromise = (newState.authenticated - && typeof this.props.filter === 'function') - ? this.props.filter() - : Promise.resolve('ok'); - // finally update the component state - const finish = () => { - const latestState = Object.assign({}, newState); - latestState.dataLoaded = true; - this.setState(latestState); - }; - return filterPromise.then( - finish, finish, - ); - }, - ), - ); - } else { + getReduxStore().then( + store => + Promise.all( + [ + store.dispatch({ type: 'CLEAR_COUNTS' }), // clear some counters + store.dispatch({ type: 'CLEAR_QUERY_NODES' }), + ], + ).then( + () => this.checkLoginStatus(store, this.state) + .then(newState => this.props.public || this.checkQuizStatus(newState)) + .then(newState => this.props.public || this.checkApiToken(store, newState)), + ).then( + (newState) => { + const filterPromise = (!this.props.public && newState.authenticated + && typeof this.props.filter === 'function') + ? this.props.filter() + : Promise.resolve('ok'); + // finally update the component state + const finish = () => { + const latestState = Object.assign({}, newState); + latestState.dataLoaded = true; + this.setState(latestState); + }; + return filterPromise.then( + finish, finish, + ); + }, + ), + ); + if (this.props.public) { getReduxStore().then( (store) => { const filterPromise = ( From 9893651273d03c0074a31d975a60deada0bd4ac8 Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Tue, 26 Feb 2019 15:21:08 -0600 Subject: [PATCH 5/9] fix(login): providers return after rendering login will cause err --- src/Login/Login.jsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Login/Login.jsx b/src/Login/Login.jsx index 396f4d55f9..0ae278415d 100644 --- a/src/Login/Login.jsx +++ b/src/Login/Login.jsx @@ -2,7 +2,7 @@ import React from 'react'; import querystring from 'querystring'; import PropTypes from 'prop-types'; // see https://github.com/facebook/prop-types#prop-types -import { basename } from '../localconf'; +import { basename, loginPath } from '../localconf'; import SlidingWindow from '../components/SlidingWindow'; import './Login.less'; @@ -12,12 +12,22 @@ class Login extends React.Component { static propTypes = { providers: PropTypes.arrayOf( PropTypes.objectOf(PropTypes.any), - ).isRequired, + ), location: PropTypes.object.isRequired, dictIcons: PropTypes.object.isRequired, data: PropTypes.object.isRequired, }; + static defaultProps = { + providers: [ + { + id: 'google', + name: 'Google OAuth', + url: `${loginPath}google/`, + }, + ], + }; + constructor(props) { super(props); this.state = getInitialState(window.innerHeight - 221); @@ -68,7 +78,7 @@ class Login extends React.Component {
{this.props.data.text}
{ - this.props.providers.map( + this.props.providers && this.props.providers.map( p => (
From b46c42d47331e71f5b6bbe9254c33b48120584c9 Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Wed, 27 Feb 2019 10:58:43 -0600 Subject: [PATCH 6/9] fix(prop): default prop already set --- src/Login/Login.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Login/Login.jsx b/src/Login/Login.jsx index 0ae278415d..02771f9c81 100644 --- a/src/Login/Login.jsx +++ b/src/Login/Login.jsx @@ -78,7 +78,7 @@ class Login extends React.Component {
{this.props.data.text}
{ - this.props.providers && this.props.providers.map( + this.props.providers.map( p => (
From 0bb7c120bcfc66d051757dfadc250b6fc9855fe2 Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Wed, 27 Feb 2019 11:08:52 -0600 Subject: [PATCH 7/9] fix(undefined): checking undefined --- src/Homepage/reducers.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Homepage/reducers.js b/src/Homepage/reducers.js index 2635dae22c..04ab4d3a06 100644 --- a/src/Homepage/reducers.js +++ b/src/Homepage/reducers.js @@ -44,7 +44,7 @@ const homepage = (state = {}, action) => { // adding counts by node const summaryCounts = nodesForIndexChart.reduce((acc, curNode, index) => { Object.keys(projectNodeCounts).forEach((proj) => { - if (typeof projectNodeCounts[proj][curNode] !== 'undefined') { + if (projectNodeCounts[proj][curNode]) { acc[index] += projectNodeCounts[proj][curNode]; } }); @@ -57,7 +57,7 @@ const homepage = (state = {}, action) => { const fileCount = fileNodes.reduce((acc, fileNode) => { let newAcc = acc; Object.keys(projectNodeCounts).forEach((proj) => { - if (typeof projectNodeCounts[proj][fileNode] !== 'undefined') { + if (projectNodeCounts[proj][fileNode]) { newAcc += projectNodeCounts[proj][fileNode]; } }); @@ -75,14 +75,14 @@ const homepage = (state = {}, action) => { code = proj.substring(projCodeIndex + 1); } let counts = 0; - if (typeof projectNodeCounts[proj] !== 'undefined') { + if (projectNodeCounts[proj]) { counts = nodesForIndexChart.map(node => projectNodeCounts[proj][node]); } if (nodesForIndexChart.length < 4) { const fileCountsForProj = fileNodes.reduce((acc, fileNode) => { let newAcc = acc; - if (typeof projectNodeCounts[proj][fileNode] !== 'undefined') { + if (projectNodeCounts[proj][fileNode]) { newAcc += projectNodeCounts[proj][fileNode]; } return newAcc; From c30f7f107dad76400901e05b5b5d10649914b475 Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Wed, 27 Feb 2019 20:26:29 -0600 Subject: [PATCH 8/9] fix(public): remove public option from config, redirect to login if 401 --- data/config/default.json | 3 +-- src/Index/page.jsx | 12 +++++++++++- src/Index/utils.js | 22 ++++++++++++++++++---- src/localconf.js | 6 ++---- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/data/config/default.json b/data/config/default.json index 0a10059742..5552d3a051 100644 --- a/data/config/default.json +++ b/data/config/default.json @@ -85,8 +85,7 @@ "node": "aliquot", "name": "Aliquots" } - ], - "public": true + ] }, "navigation": { "title": "Generic Data Commons", diff --git a/src/Index/page.jsx b/src/Index/page.jsx index 09580a49e0..7ac57481ab 100644 --- a/src/Index/page.jsx +++ b/src/Index/page.jsx @@ -1,4 +1,5 @@ import React from 'react'; +import PropTypes from 'prop-types'; import Introduction from '../components/Introduction'; import { ReduxIndexButtonBar, ReduxIndexBarChart } from './reduxer'; import dictIcons from '../img/icons'; @@ -9,7 +10,12 @@ import './page.less'; class IndexPageComponent extends React.Component { constructor(props) { super(props); - getProjectNodeCounts(); + getProjectNodeCounts((res) => { + // If Peregrine returns unauthorized, need to redirect to `/login` page + if (res.needLogin) { + props.history.push('/login'); + } + }); } render() { @@ -25,6 +31,10 @@ class IndexPageComponent extends React.Component { } } +IndexPageComponent.propTypes = { + history: PropTypes.object.isRequired, +}; + const IndexPage = IndexPageComponent; export default IndexPage; diff --git a/src/Index/utils.js b/src/Index/utils.js index 17208093a7..6d49e40804 100644 --- a/src/Index/utils.js +++ b/src/Index/utils.js @@ -19,9 +19,11 @@ const updateRedux = async projectNodeCounts => getReduxStore().then( }, ); -const getProjectNodeCounts = async () => { +const getProjectNodeCounts = async (callback) => { + const resultStatus = { needLogin: false }; if (typeof homepageChartNodes === 'undefined') { getProjectsList(); + callback(resultStatus); return; } @@ -34,11 +36,23 @@ const getProjectNodeCounts = async () => { fetchWithCreds({ path: url, }).then((res) => { - if (res.status === 200) { + switch (res.status) { + case 200: updateRedux(res.data); - } else if (res.status === 404) { + callback(resultStatus); + break; + case 404: + // Shouldn't happen, this means peregrine datasets endpoint not enabled console.error(`REST endpoint ${datasetUrl} not enabled in Peregrine yet.`); - getProjectsList(); + callback(resultStatus); + break; + case 401: + case 403: + resultStatus.needLogin = true; + callback(resultStatus); + break; + default: + break; } }) .catch((err) => { diff --git a/src/localconf.js b/src/localconf.js index 001d7df67b..53a99e0b2f 100644 --- a/src/localconf.js +++ b/src/localconf.js @@ -54,11 +54,9 @@ function buildConfig(opts) { const workspaceErrorUrl = '/no-workspace-access/'; const datasetUrl = `${hostname}api/search/datasets`; - // see index page without login - let indexPublic = typeof components.index.public === 'undefined' - ? false : components.index.public; // backward compatible: homepageChartNodes not set means using graphql query, - // which will return 401 UNAUTHORIZED, thus not making public + // which will return 401 UNAUTHORIZED if not logged in, thus not making public + let indexPublic = true; if (typeof components.index.homepageChartNodes === 'undefined') { indexPublic = false; } From 9efa45f796316d56ca671698ae540cd6de64c57e Mon Sep 17 00:00:00 2001 From: Qingya Shu Date: Thu, 28 Feb 2019 09:06:51 -0600 Subject: [PATCH 9/9] fix(constructor): move to componentDidMount --- src/Index/page.jsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Index/page.jsx b/src/Index/page.jsx index 7ac57481ab..61afc20b36 100644 --- a/src/Index/page.jsx +++ b/src/Index/page.jsx @@ -8,12 +8,11 @@ import getProjectNodeCounts from './utils'; import './page.less'; class IndexPageComponent extends React.Component { - constructor(props) { - super(props); + componentDidMount() { getProjectNodeCounts((res) => { // If Peregrine returns unauthorized, need to redirect to `/login` page if (res.needLogin) { - props.history.push('/login'); + this.props.history.push('/login'); } }); }