diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100755 index 0000000..dad73ad --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,40 @@ +module.exports = { + env: { + browser: true, + es6: true, + node: true + }, + extends: [ + 'eslint:recommended', + 'plugin:react/recommended' + ], + settings: { + react: { + pragma: 'React', + version: '^16.8.6' + } + }, + globals: { + Atomics: 'readonly', + SharedArrayBuffer: 'readonly' + }, + parserOptions: { + ecmaFeatures: { + jsx: true + }, + ecmaVersion: 2018, + sourceType: 'module' + }, + plugins: [ + 'react', + 'react-hooks' + ], + rules: { + 'no-console': 'off', + 'react/prop-types': 0, + 'no-unused-vars': 'off', + 'no-undef': 'off', + 'react-hooks/rules-of-hooks': 'error', // Checks rules of Hooks + 'react-hooks/exhaustive-deps': 'warn' // Checks effect dependencies + } +} diff --git a/gatsby-config.js b/gatsby-config.js index 999bd3d..8287fc9 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -1,9 +1,19 @@ +var proxy = require("http-proxy-middleware") + module.exports = { siteMetadata: { title: `Gatsby Default Starter`, description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`, author: `@gatsbyjs`, }, + developMiddleware: app => { + app.use( + "/api/", + proxy({ + target: "http://localhost:3000" + }) + ) + }, plugins: [ `gatsby-plugin-react-helmet`, { diff --git a/src/components/form.js b/src/components/form.js new file mode 100644 index 0000000..dd76193 --- /dev/null +++ b/src/components/form.js @@ -0,0 +1,67 @@ +import React, { useState } from "react" + +const Form = () => { + const [title, setTitle] = useState('') + const [excerpt, setExcerpt] = useState('') + const [author, setAuthor] = useState('') + + const handleSubmit = async e => { + e.preventDefault() + + try { + let payload = { + poem: title, + excerpt: excerpt, + author: author + } + + const response = await fetch('/api/save-poem/', { + method: "POST", + cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached + credentials: "omit", // include, *same-origin, omit + headers: { "Content-Type": "application/json" }, + redirect: "follow", // manual, *folslow, error + referrer: "client", // no-referrer, *client + body: JSON.stringify(payload), + }) + + const answer = await response.json() + + if (answer.success) { + alert(answer.message) + } + else { + alert(answer.errors[0].msg) + } + } + catch (err) { + alert('Error connecting to backend:', err) + } + } + + const handleTitleChange = e => { + setTitle(e.currentTarget.value) + } + + const handleExcerptChange = e => { + setExcerpt(e.currentTarget.value) + } + + const handleAuthorChange = e => { + setAuthor(e.currentTarget.value) + } + + return ( +
+
+ + + + + +
+
+ ) +} + +export default Form diff --git a/src/pages/index.js b/src/pages/index.js index 7eb6257..8204efb 100644 --- a/src/pages/index.js +++ b/src/pages/index.js @@ -4,6 +4,7 @@ import { Link } from "gatsby" import Layout from "../components/layout" import Image from "../components/image" import SEO from "../components/seo" +import Form from "../components/form" const IndexPage = () => ( @@ -14,6 +15,7 @@ const IndexPage = () => (
+
Go to page 2 )