Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builds contact form that submits to Sanity #63

Merged
merged 10 commits into from
Jan 19, 2022
31 changes: 31 additions & 0 deletions functions/submission-created.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const sanityClient = require("@sanity/client");
const client = sanityClient({
projectId: process.env.SANITY_PROJECT_ID,
dataset: process.env.SANITY_DATASET,
token: process.env.SANITY_ACCESS_TOKEN,
useCDN: false,
});

exports.handler = async function (event, context, callback) {
const { payload } = JSON.parse(event.body);

const isContactForm = payload.data.formId === "contact-form";

// Build the document JSON and submit it to SANITY
if (isContactForm) {
const contact = {
_type: "contact_submission", // must match the name of the contact document type on the Sanity schema
name: payload.data.name,
email: payload.data.email,
message: payload.data.message,
};

const result = await client
.create(contact)
.catch((err) => console.log(err));
}

callback(null, {
statusCode: 200,
});
};
1 change: 1 addition & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[build]
publish = ".next"
command = "./stackbit-build.sh"
functions = "functions/"

[[plugins]]
package = "@netlify/plugin-nextjs"
Loading