Skip to content

Commit

Permalink
add admin area for one-off global cache invalidations
Browse files Browse the repository at this point in the history
  • Loading branch information
stilt0n committed Jan 12, 2025
1 parent d967b3a commit 3e64246
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/admin/admin-action-button.tsx
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>
);
};
43 changes: 43 additions & 0 deletions app/admin/page.tsx
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;

0 comments on commit 3e64246

Please sign in to comment.