Skip to content

Commit

Permalink
try getting rid of local date state
Browse files Browse the repository at this point in the history
  • Loading branch information
nichhk committed Jul 23, 2022
1 parent a1b9e61 commit 9e5245b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
16 changes: 0 additions & 16 deletions client/components/DateSelector/DateSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,21 @@ import DateRanges from './DateRanges';
const dateFormat = 'YYYY-MM-DD';

function DateSelector({
onRangeSelect,
range,
initialDates,
updateStartDate,
updateEndDate,
}) {
const [dates, setDates] = useState(initialDates);
const [expanded, setExpanded] = useState(false);
const classes = useStyles();

const handleOptionSelect = optionDates => {
const formattedStart = moment(optionDates[0]).format(dateFormat);
const formattedEnd = moment(optionDates[1]).format(dateFormat);
setDates(() => optionDates);
updateStartDate(formattedStart);
updateEndDate(formattedEnd);
setExpanded(false);
};

const handleDateSelect = selectedDays => {
setDates(() => selectedDays);
if (typeof onRangeSelect === 'function') onRangeSelect(selectedDays);
};

const closeOptionsOnDateToggle = useCallback(() => {
setExpanded(false);
}, []);
Expand All @@ -53,9 +44,7 @@ function DateSelector({
<div className={classes.selector}>
<DatePicker
range={range}
dates={dates}
onToggle={closeOptionsOnDateToggle}
onSelect={handleDateSelect}
/>
<div className={classes.separator} />
</div>
Expand All @@ -65,7 +54,6 @@ function DateSelector({
classes={{ option, selected }}
options={options}
onSelect={handleOptionSelect}
dates={dates}
/>
</SelectorBox.Collapse>
</SelectorBox>
Expand All @@ -82,14 +70,10 @@ export default connect(null, mapDispatchToProps)(DateSelector);

DateSelector.propTypes = {
range: PropTypes.bool,
onRangeSelect: PropTypes.func,
initialDates: PropTypes.arrayOf(Date),
updateStartDate: PropTypes.func.isRequired,
updateEndDate: PropTypes.func.isRequired,
};

DateSelector.defaultProps = {
range: false,
onRangeSelect: null,
initialDates: [],
};
34 changes: 14 additions & 20 deletions client/components/common/DatePicker/DatePicker.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {
useRef, useState, useEffect, useCallback,
} from 'react';
import { connect } from 'react-redux';
import ReactDayPicker from '@components/common/ReactDayPicker';
import PropTypes from 'prop-types';
import CalendarIcon from '@material-ui/icons/CalendarToday';
Expand Down Expand Up @@ -44,6 +45,7 @@ const useStyles = makeStyles(theme => ({
}));

const renderSelectedDays = (dates, classes, range) => {
console.log('dates are ', dates);
const [from, to] = dates;
const isFromSelected = Boolean(from);
const isBothSelected = Boolean(from && to);
Expand Down Expand Up @@ -79,9 +81,8 @@ const renderSelectedDays = (dates, classes, range) => {
};

const DatePicker = ({
dates, onSelect, open, onToggle, range,
open, onToggle, range, startDate, endDate,
}) => {
const [selectedDays, setSelectedDays] = useState(() => dates);
const [showCalendar, setShowCalendar] = useState(() => open);
const classes = useStyles();

Expand All @@ -93,10 +94,6 @@ const DatePicker = ({
setShowCalendar(false);
}, [open]);

useEffect(() => {
setSelectedDays(() => dates);
}, [dates]);

const getCoordinates = () => {
if (ref.current) {
const { left, top, height } = ref.current.getClientRects()[0];
Expand All @@ -113,15 +110,9 @@ const DatePicker = ({
setShowCalendar(prevState => !prevState);
if (onToggle) onToggle();
};

const handleDateChage = incomingDates => {
setSelectedDays(() => incomingDates);
if (onSelect) onSelect(incomingDates);
};

return (
<div ref={ref} className={classes.selector}>
<div>{renderSelectedDays(selectedDays, classes, range)}</div>
<div>{renderSelectedDays([startDate, endDate], classes, range)}</div>
<IconButton
className={classes.button}
aria-label="toggle calendar datepicker"
Expand All @@ -135,8 +126,6 @@ const DatePicker = ({
{showCalendar ? (
<ReactDayPicker
range={range}
initialDates={selectedDays}
onChange={handleDateChage}
/>
) : null}
</div>
Expand All @@ -145,19 +134,24 @@ const DatePicker = ({
};

DatePicker.propTypes = {
onSelect: PropTypes.func,
range: PropTypes.bool,
open: PropTypes.bool,
onToggle: PropTypes.func,
dates: PropTypes.arrayOf(Date),
startDate: PropTypes.string,
endDate: PropTypes.string,
};

DatePicker.defaultProps = {
open: false,
range: false,
onToggle: null,
onSelect: null,
dates: [],
startDate: null,
endDate: null,
};

export default DatePicker;
const mapStateToProps = state => ({
startDate: state.filters.startDate,
endDate: state.filters.endDate,
});

export default connect(mapStateToProps)(ReactDayPicker);
7 changes: 1 addition & 6 deletions client/components/common/ReactDayPicker/ReactDayPicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getInitialState = initialDates => {
const defaultState = { from: null, to: null };

function ReactDayPicker({
onChange, initialDates, range, updateStartDate,
initialDates, range, updateStartDate,
updateEndDate,
}) {
const [state, setState] = useState(getInitialState(initialDates));
Expand All @@ -39,7 +39,6 @@ function ReactDayPicker({

const resetDays = () => {
setState(defaultState);
onChange([]);
};

const setFromDay = day => {
Expand All @@ -49,7 +48,6 @@ function ReactDayPicker({
enteredTo: null,
}));
updateStartDate(moment(day).format(INTERNAL_DATE_SPEC));
onChange([day]);
};

const setFromToDay = day => {
Expand All @@ -59,7 +57,6 @@ function ReactDayPicker({
enteredTo: day,
}));
updateEndDate(moment(day).format(INTERNAL_DATE_SPEC));
onChange([state.from, day]);
};

const handleDayClick = day => {
Expand Down Expand Up @@ -117,15 +114,13 @@ function ReactDayPicker({

ReactDayPicker.propTypes = {
range: PropTypes.bool,
onChange: PropTypes.func,
initialDates: PropTypes.arrayOf(Date),
updateStartDate: PropTypes.func,
updateEndDate: PropTypes.func,
};

ReactDayPicker.defaultProps = {
range: false,
onChange: null,
initialDates: [],
updateStartDate: null,
updateEndDate: null,
Expand Down

0 comments on commit 9e5245b

Please sign in to comment.