All files / src/components/Burger BurgerBuilder.jsx

86.79% Statements 46/53
91.66% Branches 11/12
85% Functions 17/20
86.95% Lines 40/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161                      3x 26x 26x 26x 26x   26x   26x 1x     26x 7x 6x 1x   6x       26x 1x 1x                   1x 1x     26x   26x 16x 15x 15x 15x 15x 15x   1x       26x 1x 1x     1x       26x 2x     26x               26x                                                   52x             72x       1x                 1x                               1x                              
import React, { useContext, useState, useEffect } from 'react';
import './BurgerBuilder.css';
import BurgerStation from "./BurgerStation";
import { menu } from "../../menuItems";
import { WebSocketContext } from "../../WebSocketContext";
import { playSendSound } from "../SoundEffects/playSendSound";
import { playPopSound } from "../SoundEffects/playPopSound";
import Score from "../Score/Score";
import StationStartModal from "../Modal/StationStartModal";
import Tutorial from "../Modal/Tutorial";
 
const BurgerBuilder = ({ score, day }) => {
    const [ingredients, setIngredients] = useState([]);
    const { send } = useContext(WebSocketContext);
    const [fullMessage, setFullMessage] = useState("");
    const [showStart, setShowStart] = useState(true);
 
    const foodItems = menu[0].children;
 
    const handleStart = () => {
        setShowStart(false);
    };
 
    useEffect(() => {
        if (showStart) {
            const timer = setTimeout(() => {
                handleStart();
            }, 10000);
            return () => clearTimeout(timer);
        }
    }, [showStart]);
 
    const handleSend = () => {
        const employeeOrder = ingredients.map(ingredient => ingredient.name);
        send({
            data: {
                type: "game_state",
                game_state_update_type: "order_component",
                component_type: "burger",
                component: {
                    ingredients: employeeOrder,
                }
            }
        });
        playSendSound();
        clearPlate();
    };
 
    const maxSize = 9;
 
    const addIngredient = (ingredient) => {
        if (ingredients.length <= maxSize) {
            setIngredients([...ingredients, ingredient]);
            setFullMessage("");
            playPopSound();
            const audio = new Audio(ingredient.audio);
            audio.play();
        } else {
            setFullMessage("Plate is Full!");
        }
    };
 
    const removeIngredient = () => {
        setIngredients(prevIngredients => {
            Iif (prevIngredients.length === 0) {
                return prevIngredients;
            }
            return prevIngredients.slice(0, -1);
        });
    };
 
    const clearPlate = () => {
        setIngredients([]);
    };
 
    const handleRequestRepeat = () => {
        console.log("Employee requests manager to repeat order...");
        const audio = new Audio("/audio/repeat_order.mp3");
        audio.play().catch((err) => {
            console.error("Audio playback failed:", err);
        });
    };
 
    return (
        <>
            {showStart && (
                <StationStartModal
                    stationName="Burger"
                    handleClick={handleStart}
                />
            )}
 
            {!showStart && (
                <Tutorial
                    classNames={[
                        "IngredientButtons",
                        "SendOrderButton",
                    ]}
                    audioSourceFolder={"/audio/tutorial/burger"}
                />
            )}
 
            <div className="BurgerBuilder">
                <div className="TopMenuBurger">
                    <Score score={score} day={day} />
                </div>
 
                <div className="IngredientButtons">
                    {foodItems.map((ingredient, index) => (
                        <button key={index} onClick={() => addIngredient(ingredient)}>
                            <img src={ingredient.image} alt={ingredient.name} className="IngredientImage" />
                            <p>{ingredient.name}</p>
                        </button>
                    ))}
                </div>
 
                <BurgerStation imagePaths={ingredients.map((ingredient) => ingredient.sideImage)} />
 
                <button
                    className="BottomButtons"
                    onClick={() => { removeIngredient(); playPopSound(); }}
                    disabled={ingredients.length === 0}
                >
                    <img src="/images/button_icons/undo.png" alt="Undo" className="UndoImage" />
                    <p>Undo</p>
                </button>
 
                <button
                    className="ClearPlateButton"
                    onClick={() => { clearPlate(); playPopSound(); }}
                    disabled={ingredients.length === 0}
                >
                    <img src="/images/button_icons/clear_plate.png" alt="Clear Plate" className="ClearPlateImage" />
                    <p>Delete Burger</p>
                </button>
 
                <button
                    className="BottomButtons"
                    onClick={() => { playPopSound(); handleRequestRepeat(); }}
                >
                    <img src="/images/button_icons/repeat_order.png" className="RepeatOrderImage" alt="Repeat" />
                    <p>Repeat Order</p>
                </button>
 
                <button
                    onClick={() => { handleSend(); playPopSound(); }}
                    className="SendOrderButton"
                >
                    <img src="/images/button_icons/send_order.png" alt="Send Order" className="SendCustomerOrderImage" />
                </button>
 
                <div className="ErrorMessage">
                    {fullMessage && <p>{fullMessage}</p>}
                </div>
            </div>
        </>
    );
};
 
export default BurgerBuilder;