Used setState callback to ensure no race conditions occur

This commit is contained in:
Alejandro Celaya 2018-08-01 18:32:21 +02:00
parent 0b15fba640
commit eef1946243

View file

@ -22,11 +22,11 @@ const MutedMessage = ({ children }) =>
export class ShortUrlsVisits extends React.Component { export class ShortUrlsVisits extends React.Component {
state = { startDate: undefined, endDate: undefined }; state = { startDate: undefined, endDate: undefined };
loadVisits = (dates = {}) => { loadVisits = () => {
const { match: { params } } = this.props; const { match: { params } } = this.props;
this.props.getShortUrlVisits(params.shortCode, mapObjIndexed( this.props.getShortUrlVisits(params.shortCode, mapObjIndexed(
value => value && value.format ? value.format('YYYY-MM-DD') : value, value => value && value.format ? value.format('YYYY-MM-DD') : value,
{ ...this.state, ...dates } this.state
)) ))
}; };
@ -150,19 +150,13 @@ export class ShortUrlsVisits extends React.Component {
<DateInput <DateInput
selected={this.state.startDate} selected={this.state.startDate}
placeholderText="Since" placeholderText="Since"
onChange={date => { onChange={date => this.setState({ startDate: date }, () => this.loadVisits())}
this.setState({ startDate: date });
this.loadVisits({ startDate: date });
}}
className="short-url-visits__date-input" className="short-url-visits__date-input"
/> />
<DateInput <DateInput
selected={this.state.endDate} selected={this.state.endDate}
placeholderText="Until" placeholderText="Until"
onChange={date => { onChange={date => this.setState({ endDate: date }, () => this.loadVisits())}
this.setState({ endDate: date });
this.loadVisits({ endDate: date });
}}
className="short-url-visits__date-input" className="short-url-visits__date-input"
/> />
</form> </form>