New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Country selector component integrated with country performance graphs #17
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f49611e
[#11][s]: simplify back only displaying one graph.
zelima a96aca9
[setup][s]: removed unnessary dependency for synon. added enzymeT-to-…
zelima c724194
[test][m]: switched to snapshot testing. added test for countrySelect…
zelima e2e7507
[component][m]: countrySelect component added for dropdowns. using re…
zelima 6443431
[test][s]: snapshot updated -refs #15
zelima d1d94bb
[component][m]: Selects countries from dropdown. Uneditable for defau…
zelima 9abd08f
[setup][s]: react-redux added
zelima e8c53cd
[test][s]: modified to check if state is set according to props -refs…
zelima dacec0d
[redux][m]: introduce to Redux store. Mocked store moved into index.j…
zelima 4e7bea8
[graphs][m]: Handles adding/removing lines from grphs, using redux st…
zelima fc2aa28
[test][s]: changed naming convention for onchange mathod in component…
zelima 3808a10
[test][m]: added tests for checking if container state is updated aft…
zelima 45d2951
[tidy-up][m]: pretify variables for tests and components
zelima File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,126 +1,153 @@ | ||
import React, { Component } from 'react'; | ||
import PlotlyGraph from './Plot.js' | ||
import { connect } from 'react-redux' | ||
import PlotlyGraph from './Plot.js'; | ||
import Select from 'react-select'; | ||
import 'react-select/dist/react-select.css'; | ||
|
||
|
||
class CountryPerformanceOnRisk extends Component { | ||
export class CountryPerformanceOnRisk extends Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { | ||
data: [], | ||
graphOptions: {}, | ||
countries: [], | ||
matchedCountry: {}, | ||
graphsToShow: [], | ||
graphs: {} | ||
defaultCountry: {}, | ||
selected1: undefined, | ||
selected2: undefined, | ||
selected3: undefined | ||
} | ||
this.updateValue1 = this.updateValue1.bind(this) | ||
this.updateValue2 = this.updateValue2.bind(this) | ||
this.updateValue3 = this.updateValue3.bind(this) | ||
} | ||
|
||
|
||
computeState() { | ||
let xValues = ['2017-01-01','2017-01-08','2017-01-15']; | ||
let reduxStore = { | ||
entities: { | ||
graphs: { | ||
1: { | ||
title: 'DDOS-graph', | ||
dataToshow: ['t1','t2'], | ||
graphLayout: ['l1'] | ||
} | ||
}, | ||
data: { | ||
t1: { | ||
x: xValues, | ||
y: [2,4,6], | ||
name: 'example N1', | ||
type: 'scatter' | ||
}, | ||
t2: { | ||
x: xValues, | ||
y: [1,4,7], | ||
name: 'example N2', | ||
type: 'scatter' | ||
} | ||
}, | ||
layouts: { | ||
l1: { | ||
title : 'Global DDOS potential', | ||
height: 600, | ||
barmode: 'stack', | ||
xaxis: { | ||
title: '*This chart assumes an average 1 mbit/sec Internet connection for every IP address.', | ||
gridcolor: 'transparent', | ||
}, | ||
yaxis: { | ||
title: 'GBit/sec' | ||
} | ||
} | ||
}, | ||
countries: [ | ||
{id: 'uk', name: 'United Kingdom'}, | ||
{id: 'us', name: 'United States'} | ||
] | ||
}, | ||
graphsToShow: [1] | ||
} | ||
computeState(props=this.props) { | ||
|
||
let state = { | ||
graphsToShow: reduxStore.graphsToShow, | ||
graphs: reduxStore.entities.graphs, | ||
data : reduxStore.entities.data, | ||
graphOptions: reduxStore.entities.layouts, | ||
countries: reduxStore.entities.countries, | ||
matchedCountry: {} | ||
data: props.data, | ||
graphOptions: props.graphOptions, | ||
countries: props.countries, | ||
defaultCountry: props.defaultCountry, | ||
} | ||
return state | ||
} | ||
|
||
|
||
handleSearch(event) { | ||
let searchedCountry = this.state.countries.filter( country => { | ||
return country.name.toLowerCase().indexOf(event.target.value.toLowerCase()) !== -1; | ||
}) | ||
if(searchedCountry) { | ||
this.setState({ | ||
matchedCountry: searchedCountry | ||
}) | ||
componentDidMount() { | ||
this.setState(this.computeState(this.props)) | ||
}; | ||
|
||
componentWillReceiveProps(nextProps) { | ||
this.setState(this.computeState(nextProps)) | ||
} | ||
|
||
updateValue1(newCountry) { | ||
if (newCountry) { | ||
this.props.dispatch({type: "addRemoveLine", id: newCountry.value + '1', idx: 2}) | ||
} else { | ||
this.props.dispatch({type: "addRemoveLine", id: newCountry, idx: 2}) | ||
} | ||
this.setState({ | ||
selected1: newCountry | ||
|
||
}) | ||
} | ||
|
||
updateValue2(newCountry) { | ||
if (newCountry) { | ||
this.props.dispatch({type: "addRemoveLine", id: newCountry.value + '1', idx: 3}) | ||
} else { | ||
this.props.dispatch({type: "addRemoveLine", id: newCountry, idx: 3}) | ||
} | ||
this.setState({ | ||
selected2: newCountry | ||
}) | ||
} | ||
|
||
componentDidMount() { | ||
this.setState(this.computeState()) | ||
}; | ||
updateValue3(newCountry) { | ||
if (newCountry) { | ||
this.props.dispatch({type: "addRemoveLine", id: newCountry.value + '1', idx: 4}) | ||
} else { | ||
this.props.dispatch({type: "addRemoveLine", id: newCountry, idx: 4}) | ||
} | ||
this.setState({ | ||
selected3: newCountry | ||
}) | ||
} | ||
|
||
|
||
render() { | ||
let graphsToShow = this.state.graphsToShow | ||
let graphs = this.state.graphs | ||
let data = this.state.data | ||
let graphOptions = this.state.graphOptions | ||
return ( | ||
<div> | ||
{graphsToShow.map(id => { | ||
let dataForGraph = graphs[id].dataToshow.map(line => { | ||
return data[line] | ||
}) | ||
let layoutForGraph = graphs[id].graphLayout.map(layout => { | ||
return graphOptions[layout] | ||
}) | ||
return <div key={graphs[id].title}> | ||
< PlotlyGraph | ||
data={dataForGraph} | ||
graphOptions={layoutForGraph} | ||
graphID={graphs[id].title}/> | ||
|
||
< input type="text" | ||
placeholder="Search.." | ||
value={this.state.search} | ||
onChange={this.handleSearch.bind(this)} /> | ||
</div> | ||
})} | ||
<PlotlyGraph | ||
data={this.state.data} | ||
graphOptions={this.state.graphOptions} | ||
graphID='DDOS-graph' /> | ||
< CountrySelect | ||
selectOptions={[this.state.defaultCountry]} | ||
disabled={true} | ||
/> | ||
< CountrySelect | ||
selectOptions={[{value: 't', label: 'Total'}]} | ||
disabled={true} | ||
/> | ||
< CountrySelect | ||
selectOptions={this.state.countries} | ||
onChange={this.updateValue1} | ||
selectedCountries={this.state.selected1} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets change the name to |
||
/> | ||
< CountrySelect | ||
selectOptions={this.state.countries} | ||
onChange={this.updateValue2} | ||
selectedCountries={this.state.selected2} | ||
/> | ||
< CountrySelect | ||
selectOptions={this.state.countries} | ||
onChange={this.updateValue3} | ||
selectedCountries={this.state.selected3} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
|
||
export class CountrySelect extends Component { | ||
constructor(props) { | ||
super(props) | ||
} | ||
|
||
render() { | ||
let options = this.props.selectOptions | ||
let update = this.props.onChange | ||
let selectedCountries = this.props.selectedCountries | ||
let disabled = this.props.disabled | ||
const style = { width: "20%", display: "inline", float: "left" } | ||
return ( | ||
<div style={style}> | ||
<Select | ||
name="countries" | ||
value={selectedCountries || options[0]} | ||
options={options} | ||
onChange={update} | ||
disabled={disabled} | ||
/> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default CountryPerformanceOnRisk; | ||
const mapStateToProps = (state) => { | ||
return { | ||
data: state.entities.data.filter(data => { | ||
return state.graphs[1].dataToshow.indexOf(data.id) !== -1 | ||
}), | ||
graphOptions: state.entities.layouts, | ||
countries: state.entities.countries, | ||
defaultCountry: state.defaultCountry | ||
} | ||
} | ||
|
||
export default connect(mapStateToProps)(CountryPerformanceOnRisk) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,110 @@ | ||
/* global graphData */ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { createStore } from 'redux' | ||
import { Provider } from 'react-redux' | ||
|
||
import CountryPerformanceOnRisk from './components/CountryPerformanceOnRisk'; | ||
|
||
|
||
// graphData (List of urls to API endpoints) should be passed from server | ||
let xValues = ['2017-01-01','2017-01-08','2017-01-15']; | ||
let reduxStore = { | ||
graphs: { | ||
1: { | ||
title: 'dns-graph', | ||
dataToshow: ['uk1','t1'], | ||
graphLayout: ['l1'] | ||
} | ||
}, | ||
entities: { | ||
data: [ | ||
{ | ||
id: 'uk1', | ||
x: xValues, | ||
y: [2,4,6], | ||
name: 'United Kingdom', | ||
type: 'scatter' | ||
}, | ||
{ | ||
id: 't1', | ||
x: xValues, | ||
y: [1,4,7], | ||
name: 'Global', | ||
type: 'scatter' | ||
}, | ||
{ | ||
id: 'us1', | ||
x: xValues, | ||
y: [6,3,2], | ||
name: 'United States', | ||
type: 'scatter' | ||
}, | ||
{ | ||
id: 'ge1', | ||
x: xValues, | ||
y: [5,12,1], | ||
name: 'Georgia', | ||
type: 'scatter' | ||
}, | ||
{ | ||
id: 'kz1', | ||
x: xValues, | ||
y: [0,12,10], | ||
name: 'Kazakstan', | ||
type: 'scatter' | ||
} | ||
], | ||
layouts: { | ||
l1: { | ||
title : 'Open DNS', | ||
height: 600, | ||
xaxis: { | ||
gridcolor: 'transparent', | ||
}, | ||
yaxis: { | ||
title: 'GBit/sec' | ||
} | ||
} | ||
}, | ||
countries: [ | ||
{value: '', label: 'Select a country'}, | ||
{value: 'uk', label: 'United Kingdom' }, | ||
{value: 'us', label: 'United States' }, | ||
{value: 'ge', label: 'Georgia' }, | ||
{value: 'kz', label: 'Kazakhstan' } | ||
] | ||
}, | ||
defaultCountry: {value: 'uk', label: 'United Kingdom' } | ||
} | ||
const reducer = function(state, action) { | ||
// makes new copy of list, for not to mutate previous state | ||
let newDataToShow = state.graphs[1].dataToshow | ||
newDataToShow[action.idx] = action.id | ||
|
||
switch(action.type) { | ||
case 'addRemoveLine': | ||
return {...state, | ||
graphs: { | ||
1: { | ||
title: 'dns-graph', | ||
dataToshow: Object.assign( | ||
[], | ||
state.graphs[1].dataToshow, | ||
newDataToShow | ||
), | ||
graphLayout: ['l1'] | ||
} | ||
} | ||
} | ||
default: | ||
return state | ||
} | ||
} | ||
let store = createStore(reducer, reduxStore) | ||
|
||
ReactDOM.render( | ||
<CountryPerformanceOnRisk urls={graphData || []}/>, | ||
<Provider store={store}> | ||
<CountryPerformanceOnRisk/> | ||
</Provider>, | ||
document.getElementById('root') | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I Really doubt having separate function for each selector handling dropdown changes is the right approach. Especially when there will be multiple graphs.
We need to explore more how this may be done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zelima when there will be multiple graphs they will be instantiated from different containers in parallel, so they are independent of each other.
To be able to have a single function we need to pass some indicator from onChange event - which must be passed from the container as our CountrySelect component is generic. I think in the end we will have more not DRY code than we have now.