Skip to content

Commit

Permalink
(RF) Validate Date from URL param
Browse files Browse the repository at this point in the history
  • Loading branch information
elstr committed Feb 20, 2018
1 parent a682686 commit 3f5d97f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
16 changes: 6 additions & 10 deletions src/Components/Chart/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {Axes, Bars, PlayPause} from '../index'
import ResponsiveWrapper from '../ResponsiveWrapper/ResponsiveWrapper'
import pigData from '../../wild-pig-data.json'
import {scaleBand, scaleLinear} from 'd3-scale'
import {getMinYear, getParamValue, getMaxYear, getYearsFromData, roundNumberWithTwoDecimals} from '../helper'
import {
getMinYear, getParamValue, getMaxYear, getYearsFromData, roundNumberWithTwoDecimals,
validateYear
} from '../helper'
import {Line} from 'rc-progress'
import {colors} from '../../config/constants'
import * as qs from 'query-string'
Expand Down Expand Up @@ -36,15 +39,11 @@ class Chart extends Component {
const {minYear} = this.state
const {paused, year} = qs.parse(this.props.location.search)

/* First check if year has been passed and is an integer */
const yearParamValue = parseInt(year && getParamValue(year))

/* If it's a valid year value and more or eq to the minimum year, assign it, otherwise assign minimum year from data */
/* This is to validate year=2 for example */
const yearFrom = yearParamValue && (yearParamValue >= minYear) ? yearParamValue : minYear
const yearFrom = validateYear(year, minYear)

/* Paused is a string. If paused is true, set Boolean value true, else set Boolean value false */
const isPaused = paused ? getParamValue(paused) === 'true' : false

this.setState({yearFrom, isPaused})
}

Expand Down Expand Up @@ -92,14 +91,11 @@ class Chart extends Component {
// scaleBand type
const xScale = this.xScale
.padding(0.5)
// scaleBand domain should be an array of specific values
// in our case, we want to use movie titles
.domain(data.map(d => d.island))
.range([margins.left, svgDimensions.width - margins.right])

// scaleLinear type
const yScale = this.yScale
// scaleLinear domain required at least two values, min and max
.domain([0, maxValue])
.range([svgDimensions.height - margins.bottom, margins.top])

Expand Down
9 changes: 6 additions & 3 deletions src/Components/helper.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export const getMinYear = data => Math.min(...data.map(d => d.year))
export const getMaxYear = data => Math.max(...data.map(d => d.year))
export const getParamValue = param => param.substring(param.indexOf("=") + 1).toString()
export const validateYear = year => {
export const validateYear = (year, minValidYear) => {
/* First check if year has been passed and is an integer */
const yearParamValue = parseInt(year && getParamValue(year))
/* If it's a valid year value, return it, otherwise get the minimum year from data */
return yearParamValue ? yearParamValue : getMinYear(this.data)

/* If it's a valid year value and more or eq to the minimum valid year, return it
* otherwise return minimum valid year
* Validates year=2 when min year is 2000 for example */
return yearParamValue && (yearParamValue >= minValidYear) ? yearParamValue : minValidYear
}
export const getYearsFromData = data => {
return data.reduce((acc, val) => {
Expand Down

0 comments on commit 3f5d97f

Please sign in to comment.