<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ishan Siddiqui</title>
    <description>The latest articles on DEV Community by Ishan Siddiqui (@siddykey).</description>
    <link>https://dev.to/siddykey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F801676%2Fbfb60f2c-06a9-49b5-9032-a80ba3532b89.jpeg</url>
      <title>DEV Community: Ishan Siddiqui</title>
      <link>https://dev.to/siddykey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siddykey"/>
    <language>en</language>
    <item>
      <title>Create an Events App using Next.js</title>
      <dc:creator>Ishan Siddiqui</dc:creator>
      <pubDate>Thu, 19 Jan 2023 19:28:30 +0000</pubDate>
      <link>https://dev.to/siddykey/create-an-events-app-using-nextjs-kdk</link>
      <guid>https://dev.to/siddykey/create-an-events-app-using-nextjs-kdk</guid>
      <description>&lt;p&gt;Creating an event app using Next.js can be a great way to build a fast, performant, and scalable web application. In this tutorial, we will go through the process of building an event app from scratch using Next.js, a popular framework for building server-rendered React applications.&lt;/p&gt;

&lt;p&gt;First, let's start by setting up our development environment. We will need Node.js and npm (or yarn) installed on our machine. Once we have that, we can create a new Next.js project by running the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-next-app my-events-app&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create a new directory called "my-events-app" with the basic structure of a Next.js app. We can then navigate into the new directory and start the development server by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-events-app
npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that our development environment is set up, we can start building our event app. We will be using a few external libraries to help us with some of the functionality, such as date handling and form validation. We can install these libraries by running the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install moment
npm install yup
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, let's create a new directory called "components" in the "src" directory. Inside this new directory, we will create a new file called "EventForm.js", which will contain the form for creating a new event. This form will include fields for the event name, location, date, and time. We will also include validation for these fields using the "yup" library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import { Formik, Form, Field } from 'formik'
import * as Yup from 'yup'

const EventForm = () =&amp;gt; {
  const validationSchema = Yup.object().shape({
    name: Yup.string().required('Event name is required'),
    location: Yup.string().required('Event location is required'),
    date: Yup.date().required('Event date is required'),
    time: Yup.string().required('Event time is required'),
  })

  return (
    &amp;lt;Formik
      initialValues={{
        name: '',
        location: '',
        date: '',
        time: '',
      }}
      validationSchema={validationSchema}
      onSubmit={(values, { setSubmitting }) =&amp;gt; {
        setTimeout(() =&amp;gt; {
          alert(JSON.stringify(values, null, 2))
          setSubmitting(false)
        }, 400)
      }}
    &amp;gt;
      {({ isSubmitting }) =&amp;gt; (
        &amp;lt;Form&amp;gt;
          &amp;lt;Field type="text" name="name" placeholder="Event name" /&amp;gt;
          &amp;lt;Field type="text" name="location" placeholder="Event location" /&amp;gt;
          &amp;lt;Field type="date" name="date" placeholder="Event date" /&amp;gt;
          &amp;lt;Field type="time" name="time" placeholder="Event time" /&amp;gt;
          &amp;lt;button type="submit" disabled={isSubmitting}&amp;gt;
            Create event
          &amp;lt;/button&amp;gt;
        &amp;lt;/Form&amp;gt;
      )}
    &amp;lt;/Formik&amp;gt;
  )
}

export default EventForm

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have our form set up, we can use it in our "pages/index.js" file to render it on the homepage of our app.&lt;/p&gt;

&lt;p&gt;We will also use the "moment" library to format the date and time fields in a more user-friendly format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import EventForm from '../components/EventForm'
import moment from 'moment'

const Home = () =&amp;gt; {
  const handleSubmit = values =&amp;gt; {
    const { name, location, date, time } = values
    const formattedDate = moment(date).format('MM/DD/YYYY')
    const formattedTime = moment(time, 'HH:mm').format('h:mm a')

    alert(`Event name: ${name}\nEvent location: ${location}\nEvent date: ${formattedDate}\nEvent time: ${formattedTime}`)
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;EventForm onSubmit={handleSubmit} /&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Home

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have a functional form for creating new events. However, we will also want to display a list of all the events that have been created. To do this, we will create a new component called "EventList" in our "components" directory. This component will take in an array of events and map over them to display each event's name, location, and date/time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import moment from 'moment'

const EventList = ({ events }) =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
      {events.map(event =&amp;gt; (
        &amp;lt;div key={event.id}&amp;gt;
          &amp;lt;h2&amp;gt;{event.name}&amp;lt;/h2&amp;gt;
          &amp;lt;p&amp;gt;Location: {event.location}&amp;lt;/p&amp;gt;
          &amp;lt;p&amp;gt;
            Date: {moment(event.date).format('MM/DD/YYYY')} Time:{' '}
            {moment(event.time, 'HH:mm').format('h:mm a')}
          &amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      ))}
    &amp;lt;/div&amp;gt;
  )
}

export default EventList

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then use this component in our "pages/index.js" file to display the list of events, and also pass in the new event that was just created to the list of events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react'
import EventForm from '../components/EventForm'
import EventList from '../components/EventList'

const Home = () =&amp;gt; {
  const [events, setEvents] = useState([])

  const handleSubmit = values =&amp;gt; {
    const { name, location, date, time } = values
    const formattedDate = moment(date).format('MM/DD/YYYY')
    const formattedTime = moment(time, 'HH:mm').format('h:mm a')

    const newEvent = {
      id: Date.now(),
      name,
      location,
      date: formattedDate,
      time: formattedTime
    }

    setEvents([...events, newEvent])
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;EventForm onSubmit={handleSubmit} /&amp;gt;
      &amp;lt;EventList events={events} /&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Home

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this, we now have a fully functional events app that allows users to create new events and view a list of all events&lt;/p&gt;

&lt;h2&gt;
  
  
  Styling our Events App
&lt;/h2&gt;

&lt;p&gt;Now that we have set up emotion, we can use the styled function to create CSS styles for our components. We can create a new directory called "styles" within our "components" directory, and add a new file called "EventForm.js" where we will define the styles for our EventForm component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from '@emotion/styled'

const FormContainer = styled.div`
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2rem;
`

const FormField = styled.div`
  display: flex;
  flex-direction: column;
  margin-bottom: 1rem;
`

const FormLabel = styled.label`
  font-size: 1.6rem;
  margin-bottom: 0.5rem;
`

const FormInput = styled.input`
  padding: 0.5rem;
  font-size: 1.6rem;
  border-radius: 0.5rem;
  border: none;
`

const FormButton = styled.button`
  background-color: #0070f3;
  color: #fff;
  padding: 1rem 2rem;
  font-size: 1.6rem;
  border-radius: 0.5rem;
  border: none;
  cursor: pointer;
  &amp;amp;:disabled {
    background-color: #aaa;
  }
`

export { FormContainer, FormField, FormLabel, FormInput, FormButton }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then import these styles in our EventForm component and apply them to the relevant elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react'
import { Formik, Form, Field } from 'formik'
import * as Yup from 'yup'
import { FormContainer, FormField, FormLabel, FormInput, FormButton } from '../styles/EventForm'

const EventForm = () =&amp;gt; {
  const validationSchema = Yup.object().shape({
    name: Yup.string().required('Event name is required'),
    location: Yup.string().required('Event location is required'),
    date: Yup.date().required('Event date is required'),
    time: Yup.string().required('Event time is required'),
  })

  return (
    &amp;lt;FormContainer&amp;gt;
      &amp;lt;Formik
        initialValues={{
          name: '',
          location: '',
          date: '',
          time: '',
        }}
        validationSchema={validationSchema}
        onSubmit={(values, { setSubmitting }) =&amp;gt; {
          setTimeout(() =&amp;gt; {
            alert(JSON.stringify(values, null, 2))
            setSubmitting(false)
          }, 400)
        }}
      &amp;gt;
        {({ isSubmitting }) =&amp;gt; (
          &amp;lt;Form&amp;gt;
            &amp;lt;FormField&amp;gt;
              &amp;lt;FormLabel htmlFor="name"&amp;gt;Event name&amp;lt;/FormLabel&amp;gt;
              &amp;lt;FormInput type="text" name="name" placeholder="Event name" /&amp;gt;
            &amp;lt;/FormField&amp;gt;
            &amp;lt;FormField&amp;gt;
              &amp;lt;FormLabel htmlFor="location"&amp;gt;Event location&amp;lt;/FormLabel&amp;gt;
              &amp;lt;FormInput type="text" name="location" placeholder="Event location" /&amp;gt;
            &amp;lt;/FormField&amp;gt;
            &amp;lt;FormField&amp;gt;
              &amp;lt;FormLabel htmlFor="date"&amp;gt;Event date&amp;lt;/FormLabel&amp;gt;
              &amp;lt;FormInput type="date" name="date" placeholder="Event date" /&amp;gt;
            &amp;lt;/FormField&amp;gt;
            &amp;lt;FormField&amp;gt;
              &amp;lt;FormLabel htmlFor="time"&amp;gt;Event time&amp;lt;/FormLabel&amp;gt;
              &amp;lt;FormInput type="time" name="time" placeholder="Event time" /&amp;gt;
            &amp;lt;/FormField&amp;gt;
            &amp;lt;FormButton type="submit" disabled={isSubmitting}&amp;gt;
              Create event
            &amp;lt;/FormButton&amp;gt;
          &amp;lt;/Form&amp;gt;
        )}
      &amp;lt;/Formik&amp;gt;
    &amp;lt;/FormContainer&amp;gt;
  )
}

export default EventForm

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way, we have a functional events app with a form for creating new events and displaying a list of all events, with styling using Emotion.&lt;br&gt;
It's also worth noting that this is just a simple example, in real-world scenarios you would use a state management library like redux or context-api to store the events and manage the state instead of using react useState hook. Additionally, you would also use an API to persist the data, either by using a backend or a cloud service like Firebase.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
