• Stars
    star
    173
  • Rank 212,358 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Support ticket app built with the MERN stack

Support Desk App

Support ticket application built with the MERN stack. This is a project from my React Front To Back course.

Bug Fixes, corrections and code FAQ

This branch aims to fix bugs found by students of the Udemy course and answer common questions, it also features simpler state management than the course code. If you are looking to compare your code to that from the course lessons then please refer to the originalcoursecode branch of this repository.

There are detailed notes in the comments that will hopefully help you understand and adopt the changes and corrections. An easy way of seeing all the changes and fixes is to use a note highlighter extension such as This one for VSCode or this one for Vim Where by you can easily list all the NOTE: and FIX: tags in the comments.

BUG: Sending a 2xx response instead of a bad response

Currently in the course code there are cases where we throw an error in our API routes but we didn't set a response status code. So we end up sending a 2xx (good) response with an error message to our client when really this should be a bad response.

Our errorMiddleware should only send bad responses to our client. What we can do is check the current response status code and if it is less than 400 (therefore a good response) we can change it to a bad response status code.

Code changes can be seen in errorMiddleware.js

Q: Why are we querying the user in our noteController?

We already have our currently authenticated user in our req object from our authMiddleware and we can access this in our controllers with req.user so there is no need to again query the database for a user in these route handlers.

Code changes can be seen in noteController.js

BUG: App crashes on refresh in deployment

The path to the build fails to resolve when the browser is refreshed causing the app to crash.

Code changes to fix this can be seen in server.js

BUG: User is still authenticated if they don't exist in the database

Currently if no user was found in our authMiddlware, we still call next() bypassing any kind of route protection. If no user is found in our database then we need to send back a unauthorized response and not call next().

Code changes for this fix can be seen in authMiddleware.js

Q: Why are we using nested routes in App.js

There is no need for nested routing here as the route is the same as the parent, this can be removed and PrivateRoute can be changed to render it's children.

Code changes can be seen in App.js and PrivateRoute.js

Q: Why are we using a custom hook in our PrivateRoute?

Normally you would create a custom hook where you need to share some kind of logic in many different components. Since we only use our useAuthStatus hook in our PrivateRoute component it's actually simpler to just consume Redux state directly in our PrivateRoute without using a custom hook.

Code changes can be seen in PrivateRoute.js with the removal of useAuthStatus.js

Q: Why the repetitive code to get an error message?

We currently have the same logic to get the error message repeated in multiple AsyncThunks in our authSlice, ticketSlice and noteSlice. So this is a clear opportunity to dry up our code a little and extract this logic to it's own function.

Code changes can be seen in utils.js, authSlice.js, ticketSlice.js and noteSlice.js

Q: Why is logging out a user a AsyncThunk?

For logging out a user all we are doing is removing the user from local storage, we are not doing any async work or making any requests to our backend. You may even see warnings in your editor about using async await in synchronous code. We don't need to use an AsyncThunk here and can simply use a plain action.

Code changes can be seen in authSlice.js

BUG: PrivateRoute never shows a Spinner

It won't. When a user enters the app on a PrivateRoute such as /tickets then we never actually make any reqeust to the backend to authenticate them. We have a user in local storage and in Redux or we don't. If we do have a user then we trust the user is authenticated. This doesn't really take any time as all we are doing is getting the user from our Redux store. If we were making a request to the back end to authenticate the user then we could indeed make use of isLoading and a Spinner here in our PrivateRoute. Since it's not used it can be removed.

Code changes can be see in PrivateRoute.js

Simpler state management

Tickets and note state

Our ticket and note state in Redux can be simplified by removing isLoading, isSuccess, isError and message from state.

There is no real need for isLoading or isSuccess as this can be inferred from presence or absence of data - i.e. If we have tickets then we are not loading and we successfully got our tickets, if we don't have tickets then we are loading the tickets. We either have the tickets or we are loading the tickets. For more advice on if you should use loading booleans or not then this article from Kent C Dodds is a very informative read.

We also don't need a reset function as we can reset our state in our pending case that we get from using an AsyncThunk. Relying on state being explicitly reset with it's own action did cause some bugs for students with redirection and tickets not showing, so if that sounds like something you are experiencing then this is likely the fix you need. It was also a little complex to reason about if and when you needed to reset state from your components.

For our error and failures to fetch tickets or notes, by using an AsyncThunk we can unwrap the original Promise at component level. This also removes the need for a useEffect to monitor state and show an error message to the user if for whatever reason we got a bad response.

We can also unwrap our Promise on form submission when creating a ticket or note, which again removes the need for a useEffect to watch state. If we successfully create a ticket then we can redirect the user, if we failed to create a ticket then we can show the user the error message from our API. This follows the React advice for keeping side effects in event handlers where possible i.e. not using a useEffect if you don't have to.

Changes and detailed notes can be seen in ticketSlice.js, noteSlice.js, Tickets.jsx and Ticket.jsx

Auth state

Similar to ticket and note state, we don't really need an isError or message part to our state as again we can unwrap the Promise from our AsyncThunk at component level. This allows us to do something when the Promise is resolved - like redirecting the user after login and showing a success message with the users name, or do something else if the Promise was rejected - like showing a toast error message. Unwrapping our original Promise also removes the need in our Login and Register components for a useEffect to watch state as we can handle this all in our onSubmit event handlers. Since the only places we need isError or message was in Login and Register, this doesn't really need to be in Redux state.

Changes can be seen in authSlice.js, Login.jsx and Register.jsx

Usage

Set Environment Variables

Rename the .envexample to .env and add your MongoDB database URI and your JWT secret

Install backend dependencies

npm install

Install client dependencies

cd frontend
npm install

Run app in development (frontend & backend)

npm run dev

More Repositories

1

design-resources-for-developers

Curated list of design and UI resources from stock photos, web templates, CSS frameworks, UI libraries, tools and much more
56,219
star
2

50projects50days

50+ mini web projects using HTML, CSS & JS
CSS
33,285
star
3

vanillawebprojects

Mini projects built with HTML5, CSS & JavaScript. No frameworks or libraries
JavaScript
15,310
star
4

proshop_mern

Shopping cart built with MERN & Redux
JavaScript
1,979
star
5

devconnector_2.0

Social network for developers, built on the MERN stack
JavaScript
1,888
star
6

node_passport_login

Node.js login, registration and access control using Express and Passport
JavaScript
1,695
star
7

react-crash-2021

Task tracking application from the React crash course
JavaScript
1,359
star
8

chatcord

Realtime chat app with rooms
HTML
1,144
star
9

modern_js_udemy_projects

Project files for Modern JS From The Beginning course
JavaScript
1,126
star
10

traversy-js-challenges

Challenges & docs from JS Algorithms & Data Structures course
JavaScript
796
star
11

react_express_starter

Starter pack for React and Express full stack development
JavaScript
774
star
12

mern-tutorial

Goalsetter application from Youtube series
JavaScript
694
star
13

modern_portfolio

Responsive portfolio website
SCSS
681
star
14

mern_shopping_list

Shopping List built with MERN and Redux
TypeScript
594
star
15

lead_manager_react_django

Full stack app with React, Redux & Django
JavaScript
592
star
16

loruki-website

Cloud hosting website
HTML
592
star
17

laragigs

Laravel job listing app
PHP
578
star
18

storybooks

Node.js app with Google OAuth
JavaScript
548
star
19

php-crash

Code for my PHP crash course on YouTube
PHP
542
star
20

vue-crash-2021

Task Tracker project from youtube crash course
Vue
534
star
21

feedback-app

React feedback app from React course
JavaScript
522
star
22

devcamper-api

Backend for devcamper app
HTML
513
star
23

btre_project

Django real estate website
CSS
507
star
24

javascript_cardio

JavaScript challenges and problems
JavaScript
490
star
25

ui_components

Collection of HTML & CSS UI components
HTML
483
star
26

angular-crash-2021

Task tracker app from the 2021 youtube crash course
TypeScript
478
star
27

expense-tracker-react

Simple expense tracker using React hooks & context
JavaScript
478
star
28

simple_react_pagination

Frontend pagination with React
JavaScript
477
star
29

tailwind-landing-page

CSS
475
star
30

proshop-v2

ProShop ecommerce website built with MERN & Redux Toolkit
JavaScript
469
star
31

python_sandbox

Files to learn Python basics
Python
468
star
32

vanilla-parcel-boilerplate

Simple starter workflow for building vanilla js apps with Parcel
JavaScript
434
star
33

next-crash-course

Project from my Next.js crash course on YouTube
JavaScript
426
star
34

redux_crash_course

Simple implementation of Redux with React 16
JavaScript
416
star
35

node_crash_course

Files for YouTube crash course
JavaScript
397
star
36

github-finder

Search Github users - React hooks & context
JavaScript
377
star
37

bootstrap-bootcamp-website

HTML
377
star
38

react_crash_todo

React crash course files
JavaScript
371
star
39

tailwind-course-projects

Projects from my TailwindCSS course
CSS
367
star
40

vanilla-node-rest-api

REST API using Node.js without a framework
JavaScript
361
star
41

express_crash_course

Express crash course on YouTube
JavaScript
358
star
42

rust_sandbox

Fundamentals of the Rust language
Rust
357
star
43

docker-node-mongo

Sample node and mongo app that uses docker
JavaScript
357
star
44

expense-tracker-mern

Full stack expense tracker
JavaScript
356
star
45

vue_crash_todolist

Vue crash course application
Vue
351
star
46

react_redux_express_starter

Boiler plate for React/Redux applications with Express
JavaScript
336
star
47

github-finder-app

Find github users and display their info
JavaScript
336
star
48

lsapp

Laravel from scratch website/blog application
PHP
333
star
49

contact-keeper

Contact manager using React hooks & context
JavaScript
328
star
50

project-mgmt-graphql

Full stack GraphQL, Express & React app
JavaScript
325
star
51

dj-events-frontend

Next.js website to list DJ and other musical events
JavaScript
324
star
52

webpack-starter

Boilerplate for Webpack apps
JavaScript
317
star
53

vuex_todo_manager

Vuex crah course app
Vue
315
star
54

myflaskapp

Python Flask app with authentication
Python
312
star
55

hulu-webpage-clone

Hulu webpage clone
CSS
310
star
56

nodejs-openai-image

Web app that uses Node.js and OpenAI to generate images
CSS
303
star
57

nodekb

Simple knowledgebase app with Node.js, Express and MongoDB
JavaScript
303
star
58

react_step_form

Multi step form built with React and Material UI
JavaScript
302
star
59

javascript-sandbox

Sandbox from the Modern JS From The Beginning 2.0 course
JavaScript
301
star
60

house-marketplace

House marketplace built with React and FIrebase
JavaScript
297
star
61

axios-crash

Axios crash course files
JavaScript
292
star
62

laravel-sanctum-api

REST API with auth using Laravel 8 and Sanctum
PHP
282
star
63

php_rest_myblog

PHP
281
star
64

node-api-proxy-server

Proxy server to hide public API keys with rate limiting, caching
JavaScript
281
star
65

react_webpack_starter

React 16 and Webpack 4 starter pack
TypeScript
277
star
66

next-markdown-blog

Simple blog using Next and Markdown
JavaScript
276
star
67

alexis_speech_assistant

Python speech assist app
Python
270
star
68

deno-rest-api

Simple REST API using Deno and Oak
TypeScript
268
star
69

creative-agency-website

Agency website - basic HTML/CSS
HTML
266
star
70

go_crash_course

Basics of the go language
Go
264
star
71

react_file_uploader

React and Express file uploader
JavaScript
261
star
72

breaking-bad-cast

App to show cast info for breaking bad
JavaScript
251
star
73

url_shortener_service

API to shorten urls using Node, Express & MongoDB
JavaScript
247
star
74

react_native_shopping_list

React Native app from crash course
JavaScript
243
star
75

tailwind-sandbox

Sandbox used with the Tailwind From Scratch course
HTML
241
star
76

simple-electron-react

Simple boilerplate for building Electron apps with React
JavaScript
241
star
77

bookstore

Simple RESTful JSON api using Nodejs, Express and MongoDB
HTML
240
star
78

meanauthapp

Complete MEAN stack app with authentication
JavaScript
240
star
79

electron-course-files

Files and apps for Electron course
JavaScript
238
star
80

angular-crash-todolist

Simple app for Angular crash course
TypeScript
230
star
81

node_jwt_example

Example of Node JSON Web Tokens
JavaScript
221
star
82

mern-auth

MERN authentication using JWT and HTTP-Only cookie
JavaScript
219
star
83

electronshoppinglist

Shopping list desktop app built on electron
JavaScript
219
star
84

qr-code-generator

Simple Tool to Generate QR Codes
HTML
215
star
85

go_restapi

Simple RESTful API using Golang
Go
214
star
86

lyricfinder

App that finds song lyrics using React and the Musixmatch API
JavaScript
211
star
87

svg-css-generator-examples

Tiny projects using generated code from different CSS & SVG generators
HTML
204
star
88

ciblog

Simple blog built on Codeigniter
PHP
202
star
89

larticles_api

Laravel 5.5 API using resources
PHP
193
star
90

codegig

Simple job find app for coders
CSS
192
star
91

node_push_notifications

Example using Node.js and service workers to send and show push notifications
JavaScript
186
star
92

bs4starter

Starter pack for Bootstrap 4 Beta
JavaScript
183
star
93

mongochat

Simple chat app using Mongo and websockets
HTML
178
star
94

microposts_fullstack_vue

Full stack vue and express
JavaScript
174
star
95

nestjs_rest_api

Rest api built with Nest and MongoDB
TypeScript
172
star
96

flask_sqlalchemy_rest

Products REST API using Flask, SQL Alchemy & Marshmallow
Python
171
star
97

next-13-crash-course

JavaScript
170
star
98

pollster_django_crash

Polling app built with Django (Django Crash Course)
Python
169
star
99

face_recognition_examples

Examples for Python Face Recognition library
Python
166
star
100

huddle_styled_components

Landing page from styled components crash course
JavaScript
166
star