Skip to content

Commit

Permalink
improve tasks groups
Browse files Browse the repository at this point in the history
  • Loading branch information
fivaz committed May 18, 2024
1 parent 18de405 commit 3ea0679
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 111 deletions.
107 changes: 0 additions & 107 deletions src/lib/task/store.ts

This file was deleted.

File renamed without changes.
2 changes: 1 addition & 1 deletion src/routes/dashboard/profile/profile/Profile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Button from '$lib/components/button/Button.svelte';
import Input from '$lib/components/input/Input.svelte';
import { db } from '$lib/firebase';
import { storeAvatar } from '$lib/user-utis';
import { storeAvatar } from '$lib/user-utils';
import { type Auth, updateProfile } from 'firebase/auth';
import { doc, updateDoc } from 'firebase/firestore';
import { minidenticon } from 'minidenticons';
Expand Down
3 changes: 1 addition & 2 deletions src/routes/dashboard/tasks/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import TypedCollection from '$lib/components/typed-collection/TypedCollection.svelte';
import { auth, db } from '$lib/firebase';
import { buildEmptyToDo } from '$lib/task/build-utils';
import { sortTasksByDate } from '$lib/task/store';
import { getTotalDuration } from '$lib/task/time-utils';
import { BookOpenCheck } from '@steeze-ui/lucide-icons';
import { Icon } from '@steeze-ui/svelte-icon';
import { SignedIn, collectionStore, userStore } from 'sveltefire';
import { type SortedTaskType, queryUncompletedTasks } from './service';
import { type SortedTaskType, queryUncompletedTasks, sortTasksByDate } from './service';
import TaskRow from './task-row/TaskRow.svelte';
import TasksStats from './tasks-stats/TasksStats.svelte';
Expand Down
127 changes: 127 additions & 0 deletions src/routes/dashboard/tasks/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import type { AnyTask } from '$lib/task/utils';

import { DATE, DATE_FR } from '$lib/consts';
import { db } from '$lib/firebase';
import { getTaskDate } from '$lib/task/time-utils';
import {
addWeeks,
endOfWeek,
format,
isPast,
isToday,
isTomorrow,
isWithinInterval,
parse,
startOfWeek,
} from 'date-fns';
import { type Query, collection, query, where } from 'firebase/firestore';

export type SortedTaskType = Record<string, AnyTask[]> & Iterable<string>;
Expand All @@ -9,3 +22,117 @@ export function queryUncompletedTasks(userId: string) {
const tasksRef = collection(db, `users/${userId}/tasks`);
return query(tasksRef, where('isDone', '==', false)) as Query<AnyTask>;
}

function sortTasks(tasks: AnyTask[]) {
return tasks.sort((a, b) => {
const dateAString = 'date' in a ? a.date : a.deadline;
const dateBString = 'date' in b ? b.date : b.deadline;

const dateA = dateAString ? parse(dateAString, DATE, new Date()) : null;
const dateB = dateBString ? parse(dateBString, DATE, new Date()) : null;
if (!dateA) {
return 1;
}
if (!dateB) {
return -1;
}
return dateA.getTime() - dateB.getTime();
});
}

enum GROUPS {
NextWeek = 'Next week',
Overdue = 'Overdue',
Recurring = 'Recurring',
Someday = 'Someday',
Today = 'Today',
Tomorrow = 'Tomorrow',
Week = 'This week',
}

function isCurrentWeek(date: Date) {
return startOfWeek(new Date()).getTime() === startOfWeek(date).getTime();
}
function isNextWeek(date: Date): boolean {
const now = new Date();
const start = addWeeks(startOfWeek(now, { weekStartsOn: 1 }), 1);
const end = endOfWeek(start, { weekStartsOn: 1 });

return isWithinInterval(date, { end, start });
}

function getDateName(task: AnyTask): GROUPS | string {
const date = getTaskDate(task);

if ('recurringStartAt' in task) {
return GROUPS.Recurring;
}
if (!date) {
return GROUPS.Someday;
}
if (isToday(date)) {
return GROUPS.Today;
}
if (isPast(date)) {
return GROUPS.Overdue;
}
if (isTomorrow(date)) {
return GROUPS.Tomorrow;
}
if (isCurrentWeek(date)) {
return GROUPS.Week;
}
if (isNextWeek(date)) {
return GROUPS.NextWeek;
}
return format(date, DATE_FR);
}

function groupTasksByDate(tasks: AnyTask[]): Record<string, AnyTask[]> {
return tasks.reduce<Record<string, AnyTask[]>>((groups, task) => {
const date = getDateName(task);

if (!groups[date]) {
groups[date] = [];
}
groups[date].push(task);
return groups;
}, {});
}

function getTaskByOrderedDate(tasksByDate: Record<string, AnyTask[]>) {
const priorityObject: Record<string, number> = {
[GROUPS.NextWeek]: 5,
[GROUPS.Overdue]: 1,
[GROUPS.Recurring]: 8,
// Rest : 6
[GROUPS.Someday]: 7,
[GROUPS.Today]: 2,
[GROUPS.Tomorrow]: 3,
[GROUPS.Week]: 4,
};

function sorting(a: string, b: string) {
// 5 is the Number representing the rest
const priorityA = priorityObject[a] || 5;
const priorityB = priorityObject[b] || 5;

return priorityA - priorityB;
}

return {
[Symbol.iterator]: function* () {
const sortedKeys = Object.keys(this).sort(sorting);
for (const key of sortedKeys) {
yield key;
}
},
...tasksByDate,
};
}

export function sortTasksByDate(tasks: AnyTask[]): Record<string, AnyTask[]> & Iterable<string> {
const sortedTasks = sortTasks(tasks);
const tasksByDate = groupTasksByDate(sortedTasks);
return getTaskByOrderedDate(tasksByDate);
}
2 changes: 1 addition & 1 deletion src/routes/register/register/Register.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Button from '$lib/components/button/Button.svelte';
import { loginRoute, rootRoute } from '$lib/consts';
import { auth, db } from '$lib/firebase';
import { storeAvatar } from '$lib/user-utis';
import { storeAvatar } from '$lib/user-utils';
import { validator } from '@felte/validator-yup';
import { createForm } from 'felte';
import { FirebaseError } from 'firebase/app';
Expand Down

0 comments on commit 3ea0679

Please sign in to comment.