diff --git a/src/app/api/route.ts b/src/app/api/route.ts index 4a2f5a9..a39e8ea 100644 --- a/src/app/api/route.ts +++ b/src/app/api/route.ts @@ -1,64 +1,7 @@ import {NextRequest} from "next/server"; -import {getData, OverviewConfig} from "@/data/fetchWithSQL"; -export const semester1Revision: OverviewConfig = { - title: 'Semester 1 Revision', - periodKey: 'y5-s1-revision', - goalHours: 4, - subjects: [ - { - projectId: 195482340, - }, - { - title: 'Measure Theory', - projectId: 195519024, - }, - { - title: 'Quantum Mechanics', - projectId: 195518593, - }, - { - projectId: 195754611, - } - ], - timePeriod: { - start: "2023-12-15T00:00:00.000Z", - end: "2024-01-25T00:00:00.000Z" - } -} - -export const semester2: OverviewConfig = { - title: 'Semester 2', - periodKey: 'y5-s2', - goalHours: 7.5, - subjects: [ - { - projectId: 195754611, - }, - { - projectId: 199383703, - }, - { - projectId: 199383691, - }, - { - projectId: 198859760, - }, - { - projectId: 199383698, - }, - ], - timePeriod: { - start: "2024-02-03T00:00:00.000Z", - end: "2024-05-10T00:00:00.000Z" - } -} - -export type PeriodKey = keyof typeof periodMap; -const periodMap = { - 'y5-s1-revision': semester1Revision, - 'y5-s2': semester2, -} +import {getData} from "@/data/fetchData"; +import {periodMap} from "@/data/periods"; export async function GET(request: NextRequest): Promise { const period = request.nextUrl.searchParams.get('period'); diff --git a/src/app/sem1-revision/page.tsx b/src/app/sem1-revision/page.tsx index 991739f..64fc575 100644 --- a/src/app/sem1-revision/page.tsx +++ b/src/app/sem1-revision/page.tsx @@ -1,6 +1,5 @@ -import {semester1Revision} from "@/app/api/route"; import OverviewPageWrapper from "@/components/PageWrapper"; export default function Home() { - return + return } diff --git a/src/app/sem2/page.tsx b/src/app/sem2/page.tsx index 3f9e529..22ca54b 100644 --- a/src/app/sem2/page.tsx +++ b/src/app/sem2/page.tsx @@ -1,6 +1,5 @@ -import {semester2} from "@/app/api/route"; import OverviewPageWrapper from "@/components/PageWrapper"; export default function Home() { - return + return } diff --git a/src/components/OverviewPage.tsx b/src/components/OverviewPage.tsx index a744c1f..ee20d8d 100644 --- a/src/components/OverviewPage.tsx +++ b/src/components/OverviewPage.tsx @@ -3,10 +3,9 @@ import {SubjectComparisonCard} from "@/components/cards/subjectComparisonCard"; import {CalendarOverviewCard} from "@/components/cards/calendarOverviewCard"; import {SubjectOverviewCard} from "@/components/cards/subjectOverviewCard"; -import {PeriodKey} from "@/app/api/route"; import useSWR from "swr"; -import {Data} from "@/data/fetchData"; -import {OverviewConfig} from "@/data/fetchWithSQL"; +import {Data, OverviewConfig} from "@/data/fetchData"; +import {PeriodKey} from "@/data/periods"; function useData(periodKey: PeriodKey, initialData: Data = { projects: [], diff --git a/src/components/PageWrapper.tsx b/src/components/PageWrapper.tsx index 77b6f9a..cc4fec1 100644 --- a/src/components/PageWrapper.tsx +++ b/src/components/PageWrapper.tsx @@ -1,11 +1,13 @@ "use server"; -import {getData, OverviewConfig} from "@/data/fetchWithSQL"; import OverviewPage from "@/components/OverviewPage"; +import {getData} from "@/data/fetchData"; +import {PeriodKey, periodMap} from "@/data/periods"; -export default async function OverviewPageWrapper({config}: { - config: OverviewConfig +export default async function OverviewPageWrapper({period}: { + period: PeriodKey }) { + const config = periodMap[period]; const data = await getData(config); return ; } diff --git a/src/data/fetchData.ts b/src/data/fetchData.ts index b628428..d239b11 100644 --- a/src/data/fetchData.ts +++ b/src/data/fetchData.ts @@ -1,5 +1,9 @@ "use server"; +import {db} from "@/data/database"; +import * as dFns from "date-fns"; +import {PeriodKey} from "@/data/periods"; + export interface Data { projects: { projectId: number, @@ -15,3 +19,61 @@ export interface Data { // description: string, }[], } + +export interface OverviewConfig { + title: string, + periodKey: PeriodKey, + + subjects: { + title?: string, + projectId: number, + }[], + + goalHours: number, + + timePeriod: { + start: string, + end: string + }, +} + +export async function getData(config: OverviewConfig): Promise { + // If we're in build-time, we don't have access to the database + if (!db) { + return { + projects: [], + timeEntries: [], + } + } + + console.log('fetching data from SQL') + + let projectIds = config.subjects.map((subject) => subject.projectId.toString()); + + const projects = await db.selectFrom('project') + .select('raw_json') + .where('project.toggl_id', 'in', projectIds) + .execute(); + + const timeEntries = await db.selectFrom('time_entry') + .select(['project_id', 'raw_json']) + .where('project_id', 'in', projectIds) + .where('start', '>', dFns.parseISO(config.timePeriod.start)) + .where('start', '<', dFns.parseISO(config.timePeriod.end)) + .execute(); + + return { + projects: projects.map((project) => ({ + projectId: project.raw_json.id, + name: project.raw_json.name, + color: project.raw_json.color, + })), + + timeEntries: timeEntries.map((timeEntry) => ({ + projectId: parseInt(timeEntry.project_id!), + start: timeEntry.raw_json.start, + end: timeEntry.raw_json.end, + duration: timeEntry.raw_json.seconds, + })), + } +} diff --git a/src/data/fetchWithSQL.ts b/src/data/fetchWithSQL.ts deleted file mode 100644 index 84b38e8..0000000 --- a/src/data/fetchWithSQL.ts +++ /dev/null @@ -1,62 +0,0 @@ -import {Data} from "@/data/fetchData"; -import {db} from "@/data/database"; -import * as dFns from "date-fns"; -import {PeriodKey} from "@/app/api/route"; - -export interface OverviewConfig { - title: string, - periodKey: PeriodKey, - - subjects: { - title?: string, - projectId: number, - }[], - - goalHours: number, - - timePeriod: { - start: string, - end: string - }, -} - -export async function getData(config: OverviewConfig): Promise { - // If we're in build-time, we don't have access to the database - if (!db) { - return { - projects: [], - timeEntries: [], - } - } - - console.log('fetching data from SQL') - - let projectIds = config.subjects.map((subject) => subject.projectId.toString()); - - const projects = await db.selectFrom('project') - .select('raw_json') - .where('project.toggl_id', 'in', projectIds) - .execute(); - - const timeEntries = await db.selectFrom('time_entry') - .select(['project_id', 'raw_json']) - .where('project_id', 'in', projectIds) - .where('start', '>', dFns.parseISO(config.timePeriod.start)) - .where('start', '<', dFns.parseISO(config.timePeriod.end)) - .execute(); - - return { - projects: projects.map((project) => ({ - projectId: project.raw_json.id, - name: project.raw_json.name, - color: project.raw_json.color, - })), - - timeEntries: timeEntries.map((timeEntry) => ({ - projectId: parseInt(timeEntry.project_id!), - start: timeEntry.raw_json.start, - end: timeEntry.raw_json.end, - duration: timeEntry.raw_json.seconds, - })), - } -} diff --git a/src/data/periods.ts b/src/data/periods.ts new file mode 100644 index 0000000..38a7517 --- /dev/null +++ b/src/data/periods.ts @@ -0,0 +1,60 @@ +import {OverviewConfig} from "@/data/fetchData"; + +export const semester1Revision: OverviewConfig = { + title: 'Semester 1 Revision', + periodKey: 'y5-s1-revision', + goalHours: 4, + subjects: [ + { + projectId: 195482340, + }, + { + title: 'Measure Theory', + projectId: 195519024, + }, + { + title: 'Quantum Mechanics', + projectId: 195518593, + }, + { + projectId: 195754611, + } + ], + timePeriod: { + start: "2023-12-15T00:00:00.000Z", + end: "2024-01-25T00:00:00.000Z" + } +} + +export const semester2: OverviewConfig = { + title: 'Semester 2', + periodKey: 'y5-s2', + goalHours: 7.5, + subjects: [ + { + projectId: 195754611, + }, + { + projectId: 199383703, + }, + { + projectId: 199383691, + }, + { + projectId: 198859760, + }, + { + projectId: 199383698, + }, + ], + timePeriod: { + start: "2024-02-03T00:00:00.000Z", + end: "2024-05-10T00:00:00.000Z" + } +} + +export type PeriodKey = keyof typeof periodMap; +export const periodMap = { + 'y5-s1-revision': semester1Revision, + 'y5-s2': semester2, +}