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

feat: add time filter #353

Merged
Merged
Changes from 5 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
111 changes: 82 additions & 29 deletions src/components/DateFilter/index.tsx
Original file line number Diff line number Diff line change
@@ -22,6 +22,9 @@ import {
MonthPicker,
OutsideContainer,
OutsideContent,
TimeFilterContainer,
TimeInput,
TimeSelector,
Warning,
} from './styles';

@@ -82,18 +85,6 @@ const DateFilter: React.FC<PropsWithChildren> = () => {
delete updatedQuery.enddate;
setQueryAndRouter(updatedQuery, router);
};
const filterDate = (selectedDays: ISelectedDays) => {
setQueryAndRouter(
{
...router.query,
startdate: selectedDays.start.getTime().toString(),
enddate: selectedDays.end
? (selectedDays.end.getTime() + 24 * 60 * 60 * 1000).toString()
: (selectedDays.start.getTime() + 24 * 60 * 60 * 1000).toString(),
},
router,
);
};

const [date, setDate] = useState(currentDate);
const [calendarOpen, setCalendarOpen] = useState(false);
@@ -207,22 +198,6 @@ const DateFilter: React.FC<PropsWithChildren> = () => {
[selectedDays, firstSelection],
);

const handleConfirmClick = useCallback(() => {
filterDate(selectedDays);

setInputValue(
`${selectedDays?.start?.toLocaleString().split(',')[0]}${
selectedDays.end
? ' - ' + selectedDays?.end?.toLocaleString().split(',')[0]
: ''
}`,
);
setDate(selectedDays.start);
inputRef.current?.blur();
setCalendarOpen(false);
setButtonActive(false);
}, [selectedDays, filterDate]);

const handleClear = useCallback(
(e: React.MouseEvent) => {
e.stopPropagation();
@@ -265,6 +240,62 @@ const DateFilter: React.FC<PropsWithChildren> = () => {
'December',
];

const [startTime, setStartTime] = useState('00:00');
const [endTime, setEndTime] = useState('23:59');

const formatDateWithTime = (date: Date, time: string) => {
const [hours, minutes] = time.split(':').map(Number);
caioapolonio marked this conversation as resolved.
Show resolved Hide resolved
const newDate = new Date(date);
newDate.setHours(hours, minutes, 0, 0);

return newDate;
};
caioapolonio marked this conversation as resolved.
Show resolved Hide resolved

const filterDate = (
selectedDays: ISelectedDays,
startTime: string,
endTime: string,
) => {
const startDateWithTime = formatDateWithTime(selectedDays.start, startTime);
const endDateWithTime = selectedDays.end
? formatDateWithTime(selectedDays.end, endTime)
: formatDateWithTime(selectedDays.start, endTime);

setQueryAndRouter(
{
...router.query,
startdate: startDateWithTime.getTime().toString(),
enddate: endDateWithTime.getTime().toString(),
},
router,
);
};

const handleConfirmClick = useCallback(() => {
filterDate(selectedDays, startTime, endTime);
const startWithTime = formatDateWithTime(selectedDays.start, startTime);
const endWithTime = selectedDays.end
? formatDateWithTime(selectedDays.end, endTime)
: startWithTime;

setInputValue(
`${startWithTime.toLocaleString()} - ${
endWithTime ? endWithTime.toLocaleString() : ''
}`,
);
setDate(selectedDays.start);
inputRef.current?.blur();
setCalendarOpen(false);
}, [selectedDays, startTime, endTime, filterDate]);

const handleStartTimeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setStartTime(e.target.value);
};

const handleEndTimeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setEndTime(e.target.value);
};

return router.isReady ? (
<FilterContainer open={calendarOpen}>
<span>Date Filter</span>
@@ -293,7 +324,7 @@ const DateFilter: React.FC<PropsWithChildren> = () => {
</OutsideContainer>

{calendarOpen && (
<CalendarContainer onMouseDown={e => e.preventDefault()}>
<CalendarContainer>
<CalendarHeader>
<strong>
<h3>Date Filter</h3>
@@ -356,6 +387,28 @@ const DateFilter: React.FC<PropsWithChildren> = () => {
<span>Nothing on this date</span>
</Warning>
)}
<TimeFilterContainer>
<TimeSelector>
<label>Start Time:</label>
<TimeInput
style={{ WebkitAppearance: 'none' }}
type="time"
id="start-time"
value={startTime}
onChange={handleStartTimeChange}
/>
</TimeSelector>

<TimeSelector>
<label>End Time:</label>
<TimeInput
type="time"
id="end-time"
value={endTime}
onChange={handleEndTimeChange}
/>
</TimeSelector>
</TimeFilterContainer>
caioapolonio marked this conversation as resolved.
Show resolved Hide resolved
<Confirm onClick={handleConfirmClick} isActive={buttonActive}>
{' '}
Confirm
30 changes: 30 additions & 0 deletions src/components/DateFilter/styles.ts
Original file line number Diff line number Diff line change
@@ -126,6 +126,36 @@ export const Input = styled.input`
}
`;

export const TimeFilterContainer = styled.div`
display: flex;
flex-direction: column;
gap: 0.5rem;
padding: 1rem 1rem 0 1rem;
color: ${props => props.theme.black};
`;

export const TimeSelector = styled.div`
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
color: ${props => props.theme.black};
`;

export const TimeInput = styled.input`
color: ${props => props.theme.black};
justify-items: center;
padding: 0 0.1rem;
border-bottom: 1px solid ${props => props.theme.gray700};
${props => props.theme.dark && `color-scheme: dark;`}
/* border-radius: 0.25rem; */
width: fit-content;
min-width: 4rem;
&::-webkit-calendar-picker-indicator {
display: none;
}
`;

export const CalendarContainer = styled.div`
display: flex;
flex-direction: column;