From dfc1b6bdeb92e99168ebdd1e9b866d3358512096 Mon Sep 17 00:00:00 2001 From: Courey Elliott Date: Fri, 11 Aug 2023 16:00:07 -0400 Subject: [PATCH] creating a note and displaying a list of note links that don't go anywhere yet. --- app.arc | 6 ++ app/lib/utils.ts | 6 ++ app/routes/data.new.tsx | 122 ++++++++++++++++++++++++++++----- app/routes/data/data.server.ts | 33 +++++++++ 4 files changed, 148 insertions(+), 19 deletions(-) create mode 100644 app/lib/utils.ts diff --git a/app.arc b/app.arc index 22e3266..949f7c7 100644 --- a/app.arc +++ b/app.arc @@ -18,3 +18,9 @@ plugin-remix runtime nodejs18.x region us-east-1 architecture arm64 + +@tables +notes + userId *String + noteId **String + PointInTimeRecovery true \ No newline at end of file diff --git a/app/lib/utils.ts b/app/lib/utils.ts new file mode 100644 index 0000000..711ac64 --- /dev/null +++ b/app/lib/utils.ts @@ -0,0 +1,6 @@ +// This is just for example. Typically you would get a user from the session. +// By making it reusable, you would only have to get the userId from the session +// in one place. This makes it portable. +export function getUserId() { + return 'test' +} diff --git a/app/routes/data.new.tsx b/app/routes/data.new.tsx index 4f87f67..6f14bb6 100644 --- a/app/routes/data.new.tsx +++ b/app/routes/data.new.tsx @@ -1,5 +1,5 @@ import type { DataFunctionArgs } from '@remix-run/node' -import { useFetcher } from '@remix-run/react' +import { Link, useFetcher, useLoaderData } from '@remix-run/react' import { Button, ButtonGroup, @@ -10,10 +10,12 @@ import { Label, Select, TextInput, + Textarea, } from '@trussworks/react-uswds' -import { useState } from 'react' +import { getUserId } from 'app/lib/utils' +import { useEffect, useRef, useState } from 'react' -import { getInstrumentData } from './data/data.server' +import { createNewNote, getInstrumentData, getNotes } from './data/data.server' import { tableOptions } from './data/data.table-options' function validateResultCountInput(input: string) { @@ -21,26 +23,47 @@ function validateResultCountInput(input: string) { return numericalInput.toString() === input && numericalInput <= 100 } +export async function loader({ request }: DataFunctionArgs) { + const userID = getUserId() + return await getNotes(userID) +} + export async function action({ request }: DataFunctionArgs) { + const userID = getUserId() const data = await request.formData() - const format = data.get('format')?.toString() - const resultCount = data.get('resultCount')?.toString() - const instrument = data.get('instrument')?.toString() - const ra = data.get('ra')?.toString() - const dec = data.get('dec')?.toString() - const radius = data.get('radius')?.toString() + const intent = data.get('intent')?.toString() + + if (intent === 'get-data') { + const format = data.get('format')?.toString() + const resultCount = data.get('resultCount')?.toString() + const instrument = data.get('instrument')?.toString() + const ra = data.get('ra')?.toString() + const dec = data.get('dec')?.toString() + const radius = data.get('radius')?.toString() + + if (!instrument || !format || !resultCount || !ra || !dec || !radius) + return null + const res = await getInstrumentData( + instrument, + format, + parseInt(resultCount), + parseFloat(ra), + parseFloat(dec), + parseInt(radius) + ) + return res + } else if (intent === 'new-note') { + const noteTitle = data.get('title')?.toString() + const noteBody = data.get('notes')?.toString() + if (!noteBody || !noteTitle) { + return null + } + await createNewNote(noteTitle, noteBody, userID) - if (!instrument || !format || !resultCount || !ra || !dec || !radius) return null - const res = await getInstrumentData( - instrument, - format, - parseInt(resultCount), - parseFloat(ra), - parseFloat(dec), - parseInt(radius) - ) - return res + } else { + return null + } } export default function () { @@ -51,13 +74,30 @@ export default function () { const [radius, setRadius] = useState('') const [resultCount, setResultCount] = useState('') const isValid = validateResultCountInput(resultCount) + const [noteTitle, setNoteTitle] = useState('') + const [noteBody, setNoteBody] = useState('') + + const noteRef = useRef(null) const fetcher = useFetcher() const results = fetcher.data + const fetchNotes = useFetcher() + const notes = useLoaderData() const resultArray = results?.request || ['no results'] const shouldDisableForm = !instrument || !format || !resultCount || !isValid || !ra || !dec || !radius + const shouldDisableNotes = !noteTitle || !noteBody + + useEffect(() => { + if ( + fetchNotes.state === 'idle' && + fetchNotes.data === null && + noteRef.current + ) { + noteRef.current.reset() + } + }, [fetchNotes.state, fetchNotes.data]) return (
@@ -163,6 +203,36 @@ export default function () { + +

Take Notes:

+ + + + { + setNoteTitle(e.target.value) + }} + /> + +