From 4a0ddada22cb4552f405ac23cbc7a3e110892e23 Mon Sep 17 00:00:00 2001 From: Michael Nolivos Date: Wed, 17 Apr 2024 23:55:45 -0400 Subject: [PATCH] feat: note journal --- app/routes/tracker-journal/route.tsx | 119 +++++++++----------------- src/pages/tracker/daily-note-form.tsx | 6 +- src/pages/tracker/dashboard.tsx | 12 ++- 3 files changed, 55 insertions(+), 82 deletions(-) diff --git a/app/routes/tracker-journal/route.tsx b/app/routes/tracker-journal/route.tsx index 877d9ae..6375d87 100644 --- a/app/routes/tracker-journal/route.tsx +++ b/app/routes/tracker-journal/route.tsx @@ -1,107 +1,68 @@ -import React, { useEffect, useState } from 'react' -import db, { ISupplement, INote } from 'src/pages/tracker/db' +import React from 'react' +import db, { INote } from 'src/pages/tracker/db' import { formatDateKey } from 'src/lib/utils' import ContentHeader from 'src/components/content-header.tsx' -import { - Pagination, - PaginationContent, - PaginationItem, - PaginationNext, - PaginationPrevious, -} from 'src/components/ui/pagination' -interface JournalEntry { - [date: string]: (ISupplement | INote)[] -} +import DailyNoteForm from 'src/pages/tracker/daily-note-form' +import { useLiveQuery } from 'dexie-react-hooks' const TrackerJournal: React.FC = () => { - const [entries, setEntries] = useState({}) - const [page, setPage] = useState(0) - const pageSize = 50 - const [totalEntries, setTotalEntries] = useState(0) - - const loadEntries = async () => { - const supplements: ISupplement[] = await db.supplements - .orderBy('date') - .reverse() - .offset(page * pageSize) - .limit(pageSize) - .toArray() - const notes: INote[] = await db.notes - .orderBy('date') - .reverse() - .offset(page * pageSize) - .limit(pageSize) - .toArray() - - const combinedEntries: JournalEntry = supplements - // @ts-ignore - .concat(notes) - .reduce((acc: JournalEntry, entry: ISupplement | INote) => { - const { date } = entry - if (!acc[date]) { - acc[date] = [] - } - acc[date].push(entry) - return acc - }, {}) + const journalNotes = useLiveQuery(() => db.notes.orderBy('date').reverse().toArray(), []) - setEntries((prevEntries) => ({ - ...prevEntries, - ...combinedEntries, - })) - } + const currentDate = formatDateKey(new Date()) + let lastDate = '' - useEffect(() => { - loadEntries() - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [page]) + const journalElements = journalNotes?.flatMap((note, index) => { + const noteDate = formatDateKey(new Date(note.date)) + const elements = [] - useEffect(() => { - const fetchTotalEntries = async () => { - const totalSupplements = await db.supplements.count() - const totalNotes = await db.notes.count() - setTotalEntries(totalSupplements + totalNotes) + if (noteDate !== lastDate) { + elements.push( +
  • +

    {noteDate === currentDate ? 'Today' : noteDate}

    +
  • , + ) + lastDate = noteDate } - fetchTotalEntries() - }, []) + elements.push( +
  • +

    {note.content}

    +
  • , + ) + return elements + }) return (
    - - {Object.keys(entries).length === 0 ? ( -
    No journal entries found.
    +
    + +
    + + {journalNotes?.length === 0 ? ( +
    No notes found.
    ) : ( -
      - {Object.entries(entries).map(([date, contents], index) => ( -
    • -

      {formatDateKey(new Date(date))}

      - {contents.map((content, contentIndex) => ( -

      - {'content' in content - ? `Note: ${content.content}` - : `${content.name} - ${content.dosage} ${content.dosageUnit}`} -

      - ))} -
    • - ))} -
    +
      {journalElements}
    )} - + {/* - setPage((oldPage) => Math.max(0, oldPage - 1))} href="#" /> + setPage((oldPage) => Math.max(0, oldPage - 1))} + href="#" + /> {page + 1} setPage((oldPage) => Math.min(oldPage + 1, Math.floor(totalEntries / pageSize)))} - href="#" + href="#next" /> - + */}
    ) } diff --git a/src/pages/tracker/daily-note-form.tsx b/src/pages/tracker/daily-note-form.tsx index 9397ba9..a2222c2 100644 --- a/src/pages/tracker/daily-note-form.tsx +++ b/src/pages/tracker/daily-note-form.tsx @@ -15,17 +15,20 @@ import { Textarea } from 'src/components/ui/textarea' import db from './db' import { useState } from 'react' import { toast } from 'react-hot-toast' +import { useNavigate } from '@remix-run/react' export default function DailyNoteForm({ dateKey }: { dateKey: string }) { const [note, setNote] = useState('') + const navigate = useNavigate() + // const recentNotes = useLiveQuery(() => db.notes.orderBy('date').limit(3).toArray(), [dateKey]) return ( @@ -64,6 +67,7 @@ export default function DailyNoteForm({ dateKey }: { dateKey: string }) { createdAt: new Date(), }) setNote('') + navigate('/tracker-journal') }} > Save Note diff --git a/src/pages/tracker/dashboard.tsx b/src/pages/tracker/dashboard.tsx index 0cc6a75..114ae29 100644 --- a/src/pages/tracker/dashboard.tsx +++ b/src/pages/tracker/dashboard.tsx @@ -1,5 +1,5 @@ import { differenceInCalendarDays, isSameDay, startOfDay } from 'date-fns' -import { BotIcon, CheckCircle2 } from 'lucide-react' +import { BookIcon, BotIcon, CheckCircle2 } from 'lucide-react' import { Button } from 'src/components/ui/button' import DieOffSymptoms from './die-off-symptoms' @@ -104,7 +104,15 @@ const Dashboard: React.FC = ({ startDate }) => { Edit - +
    + + +