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
68 changes: 50 additions & 18 deletions src/components/pages/Exercise01/assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,26 @@
justify-content: center;
}

.movies__list, .movies__cart {
width: 24em;
.movies__list,
.movies__cart {
width: 25em;
}

.movies__list {
background-color: #222222;
padding: 1em;
border-radius: .2em;
border-radius: 0.2em;
margin-right: 2em;
}

.movies__list-card {
background-color: #333333;
margin-bottom: 1rem;
padding: .75em;
border-radius: .2em;
padding: 0.75em;
border-radius: 0.2em;
display: flex;
flex-direction: row;
align-items: center;
}

.movies__list-card:last-child {
Expand All @@ -39,23 +43,26 @@
background-color: var(--ms-green);
border: none;
cursor: pointer;
font-size: .75em;
border-radius: .25em;
margin-top: .75em;
padding: .25em .5em;
font-size: 0.75em;
border-radius: 0.25em;
margin-top: 0.75em;
padding: 0.25em 0.5em;
}

.movies__cart {
background-color: #222222;
padding: 1em;
border-radius: .2em;
border-radius: 0.2em;
}

.movies__cart-card {
background-color: #333333;
margin-bottom: 1rem;
padding: .75em;
border-radius: .2em;
padding: 0.75em;
border-radius: 0.2em;
display: flex;
flex-direction: row;
align-items: center;
}

.movies__cart-card:last-child {
Expand All @@ -70,23 +77,48 @@
.movies__cart-card-quantity {
display: flex;
align-items: center;
margin-top: .5em;
margin-top: 0.5em;
}

.movies__cart-card span {
margin: 0 .5em;
margin: 0 0.5em;
}

.movies__cart-card button {
background-color: var(--ms-green);
border: none;
cursor: pointer;
font-size: .75em;
border-radius: .25em;
padding: .25em .5em;
font-size: 0.75em;
border-radius: 0.25em;
padding: 0.25em 0.5em;
}

.movies__cart-total {
margin-top: 1em;
font-size: 1.25em;
}
}

.movies__list-header {
margin-top: 0.5em;
margin-bottom: 0.5em;
font-size: 2.25em;
display: flex;
}

.movies__cart-header {
margin-top: 0.5em;
margin-bottom: 0.5em;
font-size: 2.25em;
}

.movie-poster {
max-width: 100px;
max-height: 100px;
}

.movie-info {
display: flex;
flex-direction: column;
justify-content: space-between;
margin-left: 10px;
}
201 changes: 124 additions & 77 deletions src/components/pages/Exercise01/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,121 +6,168 @@
* 3. Calculate and show the total cost of your cart. Ex: Total: $150
* 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.
*
* 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";

export default function Exercise01 () {
export default function Exercise01() {
const [cart, setCart] = useState([]);
const movies = [
{
id: 1,
name: 'Star Wars',
price: 20
title: "Star Wars",
price: 20,
image:
"https://i.pinimg.com/736x/01/f0/be/01f0be4e4c185993e5fc749916d375f1.jpg",
},
{
id: 2,
name: 'Minions',
price: 25
title: "Minions",
price: 25,
image: "https://www.ecartelera.com/carteles/14500/14511/002_p.jpg",
},
{
id: 3,
name: 'Fast and Furious',
price: 10
title: "Fast and Furious",
price: 10,
image:
"https://archivos-cms.cinecolombia.com/images/1/2/7/8/18721-5-esl-CO/FF9_DIGTAL_1_SHEET_MONTAGE_LAT-AM.jpg",
},
{
id: 4,
name: 'The Lord of the Rings',
price: 5
}
]

const discountRules = [
{
m: [3, 2],
discount: 0.25
title: "The Lord of the Rings",
price: 5,
image: "https://www.ecartelera.com/carteles/2600/2650/001_m.jpg",
},
{
m: [2, 4, 1],
discount: 0.5
id: 5,
title: "Ghostbusters",
price: 6,
image: "https://www.ecartelera.com/carteles/4400/4441/001_m.jpg",
},
{
m: [4, 2],
discount: 0.1
}
]
];
const discountRules = [
{ movies: [3, 2], discount: 0.25 },
{ movies: [2, 4, 1], discount: 0.5 },
{ movies: [4, 2], discount: 0.1 },
{ movies: [1, 4, 5], discount: 0.2 },
];

const [cart, setCart] = useState([
{
id: 1,
name: 'Star Wars',
price: 20,
quantity: 2
const getMovieById = (id) => {
return movies.find((movie) => movie.id === id);
};

const addMovieToCart = (id, quantity = 1) => {
const movie = getMovieById(id);
if (movie) {
const cartItem = cart.find((item) => item.movie.id === id);
if (cartItem) {
cartItem.quantity += quantity;
if (cartItem.quantity <= 0) {
setCart(cart.filter((item) => item !== cartItem));
} else {
setCart([...cart]);
}
} else if (quantity > 0) {
setCart([...cart, { movie, quantity }]);
}
}
])
};

const getTotal = () => 0 // TODO: Implement this
const calculateTotalCost = () => {
const total = cart.reduce(
(acc, item) => acc + item.movie.price * item.quantity,
0
);
const discount = calculateDiscount();
return total - discount;
};

const calculateDiscount = () => {
let maxDiscount = 0;
discountRules.forEach((rule) => {
const hasAllMovies = rule.movies.every((movieId) =>
cart.some((item) => item.movie.id === movieId)
);
if (hasAllMovies && rule.discount > maxDiscount) {
maxDiscount = rule.discount;
}
});
return cart.reduce(
(acc, item) => acc + item.movie.price * item.quantity * maxDiscount,
0
);
};

return (
<section className="exercise01">
<div className="movies__list">
<div className="movies__list-header">
<p>Movies Catalog</p>
</div>
<ul>
{movies.map(o => (
<li className="movies__list-card">
<ul>
<li>
ID: {o.id}
</li>
<li>
Name: {o.name}
</li>
<li>
Price: ${o.price}
</li>
</ul>
<button onClick={() => console.log('Add to cart', o)}>
Add to cart
</button>
{movies.map((movie) => (
<li key={movie.id}>
<div className="movies__list-card">
<img
className="movie-poster"
src={movie.image}
alt={movie.title}
/>
<div className="movie-info">
<li>ID: {movie.id}</li>
<li>Name: {movie.title}</li>
<li>Price: ${movie.price}</li>
<button onClick={() => addMovieToCart(movie.id)}>
Add to cart
</button>
</div>
</div>
</li>
))}
</ul>
</div>
<div className="movies__cart">
<ul>
{cart.map(x => (
<li className="movies__cart-card">
<ul>
<li>
ID: {x.id}
</li>
<li>
Name: {x.name}
</li>
<li>
Price: ${x.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>
<div className="movies__cart-header">
<p>Movies Cart</p>
</div>
{cart.map((cartItem) => (
<li key={cartItem.movie.id}>
<div className="movies__cart-card">
<img
className="movie-poster"
src={cartItem.movie.image}
alt={cartItem.movie.title}
/>
<div className="movie-info">
<li>ID: {cartItem.movie.id}</li>
<li>Name: {cartItem.movie.title}</li>
<li>Price: ${cartItem.movie.price}</li>
<div className="movies__cart-card-quantity">
<button
onClick={() => addMovieToCart(cartItem.movie.id, -1)}
>
-
</button>
<span>{cartItem.quantity}</span>
<button onClick={() => addMovieToCart(cartItem.movie.id)}>
+
</button>
</div>
</div>
</div>
</li>
))}
</ul>
<div className="movies__cart-total">
<p>Total: ${getTotal()}</p>
<p>Total: ${calculateTotalCost()}</p>
</div>
</div>
</section>
)
}
);
}
Loading