GithubHelp home page GithubHelp logo

regis011 / code-crafter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from net-partner-011-ab/code-crafter

1.0 0.0 0.0 858 KB

Starter Kit :: NextJS and Bulma with connection to Contentful CMS and AWS SES contact form

JavaScript 98.48% SCSS 1.52%

code-crafter's Introduction

CodeCrafter

Starter Kit :: Next.js and Bulma with connection to Contentful CMS and AWS SES contact form

Demo link: https://net-partner-011-ab.github.io/code-crafter/

CodeCrafter performance

This example showcases Next.js's Static Generation feature using Contentful as the data source.

Starter kit contains 3 routes:

index.js - Landing page
components.js - Page with all created components
contact.js - Contact page containing a form

Instructions

Requirements:

Clone a project repository to your local computer and go to project folder

  git clone https://github.com/net-partner-011-ab/code-crafter.git your-project-name

  cd your-project-name

Setup

1. To connect Contentful CMS and AWS SES with the application (Required step)

Copy .env.local.example in the project root to .env.local and edit your preferences.

Example:

CONTENTFUL_SPACE_ID=...
CONTENTFUL_ACCESS_TOKEN=...
CONTENTFUL_PREVIEW_ACCESS_TOKEN=...
CONTENTFUL_PREVIEW_SECRET=...
CONTENTFUL_MANAGEMENT_TOKEN=...

NEXT_PUBLIC_SITE_URL=...

NEXT_PUBLIC_AWS_ACCESS_KEY_ID=...
NEXT_PUBLIC_AWS_SECRET_ACCESS_KEY=...
NEXT_PUBLIC_AWS_REGION=...

2. Local build

npm run build

3. Start local server

npm run dev

4. Open page in local browser

http://localhost:3000

Documentation

Next.js

Bulma CSS

Contentful CMS

AWS SES

Usage/Examples

In the documentation related to Contentful, you can find all the necessary information about creating accounts, creating keys and content models with fields.

Below are examples of how they can be connected to the application. In the code itself you can find all the examples shown.

Contentful

It is optionally, but If you want to import our Contentful space through Contentful CLI, follow the next steps:

1. Locally installed contentful-cli

Using Homebrew:

brew install contentful-cli

Using npm:

npm install -g contentful-cli

Using yarn:

yarn global add contentful-cli

2. Authenticated with contentful-cli

contentful login --management-token <management-token>

3. Connect to your Contentful space

contentful space use 

Then choose your space.

4. Importing content

Run following command:

contentful space import --content-file lib/config.json

After that, our content and content models will be imported to yours. You can find all the mentioned steps on the following link.

Importing and exporting content with the Contentful CLI

A query is written in lib/api.js along with a functions that connects the Contact page to the Contentful content model.

const CONTACT_PAGE_FIELDS = `
title
subtitle
iconsListCollection {
  items {
    link
    text
    image {
      title
      url
      width
      height
    }
  }
}
`;

async function fetchGraphQL(query, preview = false) {
  return fetch(
    `https://graphql.contentful.com/content/v1/spaces/${process.env.NEXT_PUBLIC_CONTENTFUL_SPACE_ID}`,
    {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${
          preview
            ? process.env.NEXT_PUBLIC_CONTENTFUL_PREVIEW_ACCESS_TOKEN
            : process.env.NEXT_PUBLIC_CONTENTFUL_ACCESS_TOKEN
        }`,
      },
      body: JSON.stringify({ query }),
    }
  ).then((response) => response.json());
}

function extractContactPageEntries(fetchResponse) {
  return fetchResponse?.data?.contactPageCollection?.items;
}

export async function getContactPage(preview) {
  const entries = await fetchGraphQL(
    `query {
      contactPageCollection(limit: 50, preview: ${
          preview ? "true" : "false"
        }) {
          items {
            ${CONTACT_PAGE_FIELDS}
          }
        }
      }`,
    preview
  );
  return extractContactPageEntries(entries);
}

Then the function is imported on the Contact page and the data is accessed using the getStaticProps asynchronous function. An example is below.

import { getContactPage } from "../lib/api";

import Hero from "../components/Hero";
import ContactForm from "../components/ContactForm";

export default function Contact({ preview, allData }) {
  const contactPage = allData[0];

  return (
    <>
      <Hero title={contactPage.title} subtitle={contactPage.subtitle} />
      <ContactForm items={contactPage.iconsListCollection.items} />
    </>
  );
}

export const getStaticProps = async ({ preview = false }) => {
  const allData = (await getContactPage(preview)) ?? [];
  return {
    props: { preview, allData },
  };
};

AWS SES

In order for the form to be connected, valid credentials must be entered in the .env.local file from the beginning, and the functionality is divided into two files.

1. awsConfig.js file

import AWS from 'aws-sdk';
// Add credentials to .env
AWS.config.update({
    accessKeyId: process.env.NEXT_PUBLIC_AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.NEXT_PUBLIC_AWS_SECRET_ACCESS_KEY,
    region: process.env.NEXT_PUBLIC_AWS_REGION,
});

const ses = new AWS.SES({ apiVersion: '2010-12-01' });

export { ses };

2. config/contact.js file

import { ses } from '../awsConfig';

export default async function sendEmail(req, res) {
    const { firstName, lastName, email, message } = req;

    const params = {
        Destination: {
            ToAddresses: ['[email protected]'],
        },
        Message: {
            Body: {
                Text: { Data: `Name: ${firstName} ${lastName}\nEmail: ${email}\nMessage: ${message}` },
            },
            Subject: { Data: 'New Contact Form Submission' },
        },
        Source: '[email protected]',
    };

    try {
        await ses.sendEmail(params).promise();
    } catch (error) {
        console.error('Error sending email:', error);
    }
} 

3. components/ContactForm.js file

Import sendEmail and create handleSubmit function.

In the ContactForm component, you can see how the handleSubmit function was performed.

Good to know

Global components such as Navbar and Footer have been inserted into the layout.js component and are visible on every new page we create.

Additional packages that we use

react-multi-carousel

next-sitemap

@contentful/rich-text-react-renderer

@contentful/rich-text-types

code-crafter's People

Contributors

slobodanbajic9 avatar belikenikola avatar regis011 avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.