Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

How to use it with @payloadcms/next-payload ? #55

Closed
roguesherlock opened this issue Aug 12, 2023 · 2 comments
Closed

How to use it with @payloadcms/next-payload ? #55

roguesherlock opened this issue Aug 12, 2023 · 2 comments

Comments

@roguesherlock
Copy link

roguesherlock commented Aug 12, 2023

I currently have this quick and dirty way to do this but I'm not sure if it's correct,

actions.ts

"use server"

// import { revalidatePath } from "next/cache"
import { getPayloadClient } from "@payload/payloadClient"

import { FormDataSchema, type FormDataSchemaType } from "./schema"

export async function saveForm(data: FormDataSchemaType) {
  const payload = await getPayloadClient()
  try {
    await payload.create({
      collection: "form-submissions",
      data: FormDataSchema.parse(data),
    })
    return [true, ""]
  } catch (e) {
    console.log(e)
    return [false, JSON.stringify(e)]
  }
}

schema.ts

import { z } from "zod"

export const FormDataSchema = z.object({
  form: z.string(),
  submissionData: z
    .object({
      field: z.string(),
      value: z.unknown(),
    })
    .array(),
})
export type FormDataSchemaType = z.infer<typeof FormDataSchema>
@IAmNatch
Copy link

This will definitely work.

One thing to note is "Currently, if a route uses a Server Action, it is required to render dynamically". If you'd prefer to have static or revalidated static pages, instead of dynamic, you can use a fetch (or graphQL) request to your payload API.

You can see an example of this here.

@jacobsfletch
Copy link
Member

jacobsfletch commented Dec 12, 2023

This plugin is now being maintained in the Packages Directory of the Payload Monorepo. This repo will soon be archived and all open issues including this one will be closed. Please refer to this open discussion for more details and to continue the conversation. Or feel free to open a new discussion around this, or even hop on our Discord and get help there.

@jacobsfletch jacobsfletch closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants