Menu

FullStack Evening 5pm

FullStack Evening 5pm

Download HTML ZIP

Download HTML CSS

JS Scratchpad #3

// let age = 21;

// if (age > 18) {
//   console.log('Welcome to my app');
// }

// let num = 33;

// if (num % 2 === 0) {
//   console.log('Number is even');
// } else {
//   console.log('Number is odd');
// }

// let age = 17;

// if (age >= 65) {
//   console.log('Drink are free');
// } else if (age >= 21) {
//   console.log('You can enter and you can drink');
// } else if (age >= 18) {
//   console.log('You can enter but you cannot drink');
// } else {
//   console.log('Not allowed');
// }

// let password = 'helloworld@123';

// if (password.length > 6) {
//   if (password.indexOf(' ') !== -1) {
//     console.log('Passwords should not include spaces');
//   } else {
//     console.log('Valid password');
//   }
// } else {
//   console.log('Password is too short');
// }

// let loggedInUser = 'user';

// if (loggedInUser) {
//   console.log('Welcome to your dashboard');
// } else {
//   console.log('Redirect to login');
// }

// let age = 20;

// if (age >= 18 && age < 21) {
//   console.log('You can enter but you cannot drink');
// } else if (age >= 21 && age < 65) {
//   console.log('You can enter and you can drink');
// } else if (age >= 65) {
//   console.log('Drink are free');
// } else {
//   console.log('Not allowed');
// }

// let loggedInUser = undefined;

// if (!loggedInUser) {
//   console.log('Redirect to login');
// } else {
//   console.log('Welcome to your dashboard');
// }

// if (day === 0) {
//   console.log('Mon');
// } else if (day === 1) {
//   console.log('Tue');
// } else if (day === 2) {
//   console.log('Wed');
// } else {}

// let day = 2;

// switch (day) {
//   case 1:
//   case 2:
//   case 3:
//     console.log('Wed');
//     break;
//   case 4:
//     console.log('Thu');
//     break;
//   case 5:
//     console.log('Fri');
//     break;
//   case 6:
//     console.log('Sat');
//     break;
//   case 7:
//     console.log('Sun');
//     break;
//   default:
//     console.log('Invalid day code');
// }

// let status = 'offline';

// let color;
// if (status === 'offline') color = 'red';
// else color = 'green';

// let color = status === 'offline' ? 'red' : 'green';

// status === 'offline' ? console.log('RED') : console.log('GREEN');

// if (status === 'offline') {
//   console.log('RED');
// } else {
//   console.log('GREEN');
// }

let currentStatus = 'busy';

let color =
	currentStatus === 'offline'
		? 'red'
		: currentStatus === 'busy'
		? 'yellow'
		: 'green';

// let color;
// if (currentStatus === 'offline') {
//   color = 'red';
// } else if (currentStatus === 'busy') {
//   color = 'yellow';
// } else {
//   color = green;
// }

// console.log(color);

JS Scratchpad #4

// let product = ['iPhone 14 Pro', 'Apple', 'Some desc....', 200000, 500, true];

// let prod = {
// 	inStock: 500,
// 	name: 'iPhone 14 Pro',
// 	description: 'Some desc....',
// 	brand: 'Apple',
// 	discounted: true,
// 	price: 200000,
// 	'hello world': 123,
// 	1: 210,
// 	2: 32,
// };

// prod.price;

// const products = [
// 	{
// 		inStock: 500,
// 		name: 'iPhone 14 Pro',
// 		description: 'Some desc....',
// 		brand: 'Apple',
// 		discounted: true,
// 		price: 200000,
// 		'hello world': 123,
// 		1: 210,
// 		2: 32,
// 	},
// 	{
// 		inStock: 500,
// 		name: 'iPhone 14 Pro',
// 		description: 'Some desc....',
// 		brand: 'Apple',
// 		discounted: true,
// 		price: 200000,
// 		'hello world': 123,
// 		1: 210,
// 		2: 32,
// 	},
// 	{
// 		inStock: 500,
// 		name: 'iPhone 14 Pro',
// 		description: 'Some desc....',
// 		brand: 'Apple',
// 		discounted: true,
// 		price: 200000,
// 		'hello world': 123,
// 		1: 210,
// 		2: 32,
// 	},
// 	{
// 		inStock: 500,
// 		name: 'iPhone 14 Pro',
// 		description: 'Some desc....',
// 		brand: 'Apple',
// 		discounted: true,
// 		price: 200000,
// 		'hello world': 123,
// 		1: 210,
// 		2: 32,
// 	},
// ]

// let product = ['iPhone 14 Pro', 'Apple', 200000, true];

// // product = {0: 'iPhone 14 Pro', 1: 'Apple', 2: 200000, 3: true, length: 4}

// let name = "John";

// // let name = {0: 'J', 1: 'o', 2: 'h', 3: 'n', length: 4}

Scratchpad #5

// for (let i = 0; i < 100000000; i++) {
// 	// console.log(i ** 2, 'Hello World');
// 	// console.log(`${i} - Hello World`);
// 	console.log(`${i}*${i}=${i * i}`);
// }

// for (let i = 100; i >= -10; i--) {
// 	console.log(i);
// }

// const nums = [12, 34, 56, 34, 78, 54, 23, 12];

// for (let i = 0; i < nums.length; i++) {
// 	console.log(nums[i]);
// }

// const movies = [
// 	{ movieName: 'Inception', rating: 3.8 },
// 	{ movieName: 'Avengers', rating: 3.4 },
// 	{ movieName: 'Iron Man', rating: 3.9 },
// ];

// for (let i = 0; i < movies.length; i++) {
// 	const movie = movies[i];
// 	console.log(`${movie.movieName} has a rating of ${movie.rating}.`);
// }

// let word = 'hello world';

// for (let i = 0; i < word.length; i++) {
// 	console.log(word[i]);
// }

// let reversedWord = '';

// for (let i = word.length - 1; i >= 0; i--) {
// 	// console.log(word[i]);
// 	reversedWord += word[i];
// }

// console.log(reversedWord);

// for (let i = 0; i < 5; i++) {
// 	console.log('OUTER LOOP', i);

// 	for (let j = 0; j < 5; j++) {
// 		console.log('\tINNER LOOP', j);
// 	}
// }

// sum all the values
// sum all even numbers

const gameBoard = [
	[
		[4, 64, 8, 4, 32],
		[4, 64, 8, 4, 312, 12],
		[4, 64],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
	],
	[
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
	],
	[
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
	],
	[
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
	],
];

// let total = 0;

// for (let i = 0; i < gameBoard.length; i++) {
// 	// console.log(gameBoard[i]);
// 	for (let j = 0; j < gameBoard[i].length; j++) {
// 		// console.log(gameBoard[i][j]);
// 		total += gameBoard[i][j];
// 	}
// }

// console.log(total);

// for (let i = 0; i < 10; i++) {
// 	console.log(i);
// }

// let count = 0;

// while (count < 10) {
// 	console.log(count);
// 	count++;
// }

// let target = Math.floor(Math.random() * 10) + 1;
// let guess = Math.floor(Math.random() * 10) + 1;

// while (target !== guess) {
// 	console.log(`Guess: ${guess} | Target: ${target}`);
// 	guess = Math.floor(Math.random() * 10) + 1;
// }

// console.log(`Final Guess: ${guess} | Target: ${target}`);

// let target = Math.floor(Math.random() * 10) + 1;
// let guess = Math.floor(Math.random() * 10) + 1;

// while (true) {
// 	console.log(`Guess: ${guess} | Target: ${target}`);
// 	if (guess === target) {
// 		break;
// 	}
// 	guess = Math.floor(Math.random() * 10) + 1;
// }

// console.log(`Final Guess: ${guess} | Target: ${target}`);

// const nums = [12, 34, 56, 34, 78, 54, 23, 12];

// // for (let i = 0; i < nums.length; i++) {
// // 	console.log(nums[i]);
// // }

// // for (let num of nums) {
// // 	console.log(num);
// // }

// // for (let char of 'hello world') {
// // 	console.log(char);
// // }

// const matrix = [
// 	[1, 4, 7],
// 	[9, 7, 2],
// 	[9, 4, 6],
// ];

// for (let row of matrix) {
// 	for (let num of row) {
// 		console.log(num);
// 	}
// }

// const cats = ['fashion', 'mobiles', 'books'];
// const prods = ['tshirt', 'samsung', '1984'];

// for (let i = 0; i < cats.length; i++) {
// 	console.log(cats[i], prods[i]);
// }

const productPrices = {
	Apple: 80000,
	OnePlus: 50000,
	Samsung: 90000,
};

// for (let value of Object.values(productPrices)) {
// 	console.log(value);
// }

for (let key of Object.keys(productPrices)) {
	console.log(key, productPrices[key]);
}

for (let key in productPrices) {
	console.log(key, productPrices[key]);
}

Scratchpad #6

// function greet() {
// 	console.log('Hello my name is John');
// 	console.log('Hello my name is John');
// 	console.log('Hello my name is John');
// 	console.log('Hello my name is John');
// }

// greet();
// for (let i = 0; i < 50; i++) {
// 	greet();
// }

// function rollDie() {
// 	let roll = Math.floor(Math.random() * 6) + 1;
// 	console.log(roll);
// }

// function rollDice(times) {
// 	for (let i = 0; i < times; i++) {
// 		rollDie();
// 	}
// }

// rollDice(5);

// function greet(name, message) {
// 	console.log(`${message} ${name}`);
// }

// greet('John', 'Hello, How are you?, ');
// greet('Hi', 'Jack');
// greet('Jane', 'Hi', 'Hello', 'World');

// function add(a, b) {
// 	console.log(a + b);
// }

// add(10, 4);
// add(10);
// add();

// function add(a, b) {
// 	return a + b;
// }

// console.log(add(10, 5));

// function isString(val) {
// 	if (typeof val === 'string') {
// 		return true;
// 	}
// 	return false;
// }

// console.log(isString(100));
// console.log(isString('100'));

// let name = 'Jane Smith';

// function greet() {
// 	let name = 'John Doe';
// 	console.log(name);
// }

// console.log(name);

// greet();

// if (true) {
// 	var firstName = 'John';
// 	console.log(firstName);
// }

// console.log(firstName);

// for (var i = 0; i < 10; i++) {
// 	console.log(i);
// }

// console.log(i);

// function outer() {
// 	// let movieName = 'Prey';

// 	function inner() {
// 		let movieName = 'The gray man';
// 		console.log(movieName);
// 	}

// 	console.log(movieName);

// 	inner();
// }

// outer();

// function square(num) {
// 	return num * num;
// }

// const num = 10;

// const square = function (num) {
// 	return num * num;
// };

// console.log(square(2));

// function math(a, b, fn) {
// 	return fn(a, b);
// }

function sub(a, b) {
	return a - b;
}
// function div(a, b) {
// 	return a / b;
// }

// console.log(sub(10, 5));
// console.log(div(10, 5));
// console.log(math(10, 5, sub));
// console.log(math(10, 5, div));
// console.log(
// 	math(10, 3, function (a, b) {
// 		return a * b;
// 	})
// );

// const math = [
// 	function (a, b) {
// 		return a + b;
// 	},
// 	function (a, b) {
// 		return a - b;
// 	},
// 	function (a, b) {
// 		return a * b;
// 	},
// 	function (a, b) {
// 		return a / b;
// 	},
// 	function (num) {
// 		return num ** 2;
// 	},
// 	function (num) {
// 		return num ** 3;
// 	},
// 	sub,
// ];

// console.log(math[2](10, 3));
// console.log(math[math.length - 1](3, 3));

const math = {
	add: function (a, b) {
		return a + b;
	},
	sub: function (a, b) {
		return a - b;
	},
	mul: function (a, b) {
		return a * b;
	},
	div: function (a, b) {
		return a / b;
	},
};

console.log(math.add(10, 4));

Scratchpad #7

const gameBoard = [
	[
		[4, 64, 8, 4, 32],
		[4, 64, 8, 4, 312, 12],
		[4, 64],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
	],
	[
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
	],
	[
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
		[4, 64, 8, 4],
	],
];

let total = 0;

for (let i = 0; i < gameBoard.length; i++) {
	for (let j = 0; j < gameBoard[i].length; j++) {
		for (let k = 0; k < gameBoard[i][j].length; k++) {
			total += gameBoard[i][j][k];
		}
	}
}

console.log(total);

// function add(a, b) {
// 	return a + b;
// }

// function math(a, b, fn) {
// 	return fn(a, b);
// }

// console.log(
// 	math(10, 3, function (a, b) {
// 		return a - b;
// 	})
// );

// function repeat(func, num) {
// 	for (let i = 0; i < num; i++) {
// 		func();
// 	}
// }

// repeat(function () {
// 	console.log('Hello World');
// }, 20);

// function randomPick(f1, f2) {
// 	const random = Math.random();

// 	if (random < 0.5) {
// 		f1();
// 	} else {
// 		f2();
// 	}
// }

// randomPick(
// 	function () {
// 		console.log('Hello World');
// 	},
// 	function () {
// 		console.log('Hello Universe');
// 	}
// );

// function raiseBy(num) {
// 	return function (x) {
// 		return x ** num;
// 	};
// }

// const square = raiseBy(2);
// const cube = raiseBy(3);
// const hypercube = raiseBy(4);

// console.log(hypercube(5));

// function isBetween(x, y) {
// 	return function (num) {
// 		return num >= x && num <= y;
// 	};
// }

// const isUnderAge = isBetween(0, 18);
// const canEnterButNotDrink = isBetween(18, 21);
// const canDrink = isBetween(21, 65);
// const isSenior = isBetween(65, 100);

// console.log(canDrink(45));

// function math(a, b, fn) {
// 	return fn(a, b);
// }

// console.log(
// 	math(10, 3, function (a, b) {
// 		return a - b;
// 	})
// );

// setInterval(function () {
// 	console.log(Math.random());
// }, 100);

// add(10, 2);

// function add(a, b) {
// 	console.log(a + b);
// }

// console.log(num);

// let num = 10;

// const nums = [9, 2, 4, 6, 2, 3, 7, 6];

// // for (let num of nums) {
// // 	console.log(num);
// // }

// nums.forEach(function (num) {
// 	if (num % 2 === 0) {
// 		console.log(num);
// 	}
// });

// const movies = [
// 	{
// 		title: 'Avengers',
// 		rating: 4.1,
// 	},
// 	{
// 		title: 'Dr. Strange',
// 		rating: 3.9,
// 	},
// 	{
// 		title: 'Tenet',
// 		rating: 4.3,
// 	},
// 	{
// 		title: 'Joker',
// 		rating: 4.7,
// 	},
// ];

// movies.forEach(function (movie, i) {
// 	console.log(`${i} - ${movie.title} has a rating of ${movie.rating}`);
// // });

// const names = ['john', 'jack', 'jane', 'james'];

// // const upperNames = [];
// const upperNames = names.map(function (name) {
// 	return name.toUpperCase();
// });

// // for (let name of names) {
// // 	upperNames.push(name.toUpperCase());
// // }

// console.log(upperNames);

// const nums = [2, 3, 4, 7, 6, 8, 13, 10, 19, 12, 14, 22, 21, 16];

// const doubles = nums.map(function (num) {
// 	return {
// 		number: num,
// 		isEven: num % 2 === 0,
// 	};
// });

// console.log(doubles);

// function square(num) {
// 	return num * num;
// }

// const square = num => num * num;
// console.log(square(10));



const nums = [2, 3, 4, 7, 6, 8, 13, 10, 19, 12, 14, 22, 21, 16]

const res = nums.map(num => {
	return num % 2 === 0 ?  "Even" : "Odd"
	// if (num % 2 === 0) {
	// 	return "Even"
	// } else {
	// 	return "Odd"
	// }
})

console.log(res);

// const doubles = nums.map(function (num) {
//   return num * 2;
// })

// const doubles = nums.map(n => n * 2);
// console.log(doubles)

Scratchpad #8

// let movies = [
// 	'The Terminator',
// 	'The Avengers',
// 	'Jurassic Park',
// 	'Titanic',
// 	'National Park',
// ];

// let result = movies.find((movie) => {
// 	// return movie.includes('Park');
// 	return movie[0] === 'N';
// });

// console.log(result);

// const books = [
// 	{
// 		title: 'The Shining',
// 		author: 'Stephen King',
// 		rating: 4.1,
// 	},
// 	{
// 		title: 'Sacred Games',
// 		author: 'Vikram Chandra',
// 		rating: 4.5,
// 	},
// 	{
// 		title: '1984',
// 		author: 'George Orwell',
// 		rating: 4.9,
// 	},
// 	{
// 		title: 'The Alchemist',
// 		author: 'Paulo Coelho',
// 		rating: 3.5,
// 	},
// 	{
// 		title: 'The Great Gatsby',
// 		author: 'F. Scott Fitzgerald',
// 		rating: 3.8,
// 	},
// ];

// // const result = books.find((book) => {
// // 	// return book.author === 'George Orwell';
// // 	return book.rating > 4;
// // });

// const result = books.filter((book) => {
// 	// return book.author === 'George Orwell';
// 	return book.rating > 4;
// });

// console.log(result);

// const nums = [9, 8, 3, 4, 6, 12, 17, 23, 0];

// console.log(nums.filter((num) => num % 2 === 0));

// const goodBook = books.filter(b => b.rating >= 4.3)

// const georgeBooks = books.filter(b => (
//   b.author.includes('George')
// ))

// const lowRated = books.filter(book => {
//   return book.rating < 4
// })

// let query = 'the'
// const filteredBooks = books.filter(book => {
//   const title = book.title.toLowerCase()
//   return title.includes(query)
// })

// console.log(goodBook)
// console.log(georgeBooks)
// console.log(lowRated)
// console.log(booksWithThe)

// const names = ['jack', 'james', 'john', 'jane', 'josh', 'jrad'];

// console.log(names.every((name) => name[0] === 'j'));
// console.log(names.some((name) => name[0] !== 'j'));

// const prices = [211, 500.4, 23, 5, 4, 22.2, -23.2, 9233];

// // prices.sort((a, b) => a - b);
// prices.sort((a, b) => b - a);
// console.log(prices);

// const nums = [1, 2, 3, 4, 5];

// const result = nums.reduce((acc, currVal) => {
// 	return acc - currVal;
// });

// console.log(result);

// acc     currVal
// 221        2
// 221        1
// 221       34
// 221

// let nums = [21, 221, 2, 1, 34, 123, 4342, 56, 4];

// const result = nums.reduce((acc, currVal) => {
// 	if (currVal > acc) {
// 		return currVal;
// 	}
// 	return acc;
// });

// console.log(result);

// const nums = [1, 2, 3, 4, 5];

// const result = nums.reduce((acc, currVal) => {
// 	return acc + currVal;
// }, 1000);

// console.log(result);

// function multiply(x, y) {
// 	if (typeof y === 'undefined') {
// 		y = 1;
// 	}
// 	return x * y;
// }

// function multiply(x, y = 1) {
// 	return x * y;
// }

// console.log(multiply(10, 2));

// function printVals(a, b, c) {
// 	console.log(a);
// 	console.log(b);
// 	console.log(c);
// }

// const data = ['John', 'Jane', 'Jack'];

// // printVals(data[0], data[1], data[2]);
// // printVals(...data);
// printVals(...'hello');

// const nums = [21, 23, 4345346, 12, 1];

// // console.log(Math.max(nums[0], nums[1], nums[2]));
// console.log(Math.max(...nums));

// function add(a, b) {
// 	return a + b;
// }

// function add(...nums) {
// 	let total = 0;
// 	for (let num of nums) {
// 		total += num;
// 	}
// 	console.log(total);
// }

// add(10, 3, 2, 3, 44, 2, 2, 2, 1, 23, 33, 12, 2);

// function printNames(name1, name2, ...others) {
// 	console.log(name1);
// 	console.log(name2);
// 	console.log(others);
// }

// printNames('john', 'jane', 'jack', 'jill', 'james');

// const users = ['john', 'jane', 'jack'];

// // const sub = users[0];
// // const admin = users[1];
// // const mod = users[2];

// const [sub, ...others] = users;

// console.log(sub, others);

const user = {
	firstName: 'John',
	lastName: 'Doe',
	email: 'john.doe@gmail.com',
	phone: 99982234567,
};

// const firstName = user.firstName;
// const lastName = user.lastName;

// const { firstName, lastName, email: emailAddress, phone } = user;
const { firstName, lastName, ...others } = user;

console.log(firstName, lastName, others);

Scratchpad #9

// // function profile({ firstName, lastName, age }) {
// // 	console.log(`My name is ${firstName} ${lastName} and I am ${age} years old`);
// // }

// // profile({ firstName: 'John', lastName: 'Doe', age: 20 });

// const movieReviews = [4.5, 5.0, 3.2, 2.1, 4.7, 3.8, 3.1, 3.9, 4.4];
// const highest = Math.max(...movieReviews);
// const lowest = Math.min(...movieReviews);
// let total = 0;
// movieReviews.forEach((rating) => (total += rating));
// const average = total / movieReviews.length;

// // const info = { highest: highest, lowest: lowest, average: average };
// const info = { highest, lowest, average };

// console.log(info);

// const getReviewDetails = (arr) => {
//   const highest = Math.max(...arr)
//   const lowest = Math.min(...arr)
//   const total = arr.reduce((accumulator, nextVal) => accumulator + nextVal)
//   const average = total / arr.length

//   return {
//     highest,
//     lowest,
//     total,
//     average
//   }
// }

// const reviewList = [4.5, 5.0, 3.2, 2.1, 4.7, 3.8, 3.1, 3.9, 4.4]

// const statistics = getReviewDetails(reviewList)

// const username = 'janedoe';
// const role = 'admin';

// // const user1 = { [role]: username };
// // const user1 = { [role.toUpperCase()]: username };
// const user1 = { [1 + 2 + 3 + 4]: username };

// console.log(user1);

// const addProperty = (obj, k, v) => {
// 	return { ...obj, [k]: v };
// };

// const user = { firstName: 'John', lastName: 'Doe' };
// const result = addProperty(user, 'age', 20);

// console.log(result);

// function square(x) {
// 	return x * x;
// }

// const math = {
// 	PI: 3.1415,
// 	square,
// 	multiply(x, y) {
// 		return x * y;
// 	},
// 	divide(x, y) {
// 		return x / y;
// 	},
// 	subtract(x) {
// 		return x - x;
// 	},
// };

// console.log(math);

// console.log('This is JavaScript');

// SCOPE

function helloWorldFromJavaScript() {
	console.log('Hello World');
}

// console.log(this);

// window.helloWorldFromJavaScript();

function hello() {
	console.log(this);
}

// const profile = {
// 	firstName: 'John',
// 	lastName: 'Doe',
// 	age: 20,
// 	greet() {
// 		console.log(this.firstName);
// 	},
// };

// hello();
// profile.greet();

// let aThisIsAnExample = 'Hello World';
// hello();

// function greet() {
// 	console.log(
// 		`Hello, my name is ${this.fullName()} and I am ${this.age} years old`
// 	);
// }

// // greet();

// const user1 = {
// 	firstName: 'John',
// 	lastName: 'Doe',
// 	age: 20,
// 	fullName() {
// 		return `${this.firstName} ${this.lastName}`;
// 	},
// 	greet,
// };

// const user2 = {
// 	firstName: 'Jane',
// 	lastName: 'Doe',
// 	age: 22,
// 	fullName() {
// 		return `${this.firstName} ${this.lastName}`;
// 	},
// 	greet,
// };

// user1.greet();

// console.log(this);

// const user2 = {
// 	firstName: 'Jane',
// 	lastName: 'Doe',
// 	age: 22,
// 	fullName() {
// 		// console.log(this);
// 		console.log(`${this.firstName} ${this.lastName}`);
// 	},
// };

// // user2.fullName();
// const outFullName = user2.fullName;

// outFullName();

// setInterval(function () {
// 	console.log('Hello World');
// }, 1000);

function greet(obj) {
	console.log(obj);
}

const hellos = {
	messages: [
		'hello world',
		'hello universe',
		'hello darkness',
		'hello hello',
		'heylo',
	],
	pickMsg() {
		const index = Math.floor(Math.random() * this.messages.length);
		return this.messages[index];
	},
	start() {
		setInterval(() => {
			console.log(this.pickMsg());
		}, 1000);
	},
	hello() {
		greet(this);
	},
};

// console.log(hellos.pickMsg());
// hellos.start();

hellos.hello();

Scratchpad 10

// // // console.log('Hello World');

// // // console.dir(document.all);

// // const impLi = document.getElementById('imp');

// // // console.dir(impLi);

// // const lis = document.getElementsByTagName('li');
// // console.log(lis);

// // for (let li of lis) {
// // 	console.log(li);
// // }

// // const elements = document.getElementsByClassName('special');

// // console.log(elements);

// // const list = document.getElementsByClassName('list');
// // const list1 = list[0];

// // const lis = list1.getElementsByTagName('li');
// // console.log(lis);

// // const el = document.getElementById('imp');
// // const bolds = el.getElementsByTagName('strong');
// // console.log(bolds);

// // const el = document.querySelector('#imp');
// const el = document.querySelectorAll('.para');
// console.log(el);

// const title = document.querySelector('h1');

// console.log(title.innerText);

// title.innerText = 'Hello World';

// setTimeout(function () {
// 	title.innerText = 'This got changed';
// }, 2000);

// setInterval(function () {
// 	title.innerText = Math.random();
// }, 10);

// const spc = document.querySelector('.special');

// console.log(spc.textContent);

// const title = document.querySelector('h1');

// // title.innerHTML = 'Hello <i>World</i>';
// // title.innerHTML += '. <u>Again.</u>';

// title.innerHTML = 'Hello <i>World</i>';
// title.innerHTML += '. <u>Again.</u>';

const mylink = document.querySelector('#mylink');
const myinp = document.querySelector('#myinp');
// console.log(mylink);

// mylink.innerText = 'Yahoo';
// mylink.href = 'https://yahoo.com';
// mylink.className = 'hello-world';
// myinp.value = '';
// myinp.placeholder = 'Enter full name properly';

// console.log(mylink.getAttribute('src'));
// console.log(mylink.src);

// mylink.setAttribute('href', 'https://yahoo.com');

// const para = document.querySelector('.special');
// // console.log(para.parentElement.parentElement.parentElement);
// // console.log(para.parentElement.nextElementSibling.nextElementSibling);
// console.log(para.parentElement.previousElementSibling);

// // const ul = document.querySelector('ul');
// // console.log(ul.children);

// const lis = document.querySelectorAll('li');

// for (let li of lis) {
// 	li.innerText = 'Hello World';

// 	// setInterval(function () {
// 	// 	li.innerText = Math.random();
// 	// }, 10);
// }

// const heading = document.querySelector('h1');

// // console.log(heading.style);

// heading.style.color = 'red';
// heading.style.backgroundColor = 'yellow';
// heading.style.textDecoration = 'underline';

// const lis = document.querySelectorAll('li');
// const colors = ['red', 'yellow', 'green', 'orange', 'teal'];

// for (let li of lis) {
// 	li.style.color = colors[Math.floor(Math.random() * colors.length)];
// 	li.style.fontSize = '20';
// 	li.style.fontWeight = 'bold';
// 	li.style.backgroundColor = 'black';
// 	li.style.fontFamily = 'Helvetica, sans-serif';
// }

// const el = document.querySelector('.special');
// // console.log(el.style.color);

// const elStyles = getComputedStyle(el);
// console.log(elStyles.color);

const task = document.querySelector('.item');
// task.className = 'item done';

// task.setAttribute('class', 'item done');

// console.log(task.classList);

task.classList.toggle('done');

Scratchpad #11

// const root = document.querySelector('#root');

// const title = document.createElement('h1');
// title.innerText = 'Hello World';
// title.style.color = 'red';

// const section = document.createElement('section');
// section.style.border = '5px solid red';
// section.appendChild(title);

// //<section style="border: 5px solid red;">
// //	<h1 style="color: red">Hello World</h1>
// //</section>

// root.appendChild(section);

// const todos = document.querySelector('#todos');

// const task1 = document.createElement('li');
// task1.innerText = 'This is a new task';

// const firstTask = document.querySelector('li');

// // todos.appendChild(task1);
// todos.insertBefore(task1, firstTask);

// const p = document.querySelector('p');

// const b = document.createElement('b');
// b.innerText = 'New Content';

// // p.insertAdjacentElement('beforebegin', b);
// // p.insertAdjacentElement('afterbegin', b);
// // p.insertAdjacentElement('beforeend', b);
// p.insertAdjacentElement('afterend', b);

// const b = document.createElement('b');
// b.innerText = 'HELLO WORLD';

// const i = document.createElement('i');
// i.innerText = 'GOODBYE GALAXY';

// const p = document.querySelector('p');

// // p.append(b, i);
// p.prepend(b);

// const todos = document.querySelector('#todos');

// const firstTask = todos.querySelector('li');

// // todos.removeChild(firstTask);

// firstTask.remove();

// const btn = document.querySelector('button');
// const inp = document.querySelector('input');

// btn.onclick = function () {
// 	console.log('Hello World');
// };

// btn.onclick = function () {
// 	console.log('Hello Universe');
// };

// btn.addEventListener('click', function () {
// 	console.log('Hello World');
// });
// btn.addEventListener('mouseout', function () {
// 	console.log('Hello Universe');
// });

// const btn = document.querySelector('button');
// const inp = document.querySelector('input');
// const list = document.querySelector('ul');

// btn.addEventListener('click', function () {
// 	const li = document.createElement('li');
// 	li.innerText = inp.value;
// 	li.addEventListener('click', function () {
// 		li.remove();
// 	});

// 	list.append(li);
// 	inp.value = '';
// });

// window.addEventListener('scroll', function () {
// 	console.log("I'm scrolling");
// 	this.document.body.style.backgroundColor = 'red';
// });

// const btn = document.querySelector('button');
// btn.style.position = 'relative';

// btn.addEventListener('mouseover', function () {
// 	const width = Math.floor(Math.random() * window.innerWidth) - 22;
// 	const height = Math.floor(Math.random() * window.innerHeight) - 22;
// 	btn.style.left = `${width}px`;
// 	btn.style.top = `${height}px`;
// });

// btn.addEventListener('click', function () {
// 	btn.innerText = 'You Won!';
// 	document.body.style.backgroundColor = 'green';
// });

// const colors = [
// 	'red',
// 	'orange',
// 	'yellow',
// 	'green',
// 	'blue',
// 	'purple',
// 	'indigo',
// 	'violet',
// ];

// const text = document.querySelector('span');
// const container = document.querySelector('#root');
// container.style.display = 'flex';

// function printColor() {
// 	text.innerText = this.style.backgroundColor;
// }

// for (let color of colors) {
// 	const div = document.createElement('div');
// 	div.style.height = '100px';
// 	div.style.width = '100px';
// 	div.style.backgroundColor = color;

// 	div.addEventListener('click', printColor);

// 	container.append(div);
// }

// const colors = [
// 	'red',
// 	'orange',
// 	'yellow',
// 	'green',
// 	'blue',
// 	'purple',
// 	'indigo',
// 	'violet',
// ];

// const text = document.querySelector('span');
// const container = document.querySelector('#root');
// container.style.display = 'flex';

// for (let color of colors) {
// 	const div = document.createElement('div');
// 	div.style.height = '100px';
// 	div.style.width = '100px';
// 	div.style.backgroundColor = color;

// 	div.addEventListener('click', function (event) {
// 		console.log(event);
// 	});

// 	container.append(div);
// }

// const inp = document.querySelector('input');
// const p = document.querySelector('p');

// inp.addEventListener('keypress', function (event) {
// 	p.innerText = event.target.value;
// });

const form = document.querySelector('form');
const taskList = document.querySelector('ul');

form.addEventListener('submit', function (e) {
	e.preventDefault();
	if (!e.target.children.task.value) {
		return;
	}

	const task = document.createElement('li');
	task.innerText = e.target.children.task.value;
	task.addEventListener('click', function () {
		task.classList.toggle('done');
	});

	const delBtn = document.createElement('button');
	delBtn.innerText = 'Delete Task';
	delBtn.addEventListener('click', function () {
		task.remove();
	});

	task.append(delBtn);

	taskList.append(task);
	e.target.children.task.value = '';
});

Scratchpad #12

// // const multiply = (x, y) => x * y;

// // const square = (x) => multiply(x, x);

// // const rightTriangle = (a, b, c) => {
// // 	return square(a) + square(b) === square(c);
// // };

// // rightTriangle(3, 4, 5);

// // console.log('The first log');
// // alert('An interruption in between');
// // console.log('The last log');

// // console.log('The first log');
// // setTimeout(function () {
// // 	console.log('This callback runs later');
// // }, 4000);
// // console.log('The last log');

// const btn = document.querySelector('button');

// setTimeout(function () {
// 	btn.style.transform = `translateX(100px)`;
// 	setTimeout(function () {
// 		btn.style.transform = `translateX(200px)`;
// 		setTimeout(function () {
// 			btn.style.transform = `translateX(300px)`;
// 			setTimeout(function () {
// 				btn.style.transform = `translateX(400px)`;
// 				setTimeout(function () {
// 					btn.style.transform = `translateX(500px)`;
// 					setTimeout(function () {
// 						btn.style.transform = `translateX(600px)`;
// 						setTimeout(function () {
// 							btn.style.transform = `translateX(700px)`;
// 						}, 1000);
// 					}, 1000);
// 				}, 1000);
// 			}, 1000);
// 		}, 1000);
// 	}, 1000);
// }, 1000);

// const willGetAPlayStation = new Promise((resolve, reject) => {
// 	setTimeout(function () {
// 		const random = Math.random();

// 		if (random > 0.5) {
// 			resolve();
// 		} else {
// 			reject();
// 		}
// 	}, 500);
// });

// willGetAPlayStation
// 	.then(() => {
// 		console.log('I got the PS. Thank you parents.');
// 	})
// 	.catch(() => {
// 		console.log('Screw this!!');
// 	});

// // console.log(willGetAPlayStation);

// const willGetAPlayStation = () => {
// 	return new Promise((resolve, reject) => {
// 		setTimeout(function () {
// 			const random = Math.random();

// 			if (random > 0.5) {
// 				resolve({ hello: 'world' });
// 			} else {
// 				reject('goodbye world');
// 			}
// 		}, 500);
// 	});
// };

// willGetAPlayStation()
// 	.then((data) => {
// 		console.log(data);
// 		// console.log('I got the PS. Thank you parents.');
// 	})
// 	.catch((error) => {
// 		console.log(error);
// 		// console.log('Screw this!!');
// 	});

const fakeRequest = (url) => {
	return new Promise((resolve, reject) => {
		setTimeout(() => {
			const pages = {
				'/users': [
					{ id: 1, username: 'john' },
					{ id: 2, username: 'jane' },
				],
				'/users/1': {
					id: 1,
					username: 'johndoe',
					topPostId: 53231,
					city: 'mumbai',
				},
				'/users/5': {
					id: 1,
					username: 'janedoe',
					topPostId: 32443,
					city: 'pune',
				},
				'/posts/53231': {
					id: 1,
					title: 'Really amazing post',
					slug: 'really-amazing-post',
				},
			};

			const data = pages[url];

			if (data) {
				resolve({ status: 200, data });
			} else {
				reject({ status: 404 });
			}
		}, 1000);
	});
};

fakeRequest('/users')
	.then((response) => {
		const userId = response.data[0].id;
		return fakeRequest(`/users/${userId}`);
	})
	.then((response) => {
		const postId = response.data.topPostId;
		return fakeRequest(`/posts/${postId}`);
	})
	.then((response) => {
		console.log(response);
	})
	.catch((err) => {
		console.log(err);
	});

Scratchpad #13

/* <products>
	<product>
		<name>Product XYZ</name>
		<price>10.99</price>
		<description>Some description</description>
		<inStock>100</inStock>
	</product>
	<product>
		<name>Product ABC</name>
		<price>10.99</price>
		<description>Some description</description>
		<inStock>100</inStock>
	</product>
</products>

// JSON

[
	{
		"name": "Product XYZ",
		"price": 10.99,
		"description": "Some Description",
		"inStock": 100
	},
	{
		"name": "Product XYZ",
		"price": 10.99,
		"description": "Some Description",
		"inStock": 100
	}
] */

// const request = fetch('https://swapi.dev/api/people', {
// 	headers: { Accept: 'application/json' },
// });

// request
// 	.then((response) => {
// 		if (!response.ok) {
// 			throw new Error('Something went wrong');
// 		}
// 		return response.json();
// 	})
// 	.then((data) => {
// 		console.log(data);
// 	})
// 	.catch((error) => {
// 		console.log('I CAME INTO THE CATCH BLOCK');
// 		console.log(error);
// 	});

// const request = axios.get('https://swapi.dev/api/people');

// request
// 	.then((res) => {
// 		const mov = res.data.results[0].films[0];
// 		return axios.get(mov);
// 	})
// 	.then((res) => {
// 		console.log(res.data);
// 	})
// 	.catch((err) => {
// 		console.log('I am in the catch block');
// 		console.log(err);
// 	});

const root = document.querySelector('#root');
root.style.display = 'grid';
root.style.gridTemplateColumns = '1fr 1fr 1fr 1fr';
root.style.gap = '30px';

const request = axios.get('https://swapi.dev/api/people');

request
	.then(({ data }) => {
		for (let person of data.results) {
			const p = document.createElement('p');
			p.innerHTML = `Name: ${person.name}<br />Height: ${person.height}`;
			root.append(p);
		}
	})
	.catch((err) => {
		console.log('I am in the catch block');
		console.log(err);
	});

Scratchpad 14

// // // // const response = axios.get('https://pokeapi.co/api/v2/pokemon');

// // // // response.then((data) => {
// // // // 	console.log(data);
// // // // });

// // // // function getData() {
// // // // 	const request = axios.get('https://pokeapi.co/api/v2/pokemon');
// // // // 	request
// // // // 		.then((response) => {
// // // // 			console.log(response.data);
// // // // 		})
// // // // 		.catch((error) => {
// // // // 			console.error(error);
// // // // 		});
// // // // }

// // // // getData();

// // // // async function greet() {
// // // // 	throw { hello: 'world' };
// // // // }

// // // function getPS() {
// // // 	return new Promise((resolve, reject) => {
// // // 		const num = Math.random();

// // // 		if (num > 0.5) {
// // // 			resolve('Your got the PS');
// // // 		} else {
// // // 			reject('Nothing');
// // // 		}
// // // 	});
// // // }

// // // async function getPS5() {
// // // 	const num = Math.random();

// // // 	if (num > 0.5) {
// // // 		return 'Your got the PS';
// // // 	} else {
// // // 		throw new Error('Nothing');
// // // 	}
// // // }

// // // getPS5()
// // // 	.then((response) => {
// // // 		console.log(response);
// // // 	})
// // // 	.catch((error) => {
// // // 		console.log('I am in the catch block', error);
// // // 	});

// // // async function getData() {
// // // 	const request = axios.get('https://pokeapi.co/api/v2/pokemon');
// // // 	request
// // // 		.then(({ data }) => {
// // // 			return axios.get(
// // // 				`https://pokeapi.co/api/v2/pokemon/${data.results[0].name}`
// // // 			);
// // // 		})
// // // 		.then(({ data }) => {
// // // 			console.log(data);
// // // 		})
// // // 		.catch((err) => {
// // // 			console.log(err);
// // // 		});
// // // }

// // // getData();

// // // async function getData1() {
// // // 	const request1 = await axios.get('https://pokeapi.co/api/v2/pokemon');
// // // 	const request2 = await axios.get(
// // // 		`https://pokeapi.co/api/v2/pokemon/${request1.data.results[0].name}`
// // // 	);
// // // 	console.log(request2.data);
// // // }

// // // getData1();

// // // async function getData1() {
// // // 	const request = await axios.get('https://pokeapi.co/api/v2/pok23emon');
// // // 	console.log(request.data);
// // // }

// // // getData1().catch((err) => {
// // // 	console.log(err);
// // // });

// // // async function getData1() {
// // // 	try {
// // // 		const request = await axios.get('https://pokeapi.co/api/v2/pok23emon');
// // // 		console.log(request.data);
// // // 	} catch (err) {
// // // 		console.log(err.message);
// // // 	}
// // // }

// // // getData1();

// // async function getPokemon() {
// // 	const pokemon1Req = axios.get('https://pokeapi.co/api/v2/pokemon/1');
// // 	const pokemon2Req = axios.get('https://pokeapi.co/api/v2/pokemon/2');
// // 	const pokemon3Req = axios.get('https://pokeapi.co/api/v2/pokemon/3');

// // 	const results = await Promise.all([pokemon1Req, pokemon2Req, pokemon3Req]);
// // 	console.log(results);

// // 	// const pokemon1 = await pokemon1Req;
// // 	// const pokemon2 = await pokemon2Req;
// // 	// const pokemon3 = await pokemon3Req;

// // 	// console.log(pokemon1.data.name);
// // 	// console.log(pokemon2.data.name);
// // 	// console.log(pokemon3.data.name);
// // }

// // getPokemon();

// // function initializeDeck() {
// // 	const deck = [];
// // 	const suits = ['hearts', 'diamonds', 'spades', 'clubs'];
// // 	const values = '2,3,4,5,6,7,8,9,10,J,Q,K,A';
// // 	for (let value of values.split(',')) {
// // 		for (let suit of suits) {
// // 			deck.push({ value, suit });
// // 		}
// // 	}
// // 	return deck;
// // }

// // function drawCard(deck, drawnCards) {
// // 	const card = deck.pop();
// // 	drawnCards.push(card);
// // 	return card;
// // }

// // function drawMultiple(numCards, deck, drawnCards) {
// // 	const cards = [];
// // 	for (let i = 0; i < numCards; i++) {
// // 		cards.push(drawCard(deck, drawnCards));
// // 	}
// // 	return cards;
// // }

// // function shuffle(deck) {
// // 	// loop over array backwards
// // 	for (let i = deck.length - 1; i > 0; i--) {
// // 		// pick random index before current element
// // 		let j = Math.floor(Math.random() * (i + 1));
// // 		[deck[i], deck[j]] = [deck[j], deck[i]];
// // 	}
// // 	return deck;
// // }

// // const deck1 = initializeDeck();
// // const hand = [];

// // shuffle(deck1);
// // console.log(deck1);

// // // drawCard(deck1, hand);
// // // drawCard(deck1, hand);
// // // drawCard(deck1, hand);

// // // drawMultiple(2, deck1, hand);

// // // console.log(deck1);
// // // console.log(hand);

// const makeDeck = () => {
// 	return {
// 		deck: [],
// 		drawnCards: [],
// 		suits: ['hearts', 'diamonds', 'spades', 'clubs'],
// 		values: '2,3,4,5,6,7,8,9,10,J,Q,K,A',
// 		initializeDeck() {
// 			for (let value of this.values.split(',')) {
// 				for (let suit of this.suits) {
// 					this.deck.push({ value, suit });
// 				}
// 			}
// 			return this.deck;
// 		},
// 		drawCard() {
// 			const card = this.deck.pop();
// 			this.drawnCards.push(card);
// 			return card;
// 		},
// 		drawMultiple(numCards) {
// 			const cards = [];
// 			for (let i = 0; i < numCards; i++) {
// 				cards.push(this.drawCard());
// 			}
// 			return cards;
// 		},
// 		shuffle() {
// 			const { deck } = this;
// 			for (let i = deck.length - 1; i > 0; i--) {
// 				let j = Math.floor(Math.random() * (i + 1));
// 				[deck[i], deck[j]] = [deck[j], deck[i]];
// 			}
// 		},
// 	};
// };

// const deck1 = makeDeck();
// const deck2 = makeDeck();

// deck1.initializeDeck();
// deck1.shuffle();
// deck2.initializeDeck();
// deck2.shuffle();

// console.log(deck1);
// console.log(deck2);

function person(firstName, lastName, age) {
	return {
		firstName,
		lastName,
		age,
	};
}

// constructor function
function Person(firstName, lastName, age) {
	this.firstName = firstName;
	this.lastName = lastName;
	this.age = age;
}

Person.prototype.greet = function () {
	console.log(`Hello, my name is ${this.firstName}`);
};

const john = person('John', 'Doe', 20);
const jane = person('Jane', 'Doe', 22);
const jack = new Person('Jack', 'Ma', 25);
const jill = new Person('Jill', 'Ma', 26);

console.log(john);
console.log(jane);
console.log(jack);
console.log(jill);

React index.html

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>React</title>
	</head>
	<body>
		<div id="root"></div>

		<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
		<script
			crossorigin
			src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
		<script
			crossorigin
			src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
		<script type="text/babel">
			// const rootElement = document.querySelector('#root');
			// const heading = document.createElement('h1');
			// heading.innerText = 'Hello World!';
			// rootElement.append(heading);

			// const heading = React.createElement('h1', null, 'Hello World from React');
			// const heading = React.createElement('h1', {
			// 	id: 'special',
			// 	className: 'red-text',
			// 	children: 'Hello World from React',
			// });

			// const h1 = React.createElement('h1', null, 'Hello from React');
			// const p = React.createElement(
			// 	'p',
			// 	null,
			// 	'Some text inside a paragraph. More text...'
			// );

			// const div = React.createElement('div', {
			// 	children: [h1, p],
			// });

			// const div2 = React.createElement(
			// 	'div',
			// 	null,
			// 	React.createElement('h1', null, 'Hello World'),
			// 	React.createElement('p', null, 'This is a para')
			// );

			// const h1 = React.createElement('h1', null, 'Hello from React');
			// const heading = (
			// 	<h1 id='hello' className='world'>
			// 		Hello from React
			// 	</h1>
			// );

			// React.createElement('h1', {id: "hello", className}, 'Hello from React')

			// const children = 'Hello World. This is the page title';
			// const className = 'title';

			// const props = { children, className };

			// const h1 = <h1 {...props} className='helloworld' />; // <h1 children='Hello World. This is the page title' className="title" /> // React.createElement('h1', {children, className})

			// const message = 'Hello World';
			// const myClassName = 'special';

			// const h1 = <h1 className={myClassName}>{message.toUpperCase()}</h1>;

			const BlogPost = ({ title, children, buttonUrl, buttonText }) => {
				return (
					<article>
						<h4>{title}</h4>
						<p>{children}</p>
						<a href={buttonUrl}>{buttonText}</a>
					</article>
				);
			};

			const section = (
				<div>
					{BlogPost({
						title: 'Article Title #1',
						children:
							'If you want to share JSX, you can do that as well. In React we call these functions "components" and they have some special properties.',
						buttonText: 'Read More',
						buttonUrl: '#',
					})}
					<BlogPost
						title={'Article Title #4'.toUpperCase()}
						buttonText='Read More'
						buttonUrl='#'>
						If you want to share JSX, you can do that as well. In React we call
						these functions "components" and they have some special properties.
					</BlogPost>
					{React.createElement(
						BlogPost,
						{
							title: 'Article Title #3',

							buttonText: 'Read More',
							buttonUrl: '#',
						},
						'If you want to share JSX, you can do that as well. In React we call these functions "components" and they have some special properties.'
					)}
					{BlogPost({
						title: 'Article Title #2',
						children:
							'A react component is just a function which accepts a props object as an argument and returns the React element. In our case we are passing the props object with a children key-value in it.',
						buttonText: 'Read More',
						buttonUrl: '#',
					})}
				</div>
			);

			ReactDOM.render(section, document.querySelector('#root'));
		</script>
	</body>
</html>

React ZIP

Project

  1. Generate some boilerplate for a basic React application.
    • Go into your workspace/projects directory using the terminal.
    • cd ~/Documents/FullStack/Workspace
    • mkdir rststore
    • npx create-react-app@latest frontend –use-npm
    • code .
    • cd frontend
    • npm start
    • Open public/index.html and clean up the file. Remove all the comments and change the title and meta content.
    • Delete all the files in the src folder. Create and add basic boilerplate code to App.js and index.js.

2. Chakra UI Installation and Setup

  • npm i @chakra-ui/react @emotion/react @emotion/styled framer-motion
  • npm install react-icons –save
  • Edit index.js file. Import ChakraProvider component and wrap the App component with it.

3. Create Header and Footer components

  • Create a folder called src/components in the src folder and add all components in that.

4. Create HomeScreen product listings

  • Create the HomeScreen component (all display/screen components will go in the /screens folder), along with the product and ratings components.

5. Install and Implement React Router

  • npm install react-router-dom
  • Complete all the React Router setup on the App page.
  • Modify Product cards, Header links etc. Use React Routers Link component instead.

6. Design and build the ProductScreen component.

  1. Setting up the backend
    • Close the React server.
    • Create a folder named backend outside the frontend folder in the root of our project.
    • npm init in the root. (not inside the backend or frontend folders, but outside in the root directory)
    • During npm init setup the main will be server.js
    • npm install express (in the root folder)
    • Create a file backend/server.js in the backend folder and a folder data and copy products.js to this folder.
    • Create some routes to serve the data from the backend
    • Create a script for start in package.json
    • Create 2 basic routes for ‘/’ and ‘/api/products’
    • Convert products.js export statement to commonjs format.
    • Create another route to fetch single product by id.
  1. Fetching data from our backend using React
    • Inside the frontend folder, run npm install axios
    • Modify the HomeScreen component to fetch and store data in the component.
    • To fix the issue of our backend address for now, we’ll add a proxy to the frontend’s package.json. Add this “proxy”: “http://127.0.0.1:5000”, to frontend/package.json
    • Make sure both frontend and backend are running in two terminals and test it out.
    • Modify the ProductScreen component to also make a request to our backend to fetch data.
    • Delete the projects.js file from our frontend’s src folder as we no longer need it.

9. More backend setup

  • In the root directory: run
  • npm install -D nodemon concurrently. ‘-D’ is a dev dependency, meaning we only need these modules during development.
  • These above packages will help us auto restart our server when we change our code, so we don’t have to do it manually.
  • Modify scripts in package.json (root folder) to add some scripts to use our npm command directly to work them.

10. Setup environment variables

  • npm install dotenv
  • Do the dotenv setup in server.js file and create a .env file in the root folder.

11. Convert imports to ES Modules

  • Add “type”: “module” to package.json file. Change all import and export statements to ES module style.
  • Note: You will have to add .js extension while using ES modules in the backend.

12. Install and setup MongoDB

  • After installation, setup MongoDB Compass
  • Use this as the connection string: mongodb://localhost:27017/rststore
  • Add the following in the .env
    MONGO_URI = mongodb://localhost:27017/rststore

13. Connect to the database

  • Install mongoose: npm install mongoose (in the root folder)
  • Create a folder named config in the backend folder and create a file named backend/config/db.js inside it.
  • Do all the mongoDB connection setup
  • Import db.js into server.js and run the connectDB function.
  • Start the server and check if any error.

14. Improve console log messages.

  • Run this command in the root folder: npm install colors
  • Add stylings to server and db console messages.

15. Create database models for data

  • Create a models folder inside the backend folder.
  • Create all our data model schemas.

16. Prepare sample data for the backend data seeding

  • Delete all the _id key-values from the products.js as MongoDB
    will automatically create it for us.
  • Create data/users.js file and fill it with a few user objects.
  • npm install bcryptjs – for password hashing
  • Use bcryptjs for the password fields to encrypt the password. (temporary)

17. Create database seeder for quick sample data generation (optional)

  • Create backend/seeder.js file inside the backend folder.
  • Add seeding logic and create import and destroy scripts.

18. Fetching products from the database

  • Create a folder named backend/routes in the backend folder.
  • Create a file named backend/routes/productRoutes.js
  • npm install express-async-handler – we will wrap our callbacks with this function so that we can do error handler in a better way.
  • Move the products and single product fetch routes to this file and add all the logic for now.
  • Test the routes in Postman

19. Do Postman setup to work with our API (optional)Custom error handling. Run the following in the root folder.

20. Custom error handling. Run the following in the root folder.

  • npm install express-async-handler
  • Create a folder named backend/middleware in the backend folder
  • Create an errorMiddleware.js and add logic to handle errors.

21. Introduction to Redux

  • The Redux Pattern
  • Download the Redux DevTools brower extension.
  • Go to the frontend folder. cd frontend
  • npm install redux react-redux redux-thunk redux-devtools-extension
  • In the src folder, create a file named store.js
  • Import Provider and store into the index.js and do all the setup.

22. Create our first reducer

  • In frontend/src create a folder called reducers – (frontend/src/reducers).
  • Create a file (our reducer) named productReducer.js inside the reducers folder and add all the reducer logic.
  • Next, import productListReducer to store.js and add it to the combineReducers({}) function’s argument object as a new key/value.
  • Create a folder called constants in the frontend/src folder and inside it create the productConstants.js file to store all our action names.
  • Create a folder called actions in the frontend/src folder and inside it create the productActions.js file to store all our action functions. We will dispatch actions to our reducer.
  • All these steps, we do for each resource of our app. So, whether it’s products, or users or some other model/feature, this is the format/pattern we will follow for Redux. We create the constant, the reducer the action and then we fire it off in the component.
  • Next, we now want to fire this action off in our HomeScreen components where we need this products data.

  1. Getting Redux State in the Home Screen
    • Clean up the file. Remove axios import, the [products, setProducts] state variables and remove everything from inside useEffect. We don’t need these anymore.
    • Import useDispatch and useSelector from ‘react-redux’. Also import listProducts from ‘../actions/productActions’.
    • The first hook will be used to dispatch/call an action, and the other is use to select parts of the state. Here we will need the productList part of the state.
    • Create the dispatch object using the useDispatch hook and call it in useEffect to fire the listProducts action.
    • To select products from our state, we need to use the useSelector hook. This hook will take in an arrow function. This function gets state and then we can select which part of the state do we want.
    • Add a conditional to display a loading messageerror message or our product list.

24. Create Message and Loader components.

  • Use the Spinner and Alert components from ChakraUI to add these.

25. Single product details screen Reducer and Action

  • Again, we will follow the same pattern/steps as earlier.
  • Start off by adding the required constants. Since this is for the single product screen, we will add the constants once again to the productConstants.js
  • Create a new reducer named productDetailsReducer in productReducers.js.
  • Whenever we create a new reducer, we have to add it to our store. So, import productDetailsReducer in the store.js file and add a new piece of state named productDetails.
  • Next, step will be to create an action. Add a new action named listProductDetails to the actions file.
  • Next, in the ProductScreen.js, get rid of axios and clean up useEffect. Import useDispatchuseSelector and the listProductDetails action that we just created.
  • Create the dispatch object and dispatch the listProductDetails action in the useEffect hook. You will now be able to see the state in Redux Devtools.
  • Use useSelector hook and select the productDetails piece of state. De-structure the correct values and use them in the JSX to display the product details, error and loading components.

26. Cart and Quantity

  • Add a quantity select box with it’s logic to only contain the number of items in stock.
  • The add to cart button should redirect to the a cart page/screen with the product id and quantity as a query string.

27. Cart Screen and Route

  • Create screens/CartScreen.js file in the screens folder.
  • Import this new CartScreen.js component in the App.js file and create a route for the Cart screen<Route path=’/cart/:id?’ component={CartScreen} />
  • The :id? question mark here means that this id is optional in the route/address. Because if we directly go to the cart page we will not have any id.

28. Cart Functionality

  • Create constants/cartConstants.js and add CART_ADD_ITEM and CART_REMOVE_ITEM constant varaibles to it.
  • Create reducers/cartReducer.js and add the reducer logic.
  • Import cartReducer.js in store.js and add the cartReducer function to the combineReducers argument object.
  • Create actions/cartActions.js. Do the below steps for the building the cart actions.
  • Import axios. We need axios to make a request to /api/products/:id to get the data/fields for that particular product.
  • Import CART_ADD_ITEM from actions/cartActions.js.
  • Then create the addToCart function which will get a (id, qty). We will get both these from the URL params.
  • We will need to use thunk as we are making an async request. So we will return an async function from it. This async function will get (dispatch, getState)dispatch is used for dispatching as usual, but getState will allow us to get our entire state tree. So anything we want like productListproductDetailscart, we can get it using getState.
  • After dispatching, we also want to store this in localStorage.
  • getState().cart.cartItems will give us back a JavaScript object and we can only store strings in the browser localStorage. Hence we have to stringify it. And when we want to take it out and read, we will have to parse it using JSON.parse
  • So we saved it to localStorage but where do we get it to actually fill the state, whenever we reload. We do that in the store.js.
  • We will first see if there is anything in cartItems in the localStorage. If it’s there, then we will add it to the initial state, so always loaded on the app’s first load. If nothing is present in the localStorage then we will just add an empty array.

29. Completing CartScreen.js and creating ‘add to cart’ functionality

  • Build the add to cart functionality.
  • Finish the CartScreen.js component.
  • Add functionality to the Remove Item from cart button. Follow the steps below:
  • Add CART_REMOVE_ITEM to the cartReducer.js file.
  • Create an action named removeFromCart in the cartActions.js file.
  • Fire this action in the removeFromCartHandler function in the CartScreen component.

Project

  1. Generate some boilerplate for a basic React application.
    • Go into your workspace/projects directory using the terminal.
    • cd ~/Documents/FullStack/Workspace
    • mkdir rststore
    • npx create-react-app@latest frontend –use-npm
    • code .
    • cd frontend
    • npm start
    • Open public/index.html and clean up the file. Remove all the comments and change the title and meta content.
    • Delete all the files in the src folder. Create and add basic boilerplate code to App.js and index.js.

2. Chakra UI Installation and Setup

  • npm i @chakra-ui/react @emotion/react @emotion/styled framer-motion
  • npm install react-icons –save
  • Edit index.js file. Import ChakraProvider component and wrap the App component with it.

Project

  1. Generate some boilerplate for a basic React application.
    • Go into your workspace/projects directory using the terminal.
    • cd ~/Documents/FullStack/Workspace
    • mkdir rststore
    • npx create-react-app@latest frontend –use-npm
    • code .
    • cd frontend
    • npm start
    • Open public/index.html and clean up the file. Remove all the comments and change the title and meta content.
    • Delete all the files in the src folder. Create and add basic boilerplate code to App.js and index.js.

2. Chakra UI Installation and Setup

  • npm i @chakra-ui/react @emotion/react @emotion/styled framer-motion
  • npm install react-icons –save
  • Edit index.js file. Import ChakraProvider component and wrap the App component with it.

3. Create Header and Footer components

  • Create a folder called src/components in the src folder and add all components in that.

4. Create HomeScreen product listings

  • Create the HomeScreen component (all display/screen components will go in the /screens folder), along with the product and ratings components.

5. Install and Implement React Router

  • npm install react-router-dom
  • Complete all the React Router setup on the App page.
  • Modify Product cards, Header links etc. Use React Routers Link component instead.

6. Design and build the ProductScreen component.

  1. Setting up the backend
    • Close the React server.
    • Create a folder named backend outside the frontend folder in the root of our project.
    • npm init in the root. (not inside the backend or frontend folders, but outside in the root directory)
    • During npm init setup the main will be server.js
    • npm install express (in the root folder)
    • Create a file backend/server.js in the backend folder and a folder data and copy products.js to this folder.
    • Create some routes to serve the data from the backend
    • Create a script for start in package.json
    • Create 2 basic routes for ‘/’ and ‘/api/products’
    • Convert products.js export statement to commonjs format.
    • Create another route to fetch single product by id.
  1. Fetching data from our backend using React
    • Inside the frontend folder, run npm install axios
    • Modify the HomeScreen component to fetch and store data in the component.
    • To fix the issue of our backend address for now, we’ll add a proxy to the frontend’s package.json. Add this “proxy”: “http://127.0.0.1:5000”, to frontend/package.json
    • Make sure both frontend and backend are running in two terminals and test it out.
    • Modify the ProductScreen component to also make a request to our backend to fetch data.
    • Delete the projects.js file from our frontend’s src folder as we no longer need it.

9. More backend setup

  • In the root directory: run
  • npm install -D nodemon concurrently. ‘-D’ is a dev dependency, meaning we only need these modules during development.
  • These above packages will help us auto restart our server when we change our code, so we don’t have to do it manually.
  • Modify scripts in package.json (root folder) to add some scripts to use our npm command directly to work them.

10. Setup environment variables

  • npm install dotenv
  • Do the dotenv setup in server.js file and create a .env file in the root folder.

11. Convert imports to ES Modules

  • Add “type”: “module” to package.json file. Change all import and export statements to ES module style.
  • Note: You will have to add .js extension while using ES modules in the backend.

12. Install and setup MongoDB

  • After installation, setup MongoDB Compass
  • Use this as the connection string: mongodb://localhost:27017/rststore
  • Add the following in the .env
    MONGO_URI = mongodb://localhost:27017/rststore

13. Connect to the database

  • Install mongoose: npm install mongoose (in the root folder)
  • Create a folder named config in the backend folder and create a file named backend/config/db.js inside it.
  • Do all the mongoDB connection setup
  • Import db.js into server.js and run the connectDB function.
  • Start the server and check if any error.

14. Improve console log messages.

  • Run this command in the root folder: npm install colors
  • Add stylings to server and db console messages.

15. Create database models for data

  • Create a models folder inside the backend folder.
  • Create all our data model schemas.

16. Prepare sample data for the backend data seeding

  • Delete all the _id key-values from the products.js as MongoDB
    will automatically create it for us.
  • Create data/users.js file and fill it with a few user objects.
  • npm install bcryptjs – for password hashing
  • Use bcryptjs for the password fields to encrypt the password. (temporary)

17. Create database seeder for quick sample data generation (optional)

  • Create backend/seeder.js file inside the backend folder.
  • Add seeding logic and create import and destroy scripts.

18. Fetching products from the database

  • Create a folder named backend/routes in the backend folder.
  • Create a file named backend/routes/productRoutes.js
  • npm install express-async-handler – we will wrap our callbacks with this function so that we can do error handler in a better way.
  • Move the products and single product fetch routes to this file and add all the logic for now.
  • Test the routes in Postman

19. Do Postman setup to work with our API (optional)Custom error handling. Run the following in the root folder.

20. Custom error handling. Run the following in the root folder.

  • npm install express-async-handler
  • Create a folder named backend/middleware in the backend folder
  • Create an errorMiddleware.js and add logic to handle errors.

21. Introduction to Redux

  • The Redux Pattern
  • Download the Redux DevTools brower extension.
  • Go to the frontend folder. cd frontend
  • npm install redux react-redux redux-thunk redux-devtools-extension
  • In the src folder, create a file named store.js
  • Import Provider and store into the index.js and do all the setup.

22. Create our first reducer

  • In frontend/src create a folder called reducers – (frontend/src/reducers).
  • Create a file (our reducer) named productReducer.js inside the reducers folder and add all the reducer logic.
  • Next, import productListReducer to store.js and add it to the combineReducers({}) function’s argument object as a new key/value.
  • Create a folder called constants in the frontend/src folder and inside it create the productConstants.js file to store all our action names.
  • Create a folder called actions in the frontend/src folder and inside it create the productActions.js file to store all our action functions. We will dispatch actions to our reducer.
  • All these steps, we do for each resource of our app. So, whether it’s products, or users or some other model/feature, this is the format/pattern we will follow for Redux. We create the constant, the reducer the action and then we fire it off in the component.
  • Next, we now want to fire this action off in our HomeScreen components where we need this products data.

  1. Getting Redux State in the Home Screen
    • Clean up the file. Remove axios import, the [products, setProducts] state variables and remove everything from inside useEffect. We don’t need these anymore.
    • Import useDispatch and useSelector from ‘react-redux’. Also import listProducts from ‘../actions/productActions’.
    • The first hook will be used to dispatch/call an action, and the other is use to select parts of the state. Here we will need the productList part of the state.
    • Create the dispatch object using the useDispatch hook and call it in useEffect to fire the listProducts action.
    • To select products from our state, we need to use the useSelector hook. This hook will take in an arrow function. This function gets state and then we can select which part of the state do we want.
    • Add a conditional to display a loading messageerror message or our product list.

24. Create Message and Loader components.

  • Use the Spinner and Alert components from ChakraUI to add these.

25. Single product details screen Reducer and Action

  • Again, we will follow the same pattern/steps as earlier.
  • Start off by adding the required constants. Since this is for the single product screen, we will add the constants once again to the productConstants.js
  • Create a new reducer named productDetailsReducer in productReducers.js.
  • Whenever we create a new reducer, we have to add it to our store. So, import productDetailsReducer in the store.js file and add a new piece of state named productDetails.
  • Next, step will be to create an action. Add a new action named listProductDetails to the actions file.
  • Next, in the ProductScreen.js, get rid of axios and clean up useEffect. Import useDispatchuseSelector and the listProductDetails action that we just created.
  • Create the dispatch object and dispatch the listProductDetails action in the useEffect hook. You will now be able to see the state in Redux Devtools.
  • Use useSelector hook and select the productDetails piece of state. De-structure the correct values and use them in the JSX to display the product details, error and loading components.

26. Cart and Quantity

  • Add a quantity select box with it’s logic to only contain the number of items in stock.
  • The add to cart button should redirect to the a cart page/screen with the product id and quantity as a query string.

27. Cart Screen and Route

  • Create screens/CartScreen.js file in the screens folder.
  • Import this new CartScreen.js component in the App.js file and create a route for the Cart screen<Route path=’/cart/:id?’ component={CartScreen} />
  • The :id? question mark here means that this id is optional in the route/address. Because if we directly go to the cart page we will not have any id.

28. Cart Functionality

  • Create constants/cartConstants.js and add CART_ADD_ITEM and CART_REMOVE_ITEM constant varaibles to it.
  • Create reducers/cartReducer.js and add the reducer logic.
  • Import cartReducer.js in store.js and add the cartReducer function to the combineReducers argument object.
  • Create actions/cartActions.js. Do the below steps for the building the cart actions.
  • Import axios. We need axios to make a request to /api/products/:id to get the data/fields for that particular product.
  • Import CART_ADD_ITEM from actions/cartActions.js.
  • Then create the addToCart function which will get a (id, qty). We will get both these from the URL params.
  • We will need to use thunk as we are making an async request. So we will return an async function from it. This async function will get (dispatch, getState)dispatch is used for dispatching as usual, but getState will allow us to get our entire state tree. So anything we want like productListproductDetailscart, we can get it using getState.
  • After dispatching, we also want to store this in localStorage.
  • getState().cart.cartItems will give us back a JavaScript object and we can only store strings in the browser localStorage. Hence we have to stringify it. And when we want to take it out and read, we will have to parse it using JSON.parse
  • So we saved it to localStorage but where do we get it to actually fill the state, whenever we reload. We do that in the store.js.
  • We will first see if there is anything in cartItems in the localStorage. If it’s there, then we will add it to the initial state, so always loaded on the app’s first load. If nothing is present in the localStorage then we will just add an empty array.

29. Completing CartScreen.js and creating ‘add to cart’ functionality

  • Build the add to cart functionality.
  • Finish the CartScreen.js component.
  • Add functionality to the Remove Item from cart button. Follow the steps below:
  • Add CART_REMOVE_ITEM to the cartReducer.js file.
  • Create an action named removeFromCart in the cartActions.js file.
  • Fire this action in the removeFromCartHandler function in the CartScreen component.

30. Clean up the backend routes by extracting their logic into controllers

  • In the backend folder, create a folder called controller and create a file called productController.js inside it.
  • Extract all the logic to the productController.js from the routes file. The routes file should now only be for routing and all logic will go in to the controllers.
  • In productRoutes.js instead of using the method router.use(), instead use router.route() and add the route inside and then chain the appropriate methods get, post, put, delete etc. to it. This way we can define different controller logic to the same route. We will see this in sometime.

31. User Authentication Endpoints (Backend Routes)

  • Create routes/userRoutes.js and controllers/userController.js.
  • Start by working on an auth route. So here we want to authenticate a user by email and password. And then we want to send back some data, a token which we can save on the client (browser), so in the frontend (browser) we can use that token to access protected pages/routes (react frontend routes).
  • Import the ../models/userModel.js as we will need it to create new users in the MongoDB database.
  • Create authUser controller function. Inside it, first thing is to get data from the body. This data is on the request object and is something that will be sent here (to the backend) by a POST request and is usually sent via a form on the frontend pages. We can also mimic this sending data using Postman.
  • Before getting the data, make a new folder in rststore Postman collection.. Add a folder called Users & Auth. Inside that create a new request. Name it POST /api/users/login with the url to {{URL}}/api/users/login and add an email & password object to Body of type JSON.
  • Now this object that we add to the body tab in Postman, what will be sent in an object (key-value) on the request object. We extract that from request.body.
  • But in order for parsing this JSON data that we get on the request object from the frontend/Postman, we need to add another middleware in server.js.
  • Add the following line after const app = express part. app.use(express.json())
  • Create the authUser controller function. Also import this into userRoutes.js and add a login route.
  • Also import userRoutes in server.js and add the routes for users..
  • app.use(‘/api/users’, userRoutes)
  • Use the findOne() method on the User model to get the user’s object from the database.
  • Password needs to be encrypted before checking as we are storing encrypted passwords in the database, so add a method on the User Model itself. Edit models/userModel.js and add a method on the userSchema – userSchema.methods.matchPassword.
  • Use this new method in userController.js and finished the implementation.
  • Don’t return the token for now. Just set it to null. Return all other details leaving the password as a json object and also add an else condition.
  • Test this new endpoint in POSTMAN. (start only the server for now)

32. Using Json Web Tokens (JWT)

  • What are JWTs and how do they work?
  • Installation: npm install jsonwebtoken
  • Create a folder called backend/utils. Put all helpers and utility functions here. Inside it create a file named generateToken.js and create the token generation function. Also add the JWT_SECRET to the env as this is needed for the token generation.
  • Import generateToken.js into userController.js and use the function in the response object’s token property (key).
  • Test in Postman. Add this request to Postman.

33. Creating custom Authentication Middleware to accessprotected routes.

  • Add a new request in Postman. GET /api/users/profile with request URL {{URL}}/api/users/profile.
  • Add getUserProfile controller method to userController.js and also export it. Also add the route for this in userRoutes.js.
  • Create a new file named middleware/authMiddleware.js. This middleware will validate the token.
  • Implement the protect middleware function. In our backend we will be getting the authorization tokens in the header object in requests (we will send it that way from React).
  • Import protect into routes/userRoutes.js. We need to add our middleware function here to the /profile endpoint.
  • Implement the complete of the protect middleware function.
  • Once done, we can now use this middleware to any endpoint (route) that we want to be protected, that is only accessible using a valid JWT.
  • Finish implementation of the getUserProfile controller function in userController.js
  • Save the token in Postman, so we don’t have to keep copying and pasting tokens to the Headers. Add Tests to login request so that the token can be set in an environment variable. After that set profile request Auth Type to Bearer Token.

34. User registration

  • Add a new request to Postman: POST /api/users with url as {{URL}}/api/users. This will be a POST request. So a GET request to the same endpoint will give us a total list of users, while a POST request will create a new user.
  • Add a new registerUser controller function to the userController.js file. After that, import this function to userRoutes.js and create a new register route.
  • #### Password Encryption ####
  • The password still isn’t encrypted as we sent it directly to the User.create() function. So to encrypt password while creating a new user and adding it to the database, create a new mongoose middleware.
  • In mongoose, we can set certain things to happen on saves or finds etc. So when we execute the User.create function, before saving to database, we can run some code to encrypt the password and then save.
  • Test the new endpoint in Postman.

35. User Login Reducer and Action

  • Add all constants for the login. Create the file constants/userConstants.js and add all the constants.
  • Create the reducers/userReducer.js file and add the userLoginReducer function. Add all the necessary actions and logic.
  • Import userLoginReducer to store.js and add it to the combineReducers function’s parameter object.
  • Create the actions file in actions/userActions.js and add the action logic. This is pretty much the same as the earlier actions. Only difference here is that we have to add some headers to the axios request, and also store the user data to localStorage.
  • Lastly, since we stored the user data in localStorage, we should load them in the initial state in store.js. Create a variable named userInfoFromStorage and set it to get it’s value from localStorage. In the initialState variable, add another key named userLogin and set it’s value to userInfouserLogin: { userInfo: userInfoFromStorage }

36. User Login Screen

  • Create a new component called components/FormContainer.js which will just be a simple wrapper for our form elements. Just a box with some styling, which we will use to add all our forms in.
  • Create a new screen in screens/LoginScreen.js and write the ui and logic.
  • Import it in App.js and create a route for it.
  • (styling changes overall if required)

37. Implement all the Login – Redux functionality in the LoginScreen component.

38. Header modification to show User and User Logout feature

  • Modify Header.js. Import redux modules, for running the LOGOUT action and getting access to the state.
    import {useDispatch, useSelector} from ‘react-redux’
  • Import Menu, MenuButton, MenuList, MenuItem from Chakra.
  • Get the userInfo state using useSelector.
  • Also import import { IoChevronDown } from ‘react-icons/io5’
  • Create a logout action in userActions.js.
  • Implement all the logic.

39. User Register, Constants, Reducer, Action and Screen.

  • Add new REGISTER constants to the userConstants.js
  • Create userRegisterReducer in the userReducers.js
  • Import this reducer in store.js and add it to combineReducers
  • Create register action in userActions.js
  • Create the RegisterScreen component and add it to the App.js router.

40. Update User Profile endpoint in the backend

  • Create a updateUserProfile controller method and export it.
  • Import updateUserProfile in userRoutes.js and add a PUT request on the same /profile route. This route will also take the middleware protect as this is a protected endpoint.

41. User Profile Screen and Getting User Details

  • Add USER DETAILS constants to the userConstants.js file.
  • Create userDetailsReducer in userReducer.js and import and add it to the Store.
  • Create and add a getUserDetails action in userActions.js
  • Create the ProfileScreen and add it to Router in App.js

42. Add Update User Profile functionality

  • Add new UPDATE PROFILE constants to the userConstants.js
  • Create userUpdateProfileReducer in the userReducers.js
  • Import this reducer in store.js and add it to combineReducers
  • Create updateUserProfile action in userActions.js

43. Shipping Screen and Save Address

  • Create a new component/screen in screen/ShippingScreen.js and add it to the Router in App.js
  • Complete the ShippingScreen component. Create all the local state required and build the shipping form.
  • In the submitHandler we want to dispatch an action that will save the shipping address to the Redux store.
  • Create a new constant named CART_SAVE_SHIPPING_ADDRESS in the constants/cartConstants.js file.
  • Create a new action named saveShippingAddress in the actions/cartActions.js file.
  • Create a new case CART_SAVE_SHIPPING_ADDRESS in the reducers/cartReducers.js file. Also in the cartReducer function’s initial state object, add another key named shippingAddress and set it to an empty object.
  • Since we are going to store the shippingAddress to localStorage, we also should check if it’s already present in the user’s machine and load it if present. Add the code to the store.js file.
  • Add all Redux related functionality to the ShippingScreen component and finish the implementation.

44. Checkout Steps Component

  • Create he component and then add it to the ShippingScreen component.

45. PaymentScreen – where users can choose the payment method

  • Create the PaymentScreen component in the screens folder.
  • Add CART_SAVE_PAYMENT_METHOD to the cartConstants.js file.
  • Create the savePaymentMethod action function in actions/cartActions.js file.
  • Create a new case CART_SAVE_PAYMENT_METHOD in the reducers/cartReducer.js
  • Import and add PaymentScreen to App.js router.

46. Place Order Screen

  • We will just create a basic screen/page for now. We will do all the real setup only once we have created a backend to actually accept an order.
  • Create a new screen named PlaceOrderScreen.js in the screen folder.
  • Complete the PlaceOrderScreen implementation for now. We will complete this fully when we are done with our order functionality in the backend.
  • Also calculate and set cart.itemsPrice, cart.shippingPrice, cart.taxPrice, cart.totalPrice

47. Backend: Order controller and endpoint (route)

  • Create controllers/orderController.js.
  • Create routes/orderRoutes.js. Import the orderController and connect it here to a endpoint.
  • Lastly add the main endpoint (route) to server.js

48. Create Order

  • Create an constants/orderConstants.js file and add all the constants.
  • Create a new reducer file in reducers/orderReducers.js and add a orderCreateReducer function in it. Import this in store.js and add it to combineReducers.
  • Create a new actions file in actions/orderActions.js and add a createOrder action function.
  • Import createOrder action into the PlaceOrderScreen component and complete the implementation.

49. Get Order By ID (Backend Endpoint)

  • Create the getORderById controller function in the orderController.js file.
  • Add ‘/:id’ route to the orderRoute.js file and attached it to the getOrderbyID controller function.

50. Create the order details reducer and action (frontend)

  • Add new order details related constants in the orderConstants.js file.
  • Create orderDetailsReducer to the orderReducer.js file and connect it in the store.js file.
  • Create a new getOrderDetails function in the orderActions.js file.

51. Create the Order Screen component

  • Create the OrderScreen.js file in screens.
  • Add this new screen to the App.js route.

52. Backend endpoint for updating an order to paid

  • Create a new updateOrderToPaid controller function in the backend/controllers/orderController.js file.
  • Import updateOrderToPaid in the routes/orderRoutes.js and add a route for the endpoint ‘/:id/pay’ making it a PUT request.

53. Order pay reducer and action

  • Add new ORDER_PAY_ constants to the constants/orderConstants.js file in the frontend.
  • Create a new reducer function orderPayReducer in the reducers/orderReducers.js. Import this in store.js and add it to the combineReducers function.
  • Create a new action named payOrder in the actions/orderAction.js file.

54. Adding PayPal Payments

  • Signup for a free Personal or Business account on PayPal.
  • After signup process completion, go to https://developer.paypal.com/developer/applications
  • Go to Sandbox -> Accounts and create 2 Accounts, a personal which you will use to pay and a business to which you will be paying. This will be a sandbox/mock/test environment for working with the PayPal API. You can simply use the 2 default Sandbox Accounts provided by PayPal.
  • Go to Dashboard -> My Apps & Credentials and make sure Sandbox mode is activated. Click on Create App and follow the process to create a new application. Give the app name ‘rststore’, app type ‘Merchant’ and select the business email (do not use the personal email here). Check everything carefully and then click Create App.
  • After creation you will get your Sandbox API Credentials. We won’t add the Client ID in the frontend. We’ll add it to the backend and create a route to access it.
  • Add the client ID in the .env file and create a route/endpoint in server.js
  • In order to use PayPal, we need to add a script to our site. Visit https://developer.paypal.com/docs/checkout/reference/customize-sdk/ to see the details. We will need to add this script <script src=”https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID”></script> to the orderScreen.
  • Edit the OrderScreen.js file and do all the PayPal implementations.
  • We will use an npm package for adding the PayPal button to the screen. Install the package: npm i react-paypal-button-v2. Make sure you install this in the frontend folder.
  • Add all the paypal functionality and finish the page’s implementation.

55. Show Orders on Profile Page

  • In the backend, create a new controller function, controllers/getMyOrders.js
  • Import this controller in the routes/orderRoutes.js file and connect the route/endpoint.
  • (Optional) Test the endpoint in Postman.
  • Add new ORDER_MY_LIST constants to the constants/orderConstants.js file.
  • Create a reducer named orderMyListReducer in the reducers folder.
  • Import and add this reducer to the combineReducer function’s argument object in store.js
  • Create a new action named listMyOrders and add it to the orderAction.js.
  • Modify the ProfileScreen.js file and implement showing orders in there.

56. Clear state on logout

  • Create ORDER_MY_LIST_RESET in orderConstants.js and add that as a case to the orderMyListReducer function in orderReducer.js
  • Create USER_DETAILS_RESET in userConstants.js and add that as a case to the userDetailsReducer function in userReducer.js
  • Import USER_DETAILS_RESET and ORDER_MY_LIST_RESET and dispatch it in the logout action function.

57. Admin Middleware and Getting Users Endpoint

  • We will create routes that are admin protected and will only be accessible by admin users.
  • For testing purposed, add this as a request to Postman. {{URL}}/api/users. Make a new request for this URL and name this request GET /api/users.
  • In the backend, create a new controller named getUsers in controllers/userController.js
  • Import this controller in routes/userRoutes.js and add it as a get request.
  • Create a new admin auth middleware in middlewares/authMiddlewares.js. This will check if a user is an admin. Import this userRoutes.js and protect the required route.
  • Test route in Postman.

58. Admin User List – Frontend

  • Add new USER_LIST constants to userConstants.js
  • Import these constants and create a new reducer userListReducer in the userReducer.js file. Add it to store.js as well.
  • Create new action called listUsers in userActions.js
  • Create a component called UserListScreen.js in the screens folder. This screen will show admins the complete list of users.
  • Modify the Header.js to show the admin/manage menu and it’s links.

59. Admin Screen page security

  • Edit the UserListScreen.js. Bring in the userLogin state and read the current user login info. If the user is not an admin, then push the user to the ‘/login’ page.
  • Go to the userConstants.js file and add USER_LIST_RESET to it.
  • Import USER_LIST_RESET in the userReducers.js file and add the new case to the userListReducer function.
  • Import USER_LIST_RESET in the userActions.js file and dispatch it in the LOGOUT action. This will clear the users list from redux when an admin logs out.

60. Delete User Functionality (for Admins)

  • Create a new controller function named deleteUser in backend/controllers/userController.js.
  • Import this controller in routes/userRoutes.js and create a new /:id route and add the controller which should be protected by the protect and admin middlewares, to a new route of ‘/:id’.
  • Test this new backend route in Postman. After this, implement these features in the frontend.
  • Add new USER_DELETE_ constants in the frontend/constants/userConstants.js.
  • Add a new reducer function named userDeleteReducer in the reducers/userReducers.js file. Add this reducer to store.js.
  • Add a new action function named deleteUsers to the actions/userActions.js file. Dispatch this action in the UsersListScreen component.

61. Backend endpoints for getting and updating user by it’s ID

  • Add getUserById and updateUser controller methods to the userController.js file. Import these controllers in the userRoutes.js and add the routes.
  • Test these in Postman.

62. User Edit screen and User Details screen components

  • Update the edit link in UserListScreen.js
  • Create a new file named UserEditScreen and implement it.

63. Update user functionality

  • Add new USER_UPDATE_ constants in the userConstants.js file.
  • Create userUpdateReducer reducer function in the userReducers.js and attach it to the store.js
  • Create a new action named updateUser in the userActions.js file. Dispatch this action correctly in the screens/UserEditScreen.js

64. Admin – Product List

  • In the screens folder, create ProductListScreen.js and implement the component.
  • Import and create a route for it in App.js.

65. Admin – Delete Products

  • Create a new deleteProduct controller function in the backend/productController.js file.
  • Import this new controller in routes/productRoutes.js and create an endpoint for it. Protect this route with the product and admin middlewares.
  • (optional) Test the route in Postman.
  • Now implement the feature in the frontend. Add new PRODUCT_DELETE_ constants in the frontend/constants/productConstants.js file.
  • Import these constants in the reducers/productReducers.js file and create a new productDeleteReducer function. Import and add it to the store.js
  • Create a new deleteProduct action function in actions/productActions.js
  • Import the action back in ProductListScreen.js.
  • (for testing only) You can run npm run data:import. Remember to logout and shut down the server before doing this. Also this will reset all the data in the database, including the users and their orders.

66. Create and Update Product Backend Endpoints

  • The Create Product button will immediately add a product with some dummy data and take us to an edit page where we can edit that data.
  • In the productControllers.js file, add two new controller functions, createProduct and updateProduct.
  • Import and add those controller functions in routes/productRoutes.js
  • (optional) Test it in Postman

67. Admin – Create Product Screen

  • Add new PRODUCT_CREATE_ constants in productConstants.js
  • Add new productCreateReducer function to productReducers.js and add it to store.js
  • Add new createProduct action function in productActions.js
  • Complete the ProductListScreen and implement all the functionality.

68. Product Edit Screen

  • Create a new file named ProductEditScreen.js in the screen folder. Implement the entire component.

69. Admin – Update Product Functionality

  • Add new PRODUCT_UPDATE_ constants to the productConstants.js file.
  • Add new productUpdateReducer reducer function to the productReducers.js and add it to store.js
  • Add new updateProduct action function to the productActions.js
  • Dispatch the action in ProductEditScreen and do all required modifications.

70. Image Upload Configuration and Endpoint

  • Install multer in the root folder: npm install multer
  • In the root folder, we will create a folder called uploads. We will store all our uploads in this folder
  • Create uploadRoutes.js file in the routes folder. Complete the upload implementation.
  • In the server.js file create the main route for uploads and make /uploads folder static so we can use it to upload photos.

71. Upload images from the frontend

  • Edit the ProductEditScreen component and implement the upload button.

72. Admin – Order List

  • In controllers/orderController.js, add a new controller function named getOrders.
  • Import getOrders in the routes/orderRoutes.js file and add the route and controller.
  • Add new ORDER_LIST_ constants to the orderConstants.js file.
  • Create new orderListReducer reducer function in the reducers/orderReducers.js file. Add it to the store.js
  • Create new listOrders action function in the actions/orderActions.js file.
  • Create a new OrderListScreen.js to show the orders list to the admin. Add this new component to the App.js and it’s routes.

73. Admin – Mark order as delivered

  • In the backend, add a new controller function named updateOrderToDelivered in the controllers/orderController.js
  • Import the controller function in routes/orderRoutes.js and create a route for it.
  • Add new ORDER_DELIVER_ constants to orderConstants.js
  • Create new orderDeliverReducer reducer function in reducers/orderReducer.js. Add it to store.js
  • Create a new deliverOrder action function in orderActions.js
  • Dispatch this action in OrderScreen component and comlete the implementation.

74. Create Review Endpoint

  • Associate User to reviewSchema. Edit models/productModel.js and add a user object id ref.
  • Add a new createProductReview controller function in controllers/productControllers.js and add it to routes/productRoutes.js and create a route for it.

75. Create Review Endpoint

  • Associate User to reviewSchema. Edit models/productModel.js and add a user object id ref.
  • Add a new createProductReview controller function in controllers/productControllers.js and add it to routes/productRoutes.js and create a route for it.

76. Adding product reviews on the frontend

  • Add new PRODUCT_CREATE_REVIEW_ constants to productConstants.js
  • Create productReviewCreateReducer reducer function in productReducers.js and add it to store.js
  • Create a new createProductReview action function in productActions.js file.
  • Implement the functionality in the ProductScreen.js