Refactor Invoice POST Handler to Next.js Server Action

Ben Patton
InstructorBen Patton
Share this video with your friends

Social Share Links

Send Tweet
Published 9 months ago
Updated 8 months ago

With our customer POST handler now moved to a server action your goal is to do the same thing with the invoice POST handler

Instructor: [0:00] Now that we've created our server action for creating a customer, your challenge in this video is to do the same thing with our invoices.

[0:12] Remove the invoices route and create an invoices action, and then in the form to create an invoice, replace the React Hook Form and use Query code with the server action.

[0:23] I'll just show my solution. One thing you'll need to do is make sure you pass the customers down to the dialog component, where we have the form for creating an invoice.

[0:34] The reason being is that before we had been using React Query to call our GET customers API route, but that route no longer exists, so we need to pass those customers down from a server component to the dialog component.

[0:47] The first thing to note that I did was I added this create invoice form. This is a new form that's under the full stack forms here. In this server component, we get our customers and we pass it to this Create Invoice form, so we can look at this Create Invoice form.

[1:02] Here, I refactored it. It was using React Hook Form and React Query, and so I refactored it to use a server action. Like our customer action, we have our invoice form ref for our form to clear it once we submit it.

[1:15] Here we have our action with the form data, and then we pass that form data to our add invoice action. Then once that action completes, we set open to so the dialog closes.

[1:26] Now, if we go look in our action here, most of this was originally copied from the invoice API route, which we deleted. The main difference here is that instead of destructuring from the request, we are just getting them straight from the form data. Then we do our normal checks, and then we add it assuming there is no errors.

[1:45] Then, at the end, we redirect to that invoice ID.

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today