-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add react-query and use it to make data requests
- Loading branch information
Showing
8 changed files
with
171 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ( | ||
<form | ||
className="flex flex-col mx-auto max-w-sm border border-gray-400 rounded p-2" | ||
onSubmit={submitForm} | ||
> | ||
<input | ||
type="text" | ||
className="border" | ||
value={title} | ||
onChange={(e) => setTitle(e.target.value)} | ||
/> | ||
<div className="flex flex-row mt-2"> | ||
<button | ||
className={`${buttonStyles} flex-grow`} | ||
type="submit" | ||
onClick={submitForm} | ||
> | ||
Add | ||
</button> | ||
<button | ||
type="button" | ||
className={`${buttonStyles} ml-2`} | ||
onClick={clearItems} | ||
> | ||
X | ||
</button> | ||
</div> | ||
</form> | ||
); | ||
} | ||
|
||
const buttonStyles = 'p-1 border bg-gray-400 shadow-sm'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useQuery, useMutation, queryCache } from 'react-query'; | ||
|
||
export function useItems() { | ||
return useQuery('items', () => fetch('/api/items').then((res) => res.json())); | ||
} | ||
|
||
const addItem = (body) => { | ||
return fetch('/api/items', { | ||
method: 'POST', | ||
body: JSON.stringify(body), | ||
}); | ||
}; | ||
|
||
export function useAddItem() { | ||
return useMutation(addItem, { | ||
onSuccess() { | ||
queryCache.invalidateQueries('items'); | ||
}, | ||
}); | ||
} | ||
|
||
export function useClearItems() { | ||
return useMutation(() => fetch('/api/items', { method: 'DELETE' }), { | ||
onSuccess() { | ||
queryCache.invalidateQueries('items'); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
|
||
import faker from 'faker'; | ||
|
||
faker.seed(1); | ||
|
||
function generateItem({ title = null, description = null } = {}) { | ||
return { | ||
_id: faker.random.uuid(), | ||
title: title ?? faker.lorem.words(), | ||
description: 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: [], | ||
// }, | ||
}; | ||
} | ||
|
||
let postRequests = []; | ||
|
||
export default (req: NextApiRequest, res: NextApiResponse) => { | ||
const items = postRequests | ||
// @ts-expect-error | ||
.concat([...Array(10).keys()]) | ||
.map((item) => generateItem(typeof item === 'number' ? undefined : item)); | ||
|
||
if (req.method === 'GET') { | ||
return res.status(200).json(items); | ||
} | ||
|
||
if (req.method === 'DELETE') { | ||
postRequests = []; | ||
return res.status(200).send('deleted'); | ||
} | ||
|
||
const parsedBody = JSON.parse(req.body); | ||
|
||
postRequests.unshift(parsedBody); | ||
|
||
// don't memory leak | ||
if (postRequests.length > 9) { | ||
postRequests = []; | ||
} | ||
|
||
res.status(200).send('added'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58c542b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: