From the course: Next.js Ecommerce: Build a Shopping Platform from Scratch

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Create and update review action

Create and update review action

So we have our review list component and the review form component that opens up this dialog form. Now we want to create the action that that form is going to submit to. So let's go to lib actions, and we're going to create a new file here called review.actions. And obviously, anything to do with reviews and the database is going to go here. So first off, let's add use server because this is a server action. And then let's create our function, which will create and update reviews. So this will be used whether it's creating a new one or if we're updating our existing review. Because if I already created a review for this, if I click this button, my current review is going to be in that form. So let's create this. We want to export async function. We'll call it Create Update Review. And what it's going to take in is data, so a data object. And that's going to have a type that we're going to infer from Zod, from our schema, or using Zod. So let's say we'll set this to z.infer, and we…

Contents