All files / src/components/IngredientScrollPicker IngredientScrollPicker.jsx

100% Statements 18/18
50% Branches 1/2
100% Functions 5/5
100% Lines 16/16

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      2x   2x 11x   11x 10x 5x     11x 1x 1x 1x     11x 1x 1x 1x     11x   11x                        
import React, { useState, useEffect } from 'react';
import './IngredientScrollPicker.css';
import { menu } from "../../menuItems";
const burgerOptions = menu[0].children;
 
const IngredientScrollPicker = ({ selected, setSelected }) => {
    const [currentIndex, setCurrentIndex] = useState(0);
 
    useEffect(() => {
        const index = burgerOptions.findIndex(item => item.name === selected);
        Eif (index !== -1) setCurrentIndex(index);
    }, [selected]);
 
    const scrollUp = () => {
        const newIndex = (currentIndex - 1 + burgerOptions.length) % burgerOptions.length;
        setCurrentIndex(newIndex);
        setSelected(burgerOptions[newIndex].name);
    };
 
    const scrollDown = () => {
        const newIndex = (currentIndex + 1) % burgerOptions.length;
        setCurrentIndex(newIndex);
        setSelected(burgerOptions[newIndex].name);
    };
 
    const currentItem = burgerOptions[currentIndex];
 
    return (
        <div className="scroll-picker">
            <button className="scroll-button" onClick={scrollUp}>▲</button>
            <div className="selected-image">
                <img src={currentItem.image} alt={currentItem.name} />
            </div>
            <button className="scroll-button" onClick={scrollDown}>▼</button>
        </div>
    );
};
 
export default IngredientScrollPicker;