diff --git a/.gitignore b/.gitignore index 060587b..3836f3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ node_modules npm-debug.log - +.env .idea diff --git a/client/src/components/DateRangePicker/index.js b/client/src/components/DateRangePicker/index.js new file mode 100644 index 0000000..0e2cc1d --- /dev/null +++ b/client/src/components/DateRangePicker/index.js @@ -0,0 +1,142 @@ +import React, { Component } from 'react'; +import moment from 'moment'; + +import { Form, Message, Grid } from 'semantic-ui-react'; + +import { defaultDateFormat } from '../../constants/constants'; + +class DateRangePicker extends Component { + constructor(props) { + super(props); + + const initialStartDate = props.startDate ? moment(props.startDate, defaultDateFormat) : moment().startOf('month'); + let initialEndDate = props.endDate ? moment(props.endDate, defaultDateFormat) : moment(); + + if (initialStartDate > initialEndDate){ + initialEndDate = initialStartDate.add(1, 'day'); + } + + this.state = { + startDate: initialStartDate, + endDate: initialEndDate, + error: null, + startDateLabel: props.startDateLabel || 'Start date', + endDateLabel: props.endDateLabel || 'End date', + }; + } + + onStartDateChange(event) { + const { endDate, startDateLabel, endDateLabel } = this.state; + + const newStartDate = moment(event.target.value, defaultDateFormat); + if (newStartDate > endDate){ + this.setState({ + error: `${startDateLabel} cannot be after ${endDateLabel}` + }); + return; + } + this.setState({startDate: newStartDate, error: null}); + } + + onEndDateChange(event) { + const { startDate, startDateLabel, endDateLabel } = this.state; + + const newEndDate = moment(event.target.value, defaultDateFormat); + if (newEndDate < startDate){ + this.setState({ + error: `${startDateLabel} cannot be after ${endDateLabel}` + }); + return; + } + this.setState({endDate: newEndDate, error: null}); + } + + onDismiss() { + this.setState({error: null}); + } + + onButtonClick() { + const { onDatesUpdate } = this.props; + const { startDate, endDate } = this.state; + + if (onDatesUpdate){ + onDatesUpdate({ + startDate: startDate.format(defaultDateFormat), + endDate: endDate.format(defaultDateFormat), + }); + } + } + + componentDidMount() { + this.onButtonClick(); + } + + render() { + const { startDate, endDate, error, startDateLabel, endDateLabel } = this.state; + const { inlineButton } = this.props; + + const buttonLabel = this.props.buttonLabel || 'Save'; + + const startDateValue = startDate.format(defaultDateFormat); + const endDateValue = endDate.format(defaultDateFormat); + + const buttonRender = ( + + { inlineButton && } + {buttonLabel} + + ); + + return ( +
+ + + + + + + + + + + { + inlineButton && buttonRender + } + + { error && + + + + Wrong date +

{startDateLabel} has to be before {endDateLabel}

+
+
+
+ } + { + !inlineButton && + + {buttonRender} + + } +
+
+ ); + } +} + +export default DateRangePicker; + + diff --git a/client/src/scenes/IncidentsReport/index.js b/client/src/scenes/IncidentsReport/index.js index 27dc370..951e642 100644 --- a/client/src/scenes/IncidentsReport/index.js +++ b/client/src/scenes/IncidentsReport/index.js @@ -3,7 +3,7 @@ import { connect } from 'react-redux'; import { Container } from 'semantic-ui-react'; import MainMenu from '../../components/MainMenu'; -import MonthSelector from '../../components/MonthSelector'; +import DateRangePicker from '../../components/DateRangePicker'; import MemberIncidentsTables from '../../components/MemberIncidentsTables'; import GenerateFeesInORDButton from '../../components/GenerateFeesInORDButton'; @@ -37,7 +37,7 @@ class IncidentsReport extends Component {

Incidents Report


- +
- +