Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: two-month-picker to tsx #15

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SideList from './side-list.jsx';
import CalendarIcon from './calendar-icon';

const TwoMonthPicker = Loadable({
loader: () => import(/* webpackChunkName: "two-month-picker" */ './two-month-picker.jsx'),
loader: () => import(/* webpackChunkName: "two-month-picker" */ './two-month-picker'),
loading: () => null,
render(loaded, props) {
const Component = loaded.default;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,79 @@
import PropTypes from 'prop-types';
import moment from 'moment';
import React from 'react';
import { Calendar } from '@deriv/components';
import { addMonths, diffInMonths, epochToMoment, subMonths, toMoment } from '@deriv/shared';

const TwoMonthPicker = React.memo(({ onChange, isPeriodDisabled, value }) => {
const [left_pane_date, setLeftPaneDate] = React.useState(subMonths(value, 1).unix());
type TTwoMonthPicker = {
onChange: (date: moment.MomentInput) => void;
isPeriodDisabled: (date: moment.MomentInput) => boolean;
value: number;
};

const TwoMonthPicker = React.memo(({ onChange, isPeriodDisabled, value }: TTwoMonthPicker) => {
const [left_pane_date, setLeftPaneDate] = React.useState(
subMonths(value ? new Date(value * 1000).toISOString() : '', 1).unix()
);
const [right_pane_date, setRightPaneDate] = React.useState(value);

const navigateFrom = e => {
setLeftPaneDate(e.unix());
setRightPaneDate(addMonths(e, 1).unix());
/**
* Navigate from date
*
* @param {moment.Moment} date
*/
const navigateFrom = (date: moment.Moment) => {
setLeftPaneDate(date.unix());
setRightPaneDate(addMonths(date.toISOString(), 1).unix());
};

/**
* Only allow previous months to be available to navigate. Disable other periods
* Navigate to date
*
* @param date
* @param range
* @returns {boolean}
* @param {moment.Moment} date
*/
const validateFromArrows = (date, range) => {
return range === 'year' || diffInMonths(epochToMoment(left_pane_date), date) !== -1;
const navigateTo = (date: moment.Moment) => {
setLeftPaneDate(subMonths(date.toISOString(), 1).unix());
setRightPaneDate(toMoment(date).unix());
};

/**
* Validate values to be date_from < date_to
* Only allow previous months to be available to navigate. Disable other periods
*
* @param {moment.Moment} date
* @param {Extract<moment.DurationInputArg2, 'month'>} range
*/
const shouldDisableDate = date => {
return isPeriodDisabled(date.unix());
const validateFromArrows = (date: moment.Moment, range: Extract<moment.DurationInputArg2, 'month'>) => {
return diffInMonths(epochToMoment(left_pane_date), date) !== -1;
};

/**
* Only allow next month to be available to navigate (unless next month is in the future).
* Disable other periods
*
* @param date
* @param range
* @returns {boolean}
* @param {moment.Moment} date
* @param {Extract<moment.DurationInputArg2, 'month'>} range
*/
const validateToArrows = (date, range) => {
if (range === 'year') return true; // disallow year arrows
const validateToArrows = (date: moment.Moment, range: Extract<moment.DurationInputArg2, 'month'>) => {
const r_date = epochToMoment(right_pane_date).startOf('month');
if (diffInMonths(toMoment().startOf('month'), r_date) === 0) return true; // future months are disallowed
return diffInMonths(r_date, date) !== 1;
};

/**
* Validate values to be date_from < date_to
*
* @param {moment.Moment} date
*/
const shouldDisableDate = (date: moment.Moment) => {
return isPeriodDisabled(date.unix());
};

const jumpToCurrentMonth = () => {
const current_month = toMoment().endOf('month').unix();
setLeftPaneDate(epochToMoment(current_month).endOf('month').subtract(1, 'month').unix());
setRightPaneDate(current_month);
};

const navigateTo = e => {
setLeftPaneDate(subMonths(e, 1).unix());
setRightPaneDate(toMoment(e).unix());
};

const updateSelectedDate = e => {
const updateSelectedDate = (e: React.MouseEvent<HTMLElement>) => {
onChange(moment.utc(e.currentTarget.dataset.date, 'YYYY-MM-DD').unix());
};

Expand Down Expand Up @@ -108,9 +124,4 @@ const TwoMonthPicker = React.memo(({ onChange, isPeriodDisabled, value }) => {

TwoMonthPicker.displayName = 'TwoMonthPicker';

TwoMonthPicker.propTypes = {
isPeriodDisabled: PropTypes.func,
onChange: PropTypes.func,
value: PropTypes.number,
};
export default TwoMonthPicker;