Skip to content

Commit

Permalink
fix acitivities date picker
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyNenashev committed Oct 4, 2023
1 parent 5580f1f commit 34ea458
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addDays, startOfDay } from 'date-fns';
import { addDays, endOfDay, startOfDay } from 'date-fns';
import type { Activity, RequiredField, SerializeDateToNumber } from 'redux/interfaces';
import type { ActivityApiGetActivityRequest } from 'generated-sources';
import { ActivityType } from 'generated-sources';
Expand Down Expand Up @@ -31,7 +31,7 @@ export interface ActivityItemProps {
}

const beginDate = startOfDay(addDays(new Date(), -5)).getTime();
const endDate = startOfDay(addDays(new Date(), 1)).getTime();
const endDate = endOfDay(addDays(new Date(), 1)).getTime();

export const defaultActivityQuery: ActivityQuery = {
beginDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { addDays, endOfDay, startOfDay } from 'date-fns';
import DatePicker, { type DateObject } from 'react-multi-date-picker';
import AppDateRangePickerFooter from 'components/shared/elements/AppDateRangePicker/AppDateRangePickerFooter/AppDateRangePickerFooter';
Expand All @@ -15,15 +15,15 @@ const AppDateRangePicker: React.FC<AppDateRangePickerProps> = ({
label,
setCurrentRange,
}) => {
const datePickerRef = React.useRef<any>();
const datePickerRef = useRef<any>();

const [isRangeCorrect, setIsRangeCorrect] = React.useState(true);
const [{ rangeStart, rangeEnd }, setRange] = React.useState({
const [isRangeCorrect, setIsRangeCorrect] = useState(true);
const [{ rangeStart, rangeEnd }, setRange] = useState({
rangeStart: defaultRange.beginDate,
rangeEnd: defaultRange.endDate,
});

React.useEffect(() => {
useEffect(() => {
setRange({ rangeStart: defaultRange.beginDate, rangeEnd: defaultRange.endDate });
}, [defaultRange]);

Expand All @@ -46,18 +46,18 @@ const AppDateRangePicker: React.FC<AppDateRangePickerProps> = ({
},
];

const handleSetRange = React.useCallback(([beginDate, endDate]: Date[]) => {
const handleSetRange = useCallback(([beginDate, endDate]: Date[]) => {
setIsRangeCorrect(true);
if (!endDate) setIsRangeCorrect(false);
setRange({ rangeStart: startOfDay(beginDate), rangeEnd: startOfDay(endDate) });
setRange({ rangeStart: startOfDay(beginDate), rangeEnd: endOfDay(endDate) });
}, []);

type DisableSelectedDateParams = {
date: DateObject;
selectedDate: DateObject | DateObject[];
};

const disableSelectedDate = React.useCallback(
const disableSelectedDate = useCallback(
({ date, selectedDate }: DisableSelectedDateParams) => {
const isArray = Array.isArray(selectedDate);
if (isArray && selectedDate[0].unix === date.unix) return { disabled: true };
Expand All @@ -67,13 +67,23 @@ const AppDateRangePicker: React.FC<AppDateRangePickerProps> = ({
[]
);

const handleClickDone = React.useCallback(() => {
const handleClickDone = useCallback(() => {
if (setCurrentRange) {
setCurrentRange(rangeStart, rangeEnd);
}
datePickerRef.current?.closeCalendar();
}, [setCurrentRange, datePickerRef, rangeStart, rangeEnd]);

const appDateRangePickerFooter = (
<AppDateRangePickerFooter
position='bottom'
onClickDoneBtn={handleClickDone}
ranges={ranges}
setRange={handleSetRange}
isRangeCorrect={isRangeCorrect}
/>
);

return (
<>
<S.DateRangePickerLabel>{label}</S.DateRangePickerLabel>
Expand All @@ -92,15 +102,7 @@ const AppDateRangePicker: React.FC<AppDateRangePickerProps> = ({
handleSetRange([begin?.toDate(), end?.toDate()]);
}}
value={[rangeStart, rangeEnd]}
plugins={[
<AppDateRangePickerFooter
position='bottom'
onClickDoneBtn={handleClickDone}
ranges={ranges}
setRange={handleSetRange}
isRangeCorrect={isRangeCorrect}
/>,
]}
plugins={[appDateRangePickerFooter]}
ref={datePickerRef}
/>
</>
Expand Down
4 changes: 2 additions & 2 deletions odd-platform-ui/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const stringFormatted = (
};

export const formatDate = (date: number, dateFormat: string) => format(date, dateFormat);
export const toDate = (dateToCast: number): Date => new Date(dateToCast);
export const toTimestamp = (dateToCast: Date): number => dateToCast.getTime();
export const toDate = (date: number): Date => new Date(date);
export const toTimestamp = (date: Date): number => date.getTime();

export const setActivityBackgroundColor = (
theme: Theme,
Expand Down

0 comments on commit 34ea458

Please sign in to comment.