Skip to content

Commit

Permalink
fix stale data on activity feed when reseting & replace unstable_cach…
Browse files Browse the repository at this point in the history
…e with tags in fetch request in activity feed
  • Loading branch information
mrkarimoff committed May 22, 2024
1 parent f17148f commit 25a08b3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 58 deletions.
5 changes: 3 additions & 2 deletions actions/reset.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use server';

import { prisma } from '~/utils/db';
import { utapi } from '../app/api/uploadthing/core';
import { revalidatePath, revalidateTag } from 'next/cache';
import { requireApiAuth } from '~/utils/auth';
import { prisma } from '~/utils/db';
import { utapi } from '../app/api/uploadthing/core';

export const resetAppSettings = async () => {
await requireApiAuth();
Expand All @@ -21,6 +21,7 @@ export const resetAppSettings = async () => {

revalidatePath('/');
revalidateTag('appSettings');
revalidateTag('activityFeed');

// Remove all files from UploadThing:
await utapi.listFiles({}).then(({ files }) => {
Expand Down
45 changes: 42 additions & 3 deletions app/api/activity-feed/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
import { NextResponse, type NextRequest } from 'next/server';
import { searchParamsCache } from '~/app/dashboard/_components/ActivityFeed/SearchParams';
import { getActivities } from '~/queries/activityFeed';
import { requireApiAuth } from '~/utils/auth';
import { prisma } from '~/utils/db';

export async function GET(request: NextRequest) {
await requireApiAuth();

const rawParams = request.nextUrl.searchParams.entries();
const searchParams = searchParamsCache.parse(Object.fromEntries(rawParams));

const result = await getActivities(searchParams);
return NextResponse.json(result);
const { page, perPage, sort, sortField, filterParams } = searchParams;

// Number of items to skip
const offset = page > 0 ? (page - 1) * perPage : 0;

// Generate the dynamic filter parameters for the database call from the
// input filter params.
const queryFilterParams = filterParams
? {
OR: [
...filterParams.map(({ id, value }) => {
const operator = Array.isArray(value) ? 'in' : 'contains';
return {
[id]: { [operator]: value },
};
}),
],
}
: {};

// Transaction is used to ensure both queries are executed in a single transaction
const [count, events] = await prisma.$transaction([
prisma.events.count({
where: {
...queryFilterParams,
},
}),
prisma.events.findMany({
take: perPage,
skip: offset,
orderBy: { [sortField]: sort },
where: {
...queryFilterParams,
},
}),
]);

const pageCount = Math.ceil(count / perPage);


return NextResponse.json({ events, pageCount });
}
4 changes: 3 additions & 1 deletion app/dashboard/_components/ActivityFeed/ActivityFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const ActivityFeed = () => {
const { isPending, data } = useQuery({
queryKey: ['activityFeed', params.toString()],
queryFn: async () => {
const response = await fetch(`/api/activity-feed?${params.toString()}`);
const response = await fetch(`/api/activity-feed?${params.toString()}`, {
next: { tags: ['activityFeed'] },
});
return response.json() as Promise<{
events: Events[];
pageCount: number;
Expand Down
52 changes: 0 additions & 52 deletions queries/activityFeed.ts

This file was deleted.

0 comments on commit 25a08b3

Please sign in to comment.