All files / src/components/Drinks DrinkBuilder.jsx

84% Statements 84/100
76.66% Branches 23/30
78.57% Functions 22/28
87.5% Lines 77/88

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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258                    2x 24x 24x 24x 24x 24x 24x 24x 24x 24x   24x                 24x 24x 24x 24x   24x 5x 5x 5x     24x 6x 5x 1x   5x       24x 1x 1x 1x 1x 1x         1x     1x     24x 1x 1x     24x 1x 1x 1x 1x 1x     24x 1x                       1x 1x 1x 1x     24x 3x 7x 3x   3x 3x 3x 3x 3x 3x     24x 4x 4x 4x 4x 4x 4x     24x 1x 1x 1x 1x     24x               24x                                                       144x     3x 3x                                         1x             3x                                                                                           1x                                
import React, { useState, useRef, useEffect, useContext } from 'react';
import "./DrinkBuilder.css";
import DrinkDisplay from "./DrinkDisplay.jsx";
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 DrinkBuilder = ({ score, day }) => {
    const [color, setColor] = useState([]);
    const [fillPercentage, setFillPercentage] = useState(0);
    const fillInterval = useRef(null);
    const [colorSelected, setColorSelected] = useState(false);
    const [cupSize, setCupSize] = useState("medium");
    const [confirmMessage, setConfirmMessage] = useState("");
    const [cupPlaced, setCupPlaced] = useState(false);
    const [cupPosition, setCupPosition] = useState(0);
    const [showStart, setShowStart] = useState(true);
 
    const drinkColors = [
        { name: "Blue", color: "#34C6F4" },
        { name: "Green", color: "#99CA3C" },
        { name: "Yellow", color: "#e2d700" },
        { name: "Red", color: "#FF0000" },
        { name: "Orange", color: "#F5841F" },
        { name: "Purple", color: "#7E69AF" },
    ];
 
    const { send } = useContext(WebSocketContext);
    const maxFill = 100;
    const fillAmount = 5;
    const fillRate = 175;
 
    useEffect(() => {
        setColor(null);
        setColorSelected(false);
        setFillPercentage(0);
    }, []);
 
    useEffect(() => {
        if (showStart) {
            const timer = setTimeout(() => {
                setShowStart(false);
            }, 5000);
            return () => clearTimeout(timer);
        }
    }, [showStart]);
 
    const startFilling = () => {
        Iif (!color || !cupPlaced) return;
        const fillingSound = playFillingSound();
        fillInterval.current = setInterval(() => {
            setFillPercentage(prev => {
                Iif (prev >= maxFill) {
                    clearInterval(fillInterval.current);
                    fillingSound.pause();
                    return maxFill;
                }
                return prev + fillAmount;
            });
        }, fillRate);
        fillInterval.sound = fillingSound;
    };
 
    const stopFilling = () => {
        Eif (fillInterval.current) clearInterval(fillInterval.current);
        Eif (fillInterval.sound) fillInterval.sound.pause();
    };
 
    const clearCup = () => {
        setColor(null);
        setFillPercentage(0);
        setColorSelected(false);
        setCupPlaced(false);
        setCupPosition(0);
    };
 
    const handleSend = () => {
        send({
            data: {
                type: "game_state",
                game_state_update_type: "order_component",
                component_type: "drink",
                component: {
                    color: color,
                    fill: fillPercentage,
                    size: cupSize,
                }
            }
        });
        clearCup();
        playSendSound();
        setConfirmMessage("Drink sent to manager!");
        setTimeout(() => setConfirmMessage(""), 3000);
    };
 
    const selectColor = (selectedColor) => {
        Iif (fillPercentage > 0) return;
        const selectedDrink = drinkColors.find(drink => drink.color === selectedColor);
        const selectedName = selectedDrink ? selectedDrink.name : "";
 
        setColor("");
        setTimeout(() => setColor(selectedColor), 50);
        setColorSelected(true);
        playPopSound();
        const audio = new Audio(`/audio/${selectedName.toLowerCase()}.mp3`);
        audio.play();
    };
 
    const selectCupSize = (size) => {
        Iif (fillPercentage > 0) return;
        setCupSize(size);
        setCupPlaced(true);
        playPopSound();
        const audio = new Audio(`/audio/${size.toLowerCase()}.mp3`);
        audio.play();
    };
 
    const playFillingSound = () => {
        const audio = new Audio("/audio/filling.mp3");
        audio.loop = true;
        audio.play();
        return audio;
    };
 
    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="Drink"
                    handleClick={() => setShowStart(false)}
                />
            )}
 
            {!showStart && (
                <Tutorial
                    classNames={[
                        "CupSizeContainer",
                        "DrinkButtonsContainer",
                        "FillCupButton",
                        "SendButton",
                    ]}
                    audioSourceFolder={"/audio/tutorial/drink"}
                />
            )}
 
            <div className="DrinkBuilder">
                <div className="TopMenuDrink">
                    <Score score={score} day={day} />
                </div>
 
                <div className="DrinkButtonsContainer">
                    {drinkColors.map((choice, index) => (
                        <div className="DrinkButtons" key={index}>
                            <button
                                onClick={() => {
                                    selectColor(choice.color);
                                    setCupPosition(-375 + index * 150);
                                }}
                                style={{
                                    backgroundColor: choice.color,
                                    color: "#FFFFFF",
                                    border: color === choice.color ? "3px solid black" : "none",
                                    WebkitTextStroke: "",
                                }}
                                disabled={fillPercentage > 0}
                            >
                                {choice.name}
                            </button>
                            <img src="/images/station_specific/Dispenser.png" alt="Dispenser" className="DispenserImage" />
                        </div>
                    ))}
                </div>
 
                <div className="MainContainer">
                    <div className="CupSizeContainer">
                        <button
                            className="CupSizeButtons"
                            onClick={() => selectCupSize("small")}
                            disabled={fillPercentage > 0}
                        >
                            <img src="/images/button_icons/SmallButton.png" alt="Small Cup" className="CupSizeImageSmall" />
                        </button>
                        <button
                            className="CupSizeButtons"
                            onClick={() => selectCupSize("medium")}
                            disabled={fillPercentage > 0}
                        >
                            <img src="/images/button_icons/MediumButton.png" alt="Medium Cup" className="CupSizeImageMedium" />
                        </button>
                        <button
                            className="CupSizeButtons"
                            onClick={() => selectCupSize("large")}
                            disabled={fillPercentage > 0}
                        >
                            <img src="/images/button_icons/LargeButton.png" alt="Large Cup" className="CupSizeImageLarge" />
                        </button>
                    </div>
 
                    <div className="DrinkDisplayContainer">
                        {cupPlaced && (
                            <DrinkDisplay
                                color={color}
                                fillPercentage={fillPercentage}
                                cupSize={cupSize}
                                cupPosition={cupPosition}
                            />
                        )}
                        <div className="ConfirmMessage">
                            {confirmMessage && <p>{confirmMessage}</p>}
                        </div>
                    </div>
 
                    <div className="ActionButtonsContainer">
                        <button className="ClearCupButton" onClick={() => { clearCup(); playPopSound(); }}>
                            <img src="/images/button_icons/clear_plate.png" alt="Clear Side" className="ClearSideImage" />
                            <p>Clear Cup</p>
                        </button>
 
                        <button
                            className="FillCupButton"
                            onMouseDown={startFilling}
                            onMouseUp={stopFilling}
                            onMouseLeave={stopFilling}
                            disabled={!cupPlaced || !colorSelected}
                            title="Press and hold to fill"
                        >
                            <img src="/images/button_icons/pouring.png" alt="Fill Cup" className="FillCupImage" />
                            <p>Fill Cup</p>
                        </button>
 
                        <button className="SendButton" onClick={() => { handleSend(); playPopSound(); }}>
                            Send
                        </button>
 
                        <button className="BottomButtons" onClick={() => { playPopSound(); handleRequestRepeat(); }}>
                            <img src="/images/button_icons/repeat_order.png" className="RepeatOrderImage" alt="Repeat" />
                            <p>Repeat Order</p>
                        </button>
                    </div>
                </div>
            </div>
        </>
    );
};
 
export default DrinkBuilder;