-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add admin area for one-off global cache invalidations
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 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,28 @@ | ||
'use client'; | ||
import { Button } from '@/components/ui/button'; | ||
import { ReactNode, useState } from 'react'; | ||
|
||
interface AdminActionButtonProps { | ||
adminAction: () => Promise<void>; | ||
children?: ReactNode; | ||
} | ||
|
||
export const AdminActionButton = ({ | ||
adminAction, | ||
children, | ||
}: AdminActionButtonProps) => { | ||
const [log, setLog] = useState<string>(); | ||
return ( | ||
<form action={adminAction}> | ||
<Button | ||
type='submit' | ||
onClick={() => { | ||
setLog('queued action. check server logs for details.'); | ||
}} | ||
> | ||
{children} | ||
</Button> | ||
{log && <p>Log: {log}</p>} | ||
</form> | ||
); | ||
}; |
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,43 @@ | ||
import { notFound } from 'next/navigation'; | ||
import { currentUser } from '@clerk/nextjs/server'; | ||
import { hasAdminPermissions } from '@/lib/auth'; | ||
import { z } from 'zod'; | ||
import { getRecipes } from '@/lib/repository/recipe-store/read'; | ||
import { revalidatePath } from 'next/cache'; | ||
import { AdminActionButton } from './admin-action-button'; | ||
|
||
const invalidationSchema = z.array( | ||
z.object({ | ||
id: z.number(), | ||
title: z.string(), | ||
}) | ||
); | ||
|
||
const inavalidateAllCachesAction = async () => { | ||
'use server'; | ||
const result = await getRecipes({ keys: ['id', 'title'] }); | ||
const rows = invalidationSchema.parse(result); | ||
for (const { id, title } of rows) { | ||
console.log(`admin: invalidating cache for ${title} page`); | ||
revalidatePath(`/recipes/${id}`); | ||
} | ||
console.log('admin: finished revalidating paths'); | ||
}; | ||
|
||
const AdminStuff = async () => { | ||
const user = await currentUser(); | ||
if (!hasAdminPermissions(user)) { | ||
return notFound(); | ||
} | ||
|
||
return ( | ||
<div className='m-auto prose prose-zinc'> | ||
<h1>Admin Stuff</h1> | ||
<AdminActionButton adminAction={inavalidateAllCachesAction}> | ||
Invalidate All Caches | ||
</AdminActionButton> | ||
</div> | ||
); | ||
}; | ||
|
||
export default AdminStuff; |