-
Notifications
You must be signed in to change notification settings - Fork 2
moving rsvps to local api route #14
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
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d5269ee
no pii for you :trolley:
boykisserchan 4eb02cc
bleh
boykisserchan 85219a1
i am bad at coding
boykisserchan 79ee93d
start caching rsvps and move to local api for fast data
boykisserchan c87d44b
why wasn't this commited?
boykisserchan 7c9dcee
Update rsvp.ts
boykisserchan afd51cc
Update RSVP.tsx
boykisserchan c986e07
rsvp work
boykisserchan 3240f29
Merge branch 'main' into dev
boykisserchan 9116ec2
update subtitle
boykisserchan e32f50f
throw error to catch
boykisserchan 5c64edd
security :thumbup:
boykisserchan 7682a9d
Update src/pages/api/rsvp.ts
boykisserchan e4a2c02
Apply suggestion from @Copilot
boykisserchan a7691b0
Merge branch 'main' into dev
boykisserchan 684e4a1
install :3
boykisserchan 091130c
something something return the erorr
boykisserchan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,15 +1,17 @@ | ||
| "use client" | ||
|
|
||
| import useSWR from "swr"; | ||
|
|
||
| const fetcher = (url: string) => fetch(url).then(res => res.json()); | ||
|
|
||
| export default function RSVP() { | ||
| const { data, error } = useSWR("https://aces.femboyin.tech/count", fetcher, { | ||
| refreshInterval: 60000, | ||
| const { data, error } = useSWR("/api/rsvp", fetcher, { | ||
| refreshInterval: 10000, | ||
| revalidateOnFocus: false, | ||
| }); | ||
|
|
||
| if (error) return <span className="font-bold text-rose-300">error!</span>; | ||
| if (!data) return <span className="font-bold animate-pulse ">...</span> | ||
| if (!data || data.count === -1) return <span className="font-bold animate-pulse ">...</span> | ||
|
|
||
| return <span className="font-bold">{data.record_count}</span> | ||
| return <span className="font-bold">{data.count}</span> | ||
| } | ||
This file contains hidden or 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 hidden or 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,48 @@ | ||
| import { NextApiRequest, NextApiResponse } from "next"; | ||
|
|
||
boykisserchan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async function getCount() { | ||
| let offset; | ||
| let count = 0; | ||
|
|
||
| do { | ||
| const url = new URL(`https://api.airtable.com/v0/${process.env.RSVP_AIRTABLE_BASE_ID}/RSVPs`); | ||
boykisserchan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (offset) url.searchParams.set("offset", offset); | ||
|
|
||
| const res = await fetch(url, { | ||
| headers: { | ||
| Authorization: `Bearer ${process.env.RSVP_AIRTABLE_API_KEY}`, | ||
| }, | ||
| }); | ||
| if (!res.ok) { | ||
| console.error(res.statusText); | ||
| throw new Error(res.statusText); | ||
| } | ||
| const data = await res.json(); | ||
| count += data.records?.length ?? 0; | ||
| offset = data.offset; | ||
boykisserchan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (offset) await new Promise((resolve) => setTimeout(resolve, 200)); | ||
| } while (offset); | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| let cached = { value: -1, updated: 0 }; | ||
|
|
||
| // Cache duration in milliseconds | ||
| const CACHE_DURATION = 30000; | ||
|
|
||
| export default async function handler(_req: NextApiRequest, res: NextApiResponse) { | ||
boykisserchan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const now = Date.now(); | ||
| if (now - cached.updated > CACHE_DURATION) { | ||
| try { | ||
| const count = await getCount(); | ||
| cached = { value: count, updated: now }; | ||
| console.log("cached value", cached.value, "updatedAt", new Date(cached.updated).toISOString()); | ||
| } catch (err: unknown) { | ||
| console.error("getCount failed:", err); | ||
| res.status(500).json({ error: "couldnt get count" }); | ||
| } | ||
| } | ||
| res.status(200).json({ count: cached.value }); | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.