Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create server and client pages #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions app/client/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';

import { useEffect, useState } from 'react';
import { JournalEntry } from '@prisma/client';
import JournalEntryBox from '@/components/journalEntryBox';

export default function Client() {
const [journalEntry, setJournalEntry] = useState<JournalEntry>();

useEffect(() => {
const fetchData = async () => {
const response = await fetch('/api/journalEntry?date=1-1-1944');
const data = await response.json();
setJournalEntry(data);
};

fetchData();
}, []);

return (
<main className='min-h-screen'>
<div className='grid grid-cols-2 gap-6'>
{journalEntry && (
<JournalEntryBox key={journalEntry.id} {...journalEntry} />
)}
</div>
</main>
);
}
2 changes: 2 additions & 0 deletions app/home.components/navbar/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const limelight = Limelight({
});

const links = [
{ name: 'Server', href: '/server' },
{ name: 'Client', href: '/client' },
{
name: 'Browse',
href: '/browse',
Expand Down
73 changes: 73 additions & 0 deletions app/server/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import prisma from '@/utils/prisma';
import JournalEntryBox from '@/components/journalEntryBox';

async function getJournalEntriesForYear(yearString: string) {
try {
const year = Number(yearString);
const startDate = new Date(Date.UTC(year, 0, 1));

const entries = await prisma.journalEntry.findMany({
where: {
date: {
gte: startDate,
lt: new Date(Date.UTC(year + 1, 0, 1)),
},
},
include: {
starredBy: true,
readBy: true,
},
orderBy: {
date: 'asc',
},
});

return entries;
} catch (error) {
console.error(error);
}
}

async function getJournalEntryOnDay(dateString: string) {
try {
const parsedDate = new Date(dateString);
const utcDate = new Date(
Date.UTC(
parsedDate.getUTCFullYear(),
parsedDate.getUTCMonth(),
parsedDate.getUTCDate(),
),
);

const entry = await prisma.journalEntry.findUnique({
where: { date: utcDate },
select: {
id: true,
content: true,
date: true,
starredBy: true,
readBy: true,
startPage: true,
endPage: true,
},
});

return entry;
} catch (error) {
console.error(error);
}
}

export default async function Browse() {
const journalEntry = await getJournalEntryOnDay('1-1-1944');

return (
<main className='min-h-screen'>
<div className='grid grid-cols-2 gap-6'>
{journalEntry && (
<JournalEntryBox key={journalEntry.id} {...journalEntry} />
)}
</div>
</main>
);
}