From 58c542bc5e06649de1df8133c683f6b09291155c Mon Sep 17 00:00:00 2001 From: Harry Wolff Date: Thu, 30 Jul 2020 22:48:38 -0400 Subject: [PATCH] Add react-query and use it to make data requests --- components/AddItemForm.tsx | 56 +++++++++++++++++++++++++++++++++++++ components/content.tsx | 32 +-------------------- components/sign-in.tsx | 19 +++++++------ hooks/api-hooks.ts | 28 +++++++++++++++++++ package.json | 1 + pages/api/items.ts | 57 ++++++++++++++++++++++++++++++++++++++ pages/index.tsx | 7 ++++- yarn.lock | 12 ++++++++ 8 files changed, 171 insertions(+), 41 deletions(-) create mode 100644 components/AddItemForm.tsx create mode 100644 hooks/api-hooks.ts create mode 100644 pages/api/items.ts diff --git a/components/AddItemForm.tsx b/components/AddItemForm.tsx new file mode 100644 index 0000000..d3710e3 --- /dev/null +++ b/components/AddItemForm.tsx @@ -0,0 +1,56 @@ +import { useState } from 'react'; +import { useSession } from 'next-auth/client'; +import { useAddItem, useClearItems } from 'hooks/api-hooks'; + +export default function AddItemForm() { + const [session] = useSession(); + + const [title, setTitle] = useState(''); + const [mutate] = useAddItem(); + const [clearItems] = useClearItems(); + + const submitForm = (e: React.SyntheticEvent) => { + e.preventDefault(); + mutate({ + title, + description: `Thank you ${session.user.name} for the idea!`, + }); + setTitle(''); + }; + + if (!session) { + return null; + } + + return ( +
+ setTitle(e.target.value)} + /> +
+ + +
+
+ ); +} + +const buttonStyles = 'p-1 border bg-gray-400 shadow-sm'; diff --git a/components/content.tsx b/components/content.tsx index e1c81ab..ccb688c 100644 --- a/components/content.tsx +++ b/components/content.tsx @@ -1,33 +1,3 @@ -import faker from 'faker'; - -faker.seed(1); - -function generateItem() { - return { - _id: faker.random.uuid(), - title: faker.lorem.words(), - description: faker.lorem.paragraph(), - created: faker.date.past().toUTCString(), - updated: faker.date.past().toUTCString(), - category: faker.random.arrayElement(['Tutorial', 'Opinion', 'Vlog']), - createdBy: faker.random.uuid(), - status: faker.random.arrayElement([ - 'open', - 'accepted', - 'declined', - 'completed', - ]), - votes: faker.random.number(100), - // votes: { - // up: [], - // down: [], - // }, - }; -} - -// @ts-expect-error -const items = [...Array(10).keys()].map((i) => generateItem()); - function Item({ item }) { return (
@@ -49,7 +19,7 @@ function Item({ item }) { ); } -export default function Content() { +export default function Content({ items }) { return (