Skip to content

Commit

Permalink
Fixes propagating invalid dates as queryFilter change
Browse files Browse the repository at this point in the history
Previously strings such as “InvalidDate” were being propagated upwards with `onQueryFiltersChange`. This caused logic errors further up the component tree. Fixed by checking date validity prior to propagating.
  • Loading branch information
getdave committed Jan 25, 2019
1 parent a4f7395 commit bb4df7e
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions client/my-sites/media-library/external-media-header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,25 @@ class MediaLibraryExternalHeader extends React.Component {
} );
};

onDateChange = ( startDate, endDate ) => {
onDateChange = ( startDate = null, endDate = null ) => {
const requiredDateFormat = 'YYYY-MM-DD';
const filterStartDate = this.props.moment( startDate ).format( requiredDateFormat );
const filterEndDate = this.props.moment( endDate ).format( requiredDateFormat );
const dateRange = {};

// Parse both dates into Moment
const momentStartDate = this.props.moment( startDate );
const momentEndDate = this.props.moment( endDate );

// Only add date to range if it is valid - can be null
if ( momentStartDate && momentStartDate.isValid() ) {
dateRange.from = momentStartDate.format( requiredDateFormat );
}

if ( momentEndDate && momentEndDate.isValid() ) {
dateRange.to = momentEndDate.format( requiredDateFormat );
}

this.props.onQueryFiltersChange( {
dateRange: {
from: filterStartDate,
to: filterEndDate,
},
dateRange,
} );
};

Expand Down

0 comments on commit bb4df7e

Please sign in to comment.