From 0aeba4c6325939a1095e638a3d7f28a992f1458a Mon Sep 17 00:00:00 2001 From: Leonardo Negreiros de Oliveira Date: Fri, 19 Jan 2024 11:54:44 -0300 Subject: [PATCH] Create week view --- frontend/src/app/tasks/utils/time.ts | 12 ++-- frontend/src/app/tasks/week/page.tsx | 86 +++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/frontend/src/app/tasks/utils/time.ts b/frontend/src/app/tasks/utils/time.ts index e4a96e4a2..baecb25b2 100644 --- a/frontend/src/app/tasks/utils/time.ts +++ b/frontend/src/app/tasks/utils/time.ts @@ -4,11 +4,15 @@ export const convertTimeToMinutes = (time: string) => { return parseInt(hour) * 60 + parseInt(minute) } +export const convertMinutesToTime = (timeInMinutes: number) => { + const hours = Math.floor(timeInMinutes / 60) + const minutes = timeInMinutes % 60 + + return `${hours}h ${minutes}m` +} + export const getTimeDifference = (startMinutes: number, endMinutes: number) => { const timeDiff = endMinutes - startMinutes - const hours = Math.floor(timeDiff / 60) - const minutes = timeDiff % 60 - - return `${hours}h ${minutes}m` + return convertMinutesToTime(timeDiff) } diff --git a/frontend/src/app/tasks/week/page.tsx b/frontend/src/app/tasks/week/page.tsx index eabd3abf4..fe1377aa0 100644 --- a/frontend/src/app/tasks/week/page.tsx +++ b/frontend/src/app/tasks/week/page.tsx @@ -1,3 +1,87 @@ +import { makeGetTasks } from '@/infra/task/getTasks' +import { serverFetch } from '@/infra/lib/serverFetch' +import { authOptions } from '@/app/api/auth/[...nextauth]/route' +import { getServerSession } from 'next-auth' +import { format, sub, eachDayOfInterval } from 'date-fns' +import { Task } from '@/domain/Task' +import { Typography, Box, Stack } from '@mui/joy' +import { SimpleTaskBox } from '../components/TaskBox' +import { convertTimeToMinutes, convertMinutesToTime } from '../utils/time' + +const getTasks = async (startTime: string, endTime: string) => { + const apiClient = await serverFetch() + const getTasks = makeGetTasks(apiClient) + const session = await getServerSession(authOptions) + const { id: userId } = session!.user + + const tasks = await getTasks({ userId, startTime, endTime }) + + // Group tasks by date and add the total time for that date + return tasks.reduce; time: number }>>((acc, task) => { + const startTime = convertTimeToMinutes(task.startTime) + const endTime = convertTimeToMinutes(task.endTime) + const timeDiff = endTime - startTime + + if (acc[task.date]) { + const { tasks, time } = acc[task.date] + + return { ...acc, [task.date]: { tasks: [...tasks, task], time: time + timeDiff } } + } + + return { ...acc, [task.date]: { tasks: [task], time: timeDiff } } + }, {}) +} + export default async function WeekView() { - return
Week!
+ const today = new Date() + const sevenDaysAgo = sub(today, { days: 6 }) + const dateRange = eachDayOfInterval({ start: sevenDaysAgo, end: today }) + const groupedTasks = await getTasks( + format(sevenDaysAgo, 'yyyy-MM-dd'), + format(today, 'yyyy-MM-dd') + ) + + return ( + + {dateRange.map((date) => { + const formattedDate = format(date, 'eeee, do') + const dateTasks = groupedTasks[format(date, 'yyyy-MM-dd')] + + return ( + + + {formattedDate} + + {dateTasks?.time ? convertMinutesToTime(dateTasks.time) : '0h 0m'} + + + + {dateTasks?.tasks.map((task) => { + return ( + + + + ) + })} + + ) + })} + + ) }