#revalidate path is not updating old data

23 messages · Page 1 of 1 (latest)

torn coral
#

I'm using a server action to delete a product that is displayed in a table, but it doesn't update the data when I use the server action, what am I doing wrong? I saw in the documentation that it is simply calling revalidatePath

Server Action:

`export async function deleteProduct(id) {

const session = await auth()
if (!session) return { error: 'Não autenticado' }

const headerList = headers();
const forwardedHost = headerList.get("x-forwarded-host");
const forwardedProto = headerList.get("x-forwarded-proto") || 'http';
const hostname = new URL(${forwardedProto}://${forwardedHost}).hostname;

const user = await prisma.user.findUnique({
where: { domain: hostname, id: session.user.id }
});

if (!user) {
return { error: 'Usuário não encontrado.' }
}

const product = await prisma.product.findFirst({
where: {
id: id,
userId: user.role == "ADMIN" ? undefined : user.id,
}
});

if (!product) {
return { error: 'Produto não encontrado ou não autorizado a deletar.' };
}

await prisma.product.delete({
where: {
id: product.id,
}
});

revalidatePath('/p')

return 'Deletado com sucesso ' + product.title

}`

Page.jsx:

`export default async function Products() {
const data = await findProducts(1);

return (
<div>
<h1 className="text-xl font-medium mb-4 text-white">Produtos</h1>
<ProductDataTable columns={columns} data={{ ...data, pageSize: 50 }} />;
</div>
);
}`

how do I call the action:

<AlertDialogAction onClick={async () => { await deleteProduct(row.original.id)}} className="bg-red-800 hover:bg-red-900 w-full text-white shadow"> Continue </AlertDialogAction>

lyric creekBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

radiant bridge
torn coral
radiant bridge
torn coral
#

yes

#

Do you want me to record the screen?

radiant bridge
torn coral
radiant bridge
torn coral
#

Does useRouter need to be in a react component right? because I call the action in the columns file of the tanstack table

`const router = useRouter();

export const columns = [
{
header: "Ações",
id: "actions",
cell: ({ row }) => {
return (
<div className="flex gap-x-1.5 items-center ">
<TbEdit size={15} className="mr-2 cursor-pointer" />
<AlertDialogHeader>
<AlertDialogTitle>Você tem certeza disso?</AlertDialogTitle>
<AlertDialogDescription className="text-slate-200">
Essa ação não pode ser revertida, isso irá apagar o produto{" "}
{row.original.title}
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel className="bg-slate-200 shadow w-full text-gray-800">
Cancel
</AlertDialogCancel>
<AlertDialogAction
onClick={async () => {
let res = await deleteProduct(row.original.id);
toast(res?.error || res, {
style: {
borderRadius: "10px",
background: "#333",
color: "#fff",
},
});
router.refresh();
}}
className="bg-red-800 hover:bg-red-900 w-full text-white shadow"
>
Continue
</AlertDialogAction>
</div>
);
},

enableSorting: false,
enableHiding: false,

},
];`

#

So it shouldn't work for me then

#

Nothing makes this data update 😭😭

radiant bridge
#

Can you show the „findProducts“ function?

torn coral
#

export async function findProducts(page = 1, orderBy, searchTerm = '', type = 'ALL') {

const session = await auth();
if (!session) return { error: 'Não autenticado' };

let pageSize = 50;
const skip = (page - 1) * pageSize;

const products = await prisma.product.findMany({
skip,
take: pageSize,
orderBy: orderBy?.id ? { [orderBy.id]: orderBy.desc ? 'desc' : 'asc' } : { createdAt: 'desc' },
where: {
userId: session.user.role == "ADMIN" ? undefined : session.user.id,
title: {
contains: searchTerm ? searchTerm : undefined,
},
type: type === 'ALL' ? undefined : type
}
});

const total = await prisma.product.count({
where: {
userId: session.user.role == "ADMIN" ? undefined : session.user.id,
title: {
contains: searchTerm ? searchTerm : undefined,
},
type: type === 'ALL' ? undefined : type,
}
});

return { products, total };
};

radiant bridge
torn coral
#

But neither router.refresh() nor revalidatePath() worked

#

Do you have any idea what it could be?

radiant bridge
torn coral
#

I found the solution in case anyone has the same problem, I put the data in a useState