0

I currently have a GatsbyJS app that is using Netlify CMS (NCMS) for content management and Netlify via GitHub for hosting/deployment.

I would like to build a very simple order form system, but I would like to use JSON files and NCMS collections to represent the data because I will have very minimal data requirements and I would like the ability to have the data administered entirely through NCMS.

For example, I have a Products collection and an Orders collection. Products is using a JSON extension and contains JSON files that represent each product. Everything is looking good there. For Orders, I want to be able to create new entries from NCMS OR from an order form in my app.

Creating Orders entries in NCMS should be simple enough using the relation widget to incorporate a Products entry into each order. What I'm trying to figure out is how I can create JSON files in my filesystem (repo) upon form submission. I assume this is possible in some way with GitHub webhooks because NCMS is doing this when you create files in their system.

Can I, using some combination of Netlify's Forms API, Netlify Functions and/or GitHub webhooks, create a file in my GitHub repository when a user submits a Netlify Form in my app?

If this isn't possible, my follow up strategy would be to save Orders data in a MongoDB database. I do not have any experience yet with Mongo, but I know it's based around a "flat" data format using a JSON like syntax. Could I possibly save my order data in a Mongo collection upon submission via a Netlify Lambda function, then trigger a build via Netlify, and in my app pull in Mongo data at build time, and somehow create a file for each order object, perhaps via Gatsby's API?

My last resort would be to set up an admin area of my app where orders could be viewed, modified, and deleted. I'd prefer to avoid this because a) I do not have Netlify Pro services and thus do not have access to password protected site access (though I'm not sure how NCMS utilizes Identity and is able to avoid the premium plan?), and b) I would like all data administration to occur through the same platform (Netlify CMS).

halfer
  • 19,824
  • 17
  • 99
  • 186
Esten
  • 1,385
  • 2
  • 12
  • 21

1 Answers1

2

You can definitely do this with Netlify Form, Function & Github API. I've done something similar for one of my gatsby sites, where form data is submitted directly to a Netlify function.

If you'd like to take advantage of Netlify Form's features like spam prevention & email notification, I think it'll look something like this:

  • Set up your Netlify Form
  • Set up a Netlify function that listens to the submission-created event. In the body of the request, you'll find a payload object that contains the form data, as well as info about your site (in case you have a bunch of them). Netlify has a package that really ease up the development process.

  • Trigger a call to Github API to either create a file for that submission or update an existing file that contains all submissions.

  • At this point, you can set up netlifyCMS to read the content of that folder / file & allow admin to review / modify the submissions.

Hope it helps!

Derek Nguyen
  • 11,294
  • 1
  • 40
  • 64
  • Hey I have one quick question. I'm having a hard time finding the data that is coming from the form, and its hard to test, as I can't trigger the lambda event unless it's in prod (right??). Is the `payload` object you refer to inside of the `event` object? `context`? – Esten May 06 '19 at 20:09
  • hi @Esten! If you use the `netlify-lambda` package I mentioned, you can serve the function locally & config gatsby's proxy middleware to test them in development. IIRC the payload is a JSON string in `event` -- so `const { payload } = JSON.parse(event.body)`. Also, your function file name has to be the name of the event -- so something like `submission-created.js` – Derek Nguyen May 06 '19 at 20:28
  • Man, I'm totally losing it. It's one thing after the next. I am using netlify-lambda locally, I just wasn't sure if I would be able to trigger the submission event locally since I trying to get a hold of form data. However, I think my real problem is that my event.body is coming back base64 encoded. I've attempted to decode that, but it literally just decodes to "[object Object]" as in a string.. it's almost as if the payload supposed to be a JSON string, but it's converting a JS object to a string instead before anything gets sent over? Again, it's sending back "W29iamVjdCBPYmplY3Rd". – Esten May 07 '19 at 04:23
  • @Esten Hang on there, I'll look for an example repo or make one. Can you confirm the form submission function is triggered per form submit, and the data shows up correctly in the netlify console? – Derek Nguyen May 07 '19 at 04:52
  • Not sure if it's worth the effort actually - I think there are some Netlify bugs currently happening with Functions. I made a post here, hoping to make some headway https://community.netlify.com/t/function-logs-are-inconsistent-in-the-netlify-dashboard/656/6. Thanks for all your help with this! – Esten May 09 '19 at 04:30
  • @Esten I just implemented this in one of the sites I build & got it working -- would you DM me on twitter or discord? I can help you troubleshooting – Derek Nguyen May 10 '19 at 07:24
  • I am having a hard time figuring out how to DM you, lol. Maybe I'm just bad at twitter, but it's saying I cannot send you a message for some reason. Perhaps you could try to DM me? @thehosspatrick – Esten May 13 '19 at 17:59