memorization

This commit is contained in:
Bernt Christian Egeland 2023-01-22 09:10:47 +01:00
parent 98996f03eb
commit 92564024b7

View File

@ -1,5 +1,5 @@
import { h } from 'preact'; import { h } from 'preact';
import { useEffect, useState } from 'preact/hooks'; import { useCallback, useEffect, useMemo, useState } from 'preact/hooks';
import { ArrowDropdown } from '../icons/ArrowDropdown'; import { ArrowDropdown } from '../icons/ArrowDropdown';
import { ArrowDropup } from '../icons/ArrowDropup'; import { ArrowDropup } from '../icons/ArrowDropup';
@ -16,24 +16,33 @@ const TimePicker = ({ dateRange, onChange }) => {
* If they are not null, it creates a new Date object with the value of the property and if not, * If they are not null, it creates a new Date object with the value of the property and if not,
* it creates a new Date object with the current hours to 0 and 24 respectively. * it creates a new Date object with the current hours to 0 and 24 respectively.
*/ */
const before = dateRange.before ? new Date(dateRange.before) : new Date(new Date().setHours(24, 0, 0, 0)); const before = useMemo(() => {
const after = dateRange.after ? new Date(dateRange.after) : new Date(new Date().setHours(0, 0, 0, 0)); return dateRange.before ? new Date(dateRange.before) : new Date(new Date().setHours(24, 0, 0, 0));
}, [dateRange.before]);
const after = useMemo(() => {
return dateRange.after ? new Date(dateRange.after) : new Date(new Date().setHours(0, 0, 0, 0));
}, [dateRange.after]);
/** /**
* numberOfDaysSelected is a set that holds the number of days selected in the dateRange. * numberOfDaysSelected is a set that holds the number of days selected in the dateRange.
* The loop iterates through the days starting from the after date's day to the before date's day. * The loop iterates through the days starting from the after date's day to the before date's day.
* If the before date's hour is 0, it skips it. * If the before date's hour is 0, it skips it.
*/ */
const numberOfDaysSelected = new Set( const numberOfDaysSelected = useMemo(() => {
[...Array(before.getDate() - after.getDate() + 1)].map((_, i) => after.getDate() + i) return new Set([...Array(before.getDate() - after.getDate() + 1)].map((_, i) => after.getDate() + i));
); }, [before, after]);
if (before.getHours() === 0) numberOfDaysSelected.delete(before.getDate()); if (before.getHours() === 0) numberOfDaysSelected.delete(before.getDate());
// Create repeating array with the number of hours for each day selected ...23,24,0,1,2... // Create repeating array with the number of hours for each day selected ...23,24,0,1,2...
const hoursInDays = Array.from({ length: numberOfDaysSelected.size * 24 }, (_, i) => i % 24); const hoursInDays = useMemo(() => {
return Array.from({ length: numberOfDaysSelected.size * 24 }, (_, i) => i % 24);
}, [numberOfDaysSelected]);
// function for handling the selected time from the provided list // function for handling the selected time from the provided list
const handleTime = (hour) => { const handleTime = useCallback(
(hour) => {
if (!hour) return; if (!hour) return;
const _timeRange = new Set([...timeRange]); const _timeRange = new Set([...timeRange]);
@ -112,11 +121,16 @@ const TimePicker = ({ dateRange, onChange }) => {
for (let i = 0; i < _timeRange.size; i++) { for (let i = 0; i < _timeRange.size; i++) {
setTimeRange((timeRange) => timeRange.add(Array.from(_timeRange)[i])); setTimeRange((timeRange) => timeRange.add(Array.from(_timeRange)[i]));
} }
}; },
[after, before, timeRange, dateRange.after, numberOfDaysSelected.size, onChange]
);
const isSelected = (after, before, idx) => { const isSelected = useCallback(
(idx) => {
return !!timeRange.has(idx); return !!timeRange.has(idx);
}; },
[timeRange]
);
// background colors for each day // background colors for each day
const isSelectedCss = 'bg-blue-600 transition-all duration-50 hover:rounded-none'; const isSelectedCss = 'bg-blue-600 transition-all duration-50 hover:rounded-none';
@ -151,7 +165,7 @@ const TimePicker = ({ dateRange, onChange }) => {
{hoursInDays.map((_, idx) => ( {hoursInDays.map((_, idx) => (
<div <div
key={idx} key={idx}
className={`${isSelected(dateRange.after, dateRange.before, idx) ? isSelectedCss : ''} className={`${isSelected(idx) ? isSelectedCss : ''}
${Math.min(...timeRange) === idx ? 'rounded-t-lg' : ''} ${Math.min(...timeRange) === idx ? 'rounded-t-lg' : ''}
${Math.max(...timeRange) === idx ? 'rounded-b-lg' : ''}`} ${Math.max(...timeRange) === idx ? 'rounded-b-lg' : ''}`}
> >