DEV Community

Cover image for Build A MERN Stack App in 5 Days (Day 4: Connecting to the backend)
rohanjsx
rohanjsx

Posted on

2 2

Build A MERN Stack App in 5 Days (Day 4: Connecting to the backend)

Hello and Welcome to Day 4!
Up to this point, we have setup our backend, designed the Sidebar, Header & Widgets components and implemented Login & Logout functionalities for our user with Redux & Firebase Google Auth.

Today, we will:

  • Design 'Modal' to Add Question
  • Make request to backend to add question to DB

Let's get started!

Add Question

Our Question has 3 fields: description, imageUrl (optional) and user. The user field will be accessible from our Redux global store once the user signs in. Then, we want to display a 'Modal' once the user clicks on the 'Add Question' button in the Header where the user can enter the description & imageUrl and submit the question. Let's go ahead and:

  • Install the dependencies we will use today
  • Add a proxy to our backend server in 'package.json' in client
  • Create states to keep track of input fields:

install

states

proxy

Note: You can use any library of your choice to create the modal or create a custom 'Modal' component yourself

Let's create the function to make the request to the backend to submit our question:

add

Design the Modal

On clicking the 'Add Question' button, we set the 'isModalOpen' state to true and display our modal:

set

Our final version of the Modal in the app looks like this:

mode

It can be divided into the following sections:

  • Top (containing the texts Add Question & Share Link)
  • Middle (containing Avatar and Dropdown)
  • Inputs & Image Preview
  • Buttons to Submit Question & Close Modal.

top

middle

inputs

buttons

Let's go ahead and test our Modal!
Run 'npm start' in the terminal from the 'client' directory.
The app should run on localhost:3000. Login and click on the 'Add Question' button and the Modal should pop up! Go ahead and Add a Question.

will

If we go and check our MongoDB database,we see that the Question has been added to our database successfully!

db add

With the 'Add Question' functionality out of the way, the only functionality we are missing is to 'Add Answer'.

In the next and final part of the series, we will:

  • Add functionality to Submit Answer
  • Design our Feed
  • Polish and wrap up our application!

Homework

  • Design your own custom Modal component
  • Add a custom alert to display 'Question Added' when you submit the question

See you in the final part!

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay