diff --git a/src/ConferenceEvent.jsx b/src/ConferenceEvent.jsx index 612a4648..9d6e1d04 100644 --- a/src/ConferenceEvent.jsx +++ b/src/ConferenceEvent.jsx @@ -1,12 +1,16 @@ import React, { useState } from "react"; import "./ConferenceEvent.css"; import TotalCost from "./TotalCost"; +import { toggleMealSelection } from "./mealsSlice"; +import { incrementAvQuantity, decrementAvQuantity } from "./avSlice"; import { useSelector, useDispatch } from "react-redux"; import { incrementQuantity, decrementQuantity } from "./venueSlice"; const ConferenceEvent = () => { const [showItems, setShowItems] = useState(false); const [numberOfPeople, setNumberOfPeople] = useState(1); const venueItems = useSelector((state) => state.venue); + const avItems = useSelector((state) => state.av); + const mealsItems = useSelector((state) => state.meals) const dispatch = useDispatch(); const remainingAuditoriumQuantity = 3 - venueItems.find(item => item.name === "Auditorium Hall (Capacity:200)").quantity; @@ -29,22 +33,89 @@ const ConferenceEvent = () => { } }; const handleIncrementAvQuantity = (index) => { + dispatch(incrementAvQuantity(index)); }; const handleDecrementAvQuantity = (index) => { + dispatch(decrementAvQuantity(index)); }; const handleMealSelection = (index) => { + const item = mealsItems[index]; + if (item.selected && item.type === "mealForPeople") { + // Asegurar que numberOfPeople esté configurado antes de alternar la selección + const newNumberOfPeople = item.selected ? numberOfPeople : 0; + dispatch(toggleMealSelection(index, newNumberOfPeople)); + } + else { + dispatch(toggleMealSelection(index)); + } }; const getItemsFromTotalCost = () => { const items = []; + venueItems.forEach((item) => { + if (item.quantity > 0) { + items.push({ ...item, type: "venue" }); + } + }); + avItems.forEach((item) => { + if ( + item.quantity > 0 && + !items.some((i) => i.name === item.name && i.type === "av") + ) { + items.push({ ...item, type: "av" }); + } + }); + mealsItems.forEach((item) => { + if (item.selected) { + const itemForDisplay = { ...item, type: "meals" }; + if (item.numberOfPeople) { + itemForDisplay.numberOfPeople = numberOfPeople; + } + items.push(itemForDisplay); + } + }); + return items; }; const items = getItemsFromTotalCost(); const ItemsDisplay = ({ items }) => { + console.log(items); + return <> +
+ {items.length === 0 &&

No se han seleccionado elementos

} + + + + + + + + + + + {items.map((item, index) => ( + + + + + + + ))} + +
NombreCosto UnitarioCantidadSubtotal
{item.name}${item.cost} + {item.type === "meals" || item.numberOfPeople + ? ` Para ${numberOfPeople} personas` + : item.quantity} + {item.type === "meals" || item.numberOfPeople + ? `${item.cost * numberOfPeople}` + : `${item.cost * item.quantity}`} +
+
+ }; const calculateTotalCost = (section) => { @@ -53,11 +124,22 @@ const ConferenceEvent = () => { venueItems.forEach((item) => { totalCost += item.cost * item.quantity; }); + } else if (section === "av") { + avItems.forEach((item) => { + totalCost += item.cost * item.quantity; + }); + } else if (section === "meals") { + mealsItems.forEach((item) => { + if (item.selected) { + totalCost += item.cost * numberOfPeople; + } + }); } return totalCost; }; const venueTotalCost = calculateTotalCost("venue"); - + const avTotalCost = calculateTotalCost("av"); + const mealsTotalCost = calculateTotalCost("meals"); const navigateToProducts = (idType) => { if (idType == '#venue' || idType == '#addons' || idType == '#meals') { if (showItems) { // Check if showItems is false @@ -65,6 +147,11 @@ const ConferenceEvent = () => { } } } + const totalCosts = { + venue: venueTotalCost, + av: avTotalCost, + meals: mealsTotalCost, + }; return ( <> @@ -153,13 +240,26 @@ const ConferenceEvent = () => {
-

Add-ons Selection

+

Selección de Complementos

- + {avItems.map((item, index) => ( +
+
+ {item.name} +
+
{item.name}
+
${item.cost}
+
+ + {item.quantity} + +
+
+))}
-
Total Cost:
+
Costo Total: {avTotalCost}
@@ -173,20 +273,36 @@ const ConferenceEvent = () => {
- +
+ + setNumberOfPeople(parseInt(e.target.value))} + min="1" + /> +
- + {mealsItems.map((item, index) => ( +
+
+ handleMealSelection(index)} + /> +
-
Total Cost:
- +
${item.cost}
+
+ ))} +
+
Costo Total: {mealsTotalCost}
) : (
- } /> -
+ } /> + ) } diff --git a/src/TotalCost.jsx b/src/TotalCost.jsx index 845abca9..b3c18431 100644 --- a/src/TotalCost.jsx +++ b/src/TotalCost.jsx @@ -1,27 +1,26 @@ -import React, { useState, useEffect } from 'react'; +import React from 'react'; import "./TotalCost.css"; const TotalCost = ({ totalCosts, ItemsDisplay }) => { - - - return ( -
-
-
-

Total cost for the event

-
-
-

- -

- -
- + const total_amount = totalCosts.venue + totalCosts.av + totalCosts.meals; + return ( +
+
+
+

Costo total del evento

+
+
+

+ ${total_amount} +

+
+ +
+
-
-
- ); + ); }; export default TotalCost; + diff --git a/src/avSlice.js b/src/avSlice.js index cdd79aac..a4f07ede 100644 --- a/src/avSlice.js +++ b/src/avSlice.js @@ -1,18 +1,53 @@ +// avSlice.js + import { createSlice } from "@reduxjs/toolkit"; export const avSlice = createSlice({ name: "av", initialState: [ - + { + img: "https://cdn.pixabay.com/photo/2012/03/01/01/10/business-20031_1280.jpg", + name: "Proyectores", + cost: 200, + quantity: 0, + }, + { + img: "https://cdn.pixabay.com/photo/2019/04/07/09/41/speakers-4109274_1280.jpg", + name: "Altavoz", + cost: 35, + quantity: 0, + }, + { + img: "https://cdn.pixabay.com/photo/2019/01/11/01/54/public-speaking-3926344_1280.jpg", + name: "Micrófonos", + cost: 45, + quantity: 0, + }, + { + img: "https://cdn.pixabay.com/photo/2015/01/08/18/30/white-board-593370_1280.jpg", + name: "Pizarras", + cost: 80, + quantity: 0, + }, + { + img: "https://cdn.pixabay.com/photo/2014/06/18/20/58/directory-371692_640.jpg", + name: "Señalización", + cost: 80, + quantity: 0, + }, ], - - reducers: { incrementAvQuantity: (state, action) => { - + const item = state[action.payload]; + if (item) { + item.quantity++; + } }, decrementAvQuantity: (state, action) => { - + const item = state[action.payload]; + if (item && item.quantity > 0) { + item.quantity--; + } }, }, }); diff --git a/src/mealsSlice.js b/src/mealsSlice.js index faf8138a..1f08e0dd 100644 --- a/src/mealsSlice.js +++ b/src/mealsSlice.js @@ -1,13 +1,20 @@ -// mealsSlice.js import { createSlice } from '@reduxjs/toolkit'; export const mealsSlice = createSlice({ name: 'meals', initialState: [ - + { name: 'Desayuno', cost: 50, selected: false }, + { name: 'Té de la tarde', cost: 25, selected: false }, + { name: 'Almuerzo', cost: 65, selected: false }, + { name: 'Cena', cost: 70, selected: false }, ], reducers: { toggleMealSelection: (state, action) => { + // Expect action.payload to be the index of the meal + const index = action.payload; + if (typeof index === 'number' && index >= 0 && index < state.length) { + state[index].selected = !state[index].selected; + } }, }, }); diff --git a/src/store.js b/src/store.js index f05b9f8f..0e8b632f 100644 --- a/src/store.js +++ b/src/store.js @@ -1,9 +1,13 @@ // store.js import { configureStore } from '@reduxjs/toolkit'; import venueReducer from './venueSlice'; +import avReducer from './avSlice'; +import mealsReducer from './mealsSlice'; export default configureStore({ reducer: { venue: venueReducer, + av: avReducer, + meals: mealsReducer, }, });