Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 21, 2024
1 parent d043e6e commit 19cb087
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 11 deletions.
4 changes: 2 additions & 2 deletions next-enterprise/src/app/ThemeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import type {SyncTag} from '@sanity/client'
import {useTransition} from 'react'
import {randomColorTheme} from './actions'

export function ThemeButton({tags}: {tags: SyncTag[]}) {
export function ThemeButton() {
const [pending, startTransition] = useTransition()
return (
<button
disabled={pending}
onClick={() =>
startTransition(async () => {
await randomColorTheme(tags)
await randomColorTheme()
// Wait 2 seconds to stagger requests a little bit
await new Promise((resolve) => setTimeout(resolve, 2_000))
})
Expand Down
8 changes: 1 addition & 7 deletions next-enterprise/src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
'use server'

import type {SyncTag} from '@sanity/client'
import {revalidateTag} from 'next/cache'

export async function randomColorTheme(tags: SyncTag[]) {
export async function randomColorTheme() {
const res = await fetch('https://lcapi-examples-api.sanity.dev/api/random-color-theme', {
method: 'PUT',
})
for (const tag of tags) {
revalidateTag(tag)
}
return res.json()
}
4 changes: 2 additions & 2 deletions next-enterprise/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function RootLayout({
}: Readonly<{
children: React.ReactNode
}>) {
const {data, tags, fetchedAt} = await sanityFetch({query: THEME_QUERY})
const {data, fetchedAt} = await sanityFetch({query: THEME_QUERY})

return (
<html
Expand All @@ -31,7 +31,7 @@ export default async function RootLayout({
</Suspense>
{children}
<Suspense>
<ThemeButton tags={tags!} />
<ThemeButton />
</Suspense>
</div>
<Suspense>
Expand Down

0 comments on commit 19cb087

Please sign in to comment.