Skip to content

Commit

Permalink
let consumer set minDate for createListItems
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Nov 8, 2022
1 parent 2c5a2ef commit bc3d3d3
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/components/organisms/TaskList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ReactList from 'react-list';
import { Alert, AlertTitle, ListSubheader } from '@mui/material';
import Task from '../molecules/Task';
import useFilters from '../../lib/useFilters';
import browser from '../../lib/Browser';

interface TaskListProps {
newTask?: TaskType;
Expand Down Expand Up @@ -32,6 +33,7 @@ const TaskList = ({ newTask }: TaskListProps): JSX.Element => {
createListItems({
tasks: filtered,
newTask,
minDue: browser.getLastMidnight(),
});

setEntries(newEntries);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Browser {
return el.scrollTop / (el.scrollHeight - el.clientHeight);
}

getLastMidnight(date: Date): Date {
getLastMidnight(date: Date = browser.getNowDate()): Date {
const d = new Date(date);
d.setHours(0, 0, 0, 0);
return d;
Expand Down
7 changes: 3 additions & 4 deletions src/lib/createListItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ type Entries = (TaskType | string)[];
type Options = {
tasks: TaskType[];
newTask: TaskType | undefined;
minDue: Date;
};

export default function createListItems({ tasks, newTask }: Options): {
export default function createListItems({ tasks, newTask, minDue }: Options): {
entries: Entries;
newTaskIndex: number | undefined;
} {
const now = browser.getNowDate();
const lastMidnight = browser.getLastMidnight(now);
const sortedTasks = sortTasks(tasks);

let lastTitle: string;
let newTaskIndex: number | undefined = undefined;

const entries = sortedTasks.reduce((acc: Entries, t: TaskType): Entries => {
if (new Date(t.due) < lastMidnight) return acc;
if (new Date(t.due) < minDue) return acc;

const title = makeTitle(t);
const shouldAddHeading = title !== lastTitle || !acc.length;
Expand Down

1 comment on commit bc3d3d3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.