Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 60 additions & 82 deletions src/components/pages/Exercise01/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,112 +7,90 @@
* 4. Apply discount rules. You have an array of offers with discounts depending of the combination of movie you have in your cart.
* You have to apply all discounts in the rules array (discountRules).
* Ex: If m: [1, 2, 3], it means the discount will be applied to the total when the cart has all that products in only.
*
*
* You can modify all the code, this component isn't well designed intentionally. You can redesign it as you need.
*/

import './assets/styles.css'
import { useState } from 'react'
import "./assets/styles.css";
import { useState } from "react";
import { MOVIES, DISCOUNT_RULES } from "../../../constants";

export default function Exercise01 () {
const movies = [
{
id: 1,
name: 'Star Wars',
price: 20
},
{
id: 2,
name: 'Minions',
price: 25
},
{
id: 3,
name: 'Fast and Furious',
price: 10
},
{
id: 4,
name: 'The Lord of the Rings',
price: 5
}
]
export default function Exercise01() {
const [cart, setCart] = useState([]);

const discountRules = [
{
m: [3, 2],
discount: 0.25
},
{
m: [2, 4, 1],
discount: 0.5
},
{
m: [4, 2],
discount: 0.1
}
]
const addMovie = (movie) => {
const findMovie = cart.find((item) => item.id === movie.id);
if (!!findMovie) {
const updatedCart = (myCart) =>
myCart.map((item) =>
item.id === movie.id ? { ...item, quantity: item.quantity + 1 } : item
);
setCart(updatedCart);
} else {
setCart((prevCart) => [...prevCart, { ...movie, quantity: 1 }]);
}
};

const [cart, setCart] = useState([
{
id: 1,
name: 'Star Wars',
price: 20,
quantity: 2
const removeMovie = (movie) => {
const findMovie = cart.find((item) => item.id === movie.id);
let updatedCart;
if (findMovie.quantity > 1) {
updatedCart = (myCart) =>
myCart.map((item) =>
item.id === movie.id ? { ...item, quantity: item.quantity - 1 } : item
);
setCart(updatedCart);
} else {
updatedCart = (myCart) => myCart.filter((item) => item.id !== movie.id);
setCart(updatedCart);
}
])
};

const getTotal = () => {
let total = cart.reduce((sum, item) => sum + item.price * item.quantity, 0);

DISCOUNT_RULES.forEach((rule) => {
const applyDiscount = rule.m.every((ruleId) =>
cart.some((cartItem) => cartItem.id === ruleId)
);
if (applyDiscount) {
const discountAmmount = total - total * rule.discount;
total = discountAmmount;
}
});

const getTotal = () => 0 // TODO: Implement this
return total;
};

return (
<section className="exercise01">
<div className="movies__list">
<ul>
{movies.map(o => (
{MOVIES.map((movie) => (
<li className="movies__list-card">
<ul>
<li>
ID: {o.id}
</li>
<li>
Name: {o.name}
</li>
<li>
Price: ${o.price}
</li>
<li>ID: {movie.id}</li>
<li>Name: {movie.name}</li>
<li>Price: ${movie.price}</li>
</ul>
<button onClick={() => console.log('Add to cart', o)}>
Add to cart
</button>
<button onClick={() => addMovie(movie)}>Add to cart</button>
</li>
))}
</ul>
</div>
<div className="movies__cart">
<ul>
{cart.map(x => (
{cart.map((movie) => (
<li className="movies__cart-card">
<ul>
<li>
ID: {x.id}
</li>
<li>
Name: {x.name}
</li>
<li>
Price: ${x.price}
</li>
<li>ID: {movie.id}</li>
<li>Name: {movie.name}</li>
<li>Price: ${movie.price}</li>
</ul>
<div className="movies__cart-card-quantity">
<button onClick={() => console.log('Decrement quantity', x)}>
-
</button>
<span>
{x.quantity}
</span>
<button onClick={() => console.log('Increment quantity', x)}>
+
</button>
<button onClick={() => removeMovie(movie)}>-</button>
<span>{movie.quantity}</span>
<button onClick={() => addMovie(movie)}>+</button>
</div>
</li>
))}
Expand All @@ -122,5 +100,5 @@ export default function Exercise01 () {
</div>
</div>
</section>
)
}
);
}
80 changes: 72 additions & 8 deletions src/components/pages/Exercise02/assets/styles.css
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
.movie-library {
background-color: var(--black);
color: var(--white);
min-height: 100vh;
padding: 2em;
background-color: #222222;
}

.movie-library__title {
font-size: 2em;
margin-bottom: 1em;
padding-top: 2em;
padding-left: 2em;
}

.movie-library__actions {
display: flex;
margin-bottom: 1em;
padding-left: 2em;
}

.movie-library__actions select {
width: 100%;
width: 90%;
padding: .25em;
}

Expand All @@ -34,18 +35,31 @@

.movie-library__list {
padding: 1em;
background-color: #222222;
column-count: 4;
column-gap: 1rem;
}

.movie-library__card {
background-color: #333333;
margin-bottom: 1em;
margin: 5em;
display: flex;
align-items: center;
justify-content: center;
width: 20em;
height: 30em;
}

.movie-library__card-gradient {
background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 0%, rgba(23, 96, 9, 0.8) 120%);
position: absolute;
width: 20em;
height: 30em;
display: flex;
align-items: flex-end;
justify-content: center;
}

.movie-library__card img {
width: 10em;
width: 20em;
}

.movie-library__card ul {
Expand All @@ -55,3 +69,53 @@
.movie-library__card li {
line-height: 120%;
}

.background-container {
min-height: 100vh;
background-image: url('../assets/mountains.jpeg');
background-size: cover;
background-position: center;
height: 200vh; /* Adjust the height as needed */
}

.background-gradient-container {
min-height: 100vh;
background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 1%, rgba(0, 0, 0, 1) 10%);
}

.form-group {
margin-bottom: 1rem; /* Adjust the margin as needed */
}

.custom-select {
display: block;
width: 90%;
padding: 0.4rem 0.8rem;
font-size: 1.2rem;
line-height: 4;
color: #495057;
background-color: #fff;
border: 3px solid #2fa60a;
border-radius: 0.25rem 0rem 0rem 0.25rem;
}

.custom-select-button {
display: block;
padding: 0.4rem 0.8rem;
font-size: 0.7rem;
line-height: 4;
color: #ffff;
background-color: #2fa60a;
border: 3px solid #2fa60a;
border-radius: 0rem 0.25rem 0.25rem 0rem;
}

.movie-description-wrapper {
list-style: none;
}

.movie-description-wrapper li {
width: 100%;
float: left;
}

Loading