• Stars
    star
    178
  • Rank 214,989 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 11 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
58,625
star
2

50projects50days

50+ mini web projects using HTML, CSS & JS
CSS
36,264
star
3

vanillawebprojects

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

proshop_mern

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

devconnector_2.0

Social network for developers, built on the MERN stack
JavaScript
1,907
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,161
star
9

modern_js_udemy_projects

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

traversy-js-challenges

Challenges & docs from JS Algorithms & Data Structures course
JavaScript
903
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
710
star
13

modern_portfolio

Responsive portfolio website
SCSS
681
star
14

laragigs

Laravel job listing app
PHP
606
star
15

loruki-website

Cloud hosting website
HTML
595
star
16

mern_shopping_list

Shopping List built with MERN and Redux
TypeScript
594
star
17

lead_manager_react_django

Full stack app with React, Redux & Django
JavaScript
590
star
18

proshop-v2

ProShop ecommerce website built with MERN & Redux Toolkit
JavaScript
584
star
19

php-crash

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

storybooks

Node.js app with Google OAuth
JavaScript
561
star
21

feedback-app

React feedback app from React course
JavaScript
542
star
22

vue-crash-2021

Task Tracker project from youtube crash course
Vue
536
star
23

devcamper-api

Backend for devcamper app
HTML
516
star
24

btre_project

Django real estate website
CSS
507
star
25

tailwind-landing-page

CSS
500
star
26

javascript_cardio

JavaScript challenges and problems
JavaScript
490
star
27

expense-tracker-react

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

ui_components

Collection of HTML & CSS UI components
HTML
484
star
29

python_sandbox

Files to learn Python basics
Python
483
star
30

angular-crash-2021

Task tracker app from the 2021 youtube crash course
TypeScript
480
star
31

simple_react_pagination

Frontend pagination with React
JavaScript
479
star
32

vanilla-parcel-boilerplate

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

next-crash-course

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

tailwind-course-projects

Projects from my TailwindCSS course
CSS
425
star
35

redux_crash_course

Simple implementation of Redux with React 16
JavaScript
416
star
36

node_crash_course

Files for YouTube crash course
JavaScript
401
star
37

bootstrap-bootcamp-website

HTML
390
star
38

react_crash_todo

React crash course files
JavaScript
373
star
39

javascript-sandbox

Sandbox from the Modern JS From The Beginning 2.0 course
JavaScript
372
star
40

github-finder

Search Github users - React hooks & context
JavaScript
372
star
41

expense-tracker-mern

Full stack expense tracker
JavaScript
368
star
42

vanilla-node-rest-api

REST API using Node.js without a framework
JavaScript
368
star
43

rust_sandbox

Fundamentals of the Rust language
Rust
358
star
44

express_crash_course

Express crash course on YouTube
JavaScript
358
star
45

docker-node-mongo

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

vue_crash_todolist

Vue crash course application
Vue
351
star
47

project-mgmt-graphql

Full stack GraphQL, Express & React app
JavaScript
339
star
48

webpack-starter

Boilerplate for Webpack apps
JavaScript
337
star
49

github-finder-app

Find github users and display their info
JavaScript
336
star
50

react_redux_express_starter

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

lsapp

Laravel from scratch website/blog application
PHP
333
star
52

contact-keeper

Contact manager using React hooks & context
JavaScript
327
star
53

dj-events-frontend

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

vuex_todo_manager

Vuex crah course app
Vue
314
star
55

myflaskapp

Python Flask app with authentication
Python
312
star
56

house-marketplace

House marketplace built with React and FIrebase
JavaScript
311
star
57

hulu-webpage-clone

Hulu webpage clone
CSS
307
star
58

nodejs-openai-image

Web app that uses Node.js and OpenAI to generate images
CSS
305
star
59

nodekb

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

react_step_form

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

axios-crash

Axios crash course files
JavaScript
298
star
62

mern-auth

MERN authentication using JWT and HTTP-Only cookie
JavaScript
291
star
63

node-api-proxy-server

Proxy server to hide public API keys with rate limiting, caching
JavaScript
286
star
64

laravel-sanctum-api

REST API with auth using Laravel 8 and Sanctum
PHP
285
star
65

php_rest_myblog

PHP
281
star
66

tailwind-sandbox

Sandbox used with the Tailwind From Scratch course
HTML
280
star
67

react_webpack_starter

React 16 and Webpack 4 starter pack
TypeScript
277
star
68

next-markdown-blog

Simple blog using Next and Markdown
JavaScript
276
star
69

creative-agency-website

Agency website - basic HTML/CSS
HTML
275
star
70

react-crash-2024

React jobs project from YouTube crash course
HTML
271
star
71

react_file_uploader

React and Express file uploader
JavaScript
268
star
72

alexis_speech_assistant

Python speech assist app
Python
267
star
73

deno-rest-api

Simple REST API using Deno and Oak
TypeScript
266
star
74

go_crash_course

Basics of the go language
Go
264
star
75

url_shortener_service

API to shorten urls using Node, Express & MongoDB
JavaScript
250
star
76

breaking-bad-cast

App to show cast info for breaking bad
JavaScript
250
star
77

electron-course-files

Files and apps for Electron course
JavaScript
248
star
78

react_native_shopping_list

React Native app from crash course
JavaScript
244
star
79

simple-electron-react

Simple boilerplate for building Electron apps with React
JavaScript
243
star
80

bookstore

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

meanauthapp

Complete MEAN stack app with authentication
JavaScript
240
star
82

angular-crash-todolist

Simple app for Angular crash course
TypeScript
231
star
83

qr-code-generator

Simple Tool to Generate QR Codes
HTML
223
star
84

node_jwt_example

Example of Node JSON Web Tokens
JavaScript
221
star
85

electronshoppinglist

Shopping list desktop app built on electron
JavaScript
219
star
86

go_restapi

Simple RESTful API using Golang
Go
214
star
87

lyricfinder

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

svg-css-generator-examples

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

ciblog

Simple blog built on Codeigniter
PHP
202
star
90

larticles_api

Laravel 5.5 API using resources
PHP
193
star
91

codegig

Simple job find app for coders
CSS
191
star
92

node_push_notifications

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

bs4starter

Starter pack for Bootstrap 4 Beta
JavaScript
183
star
94

mongochat

Simple chat app using Mongo and websockets
HTML
178
star
95

nestjs_rest_api

Rest api built with Nest and MongoDB
TypeScript
173
star
96

next-13-crash-course

JavaScript
173
star
97

microposts_fullstack_vue

Full stack vue and express
JavaScript
172
star
98

flask_sqlalchemy_rest

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

pollster_django_crash

Polling app built with Django (Django Crash Course)
Python
170
star
100

huddle_styled_components

Landing page from styled components crash course
JavaScript
168
star