All files / src/component/taskButton TaskButton.jsx

47.16% Statements 25/53
25.71% Branches 9/35
28.57% Functions 4/14
47.16% Lines 25/53

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        3x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x 37x   37x 3x     37x 1x 1x 1x 1x       37x                                                                                                       37x         37x       37x       37x                   1x                                                                                                                          
import React, { useState } from 'react';
import './TaskButton.css';
import TaskDialog from './TaskDialog';
 
const TaskButton = ({ onAddHabit, onAddDaily, onAddTodo, onAddReward }) => {
    const [showMenu, setShowMenu] = useState(false);
    const [showDialog, setShowDialog] = useState(false);
    const [dialogTitle, setDialogTitle] = useState('');
    const [habitTitle, setHabitTitle] = useState('');
    const defaultTitle = 'Add a title';
    const [habitNotes, setHabitNotes] = useState('');
    const defaultNotes = 'Add notes';
    const [rewardPrice, setRewardPrice] = useState();
    const defaultPrice = 10;
    const [positive, setPositive] = useState(true);
    const [negative, setNegative] = useState(true);
 
    const handleButtonClick = () => {
        setShowMenu(!showMenu);
    };
 
    const handleMenuItemClick = (menuItem) => {
        console.log(`Clicked on ${menuItem}`);
        setShowMenu(false);
        setShowDialog(true);
        setDialogTitle(`Create ${menuItem}`);
    };
 
    // TODO: createXX logic
    const handleCreateTask = () => {
        console.log('Creating...');
        let createFunction;
        let newItem;
 
        switch (dialogTitle) {
            case 'Create Habit':
                createFunction = onAddHabit;
                newItem = {
                    id: Date.now(),
                    content: habitTitle || defaultTitle,
                    notes: habitNotes || defaultNotes,
                    positive: positive,
                    negative: negative
                };
                break;
            case 'Create Daily':
                createFunction = onAddDaily;
                newItem = {
                    id: Date.now(),
                    content: habitTitle || defaultTitle,
                    notes: habitNotes || defaultNotes,
                    completed: false
                };
                break;
            case 'Create To Do':
                createFunction = onAddTodo;
                newItem = {
                    id: Date.now(),
                    content: habitTitle || defaultTitle,
                    notes: habitNotes || defaultNotes,
                    completed: false
                };
                break;
            case 'Create Reward':
                createFunction = onAddReward;
                newItem = {
                    id: Date.now(),
                    content: habitTitle || defaultTitle,
                    notes: habitNotes || defaultNotes,
                    price: rewardPrice || defaultPrice
                };
                break;
            default:
                console.warn('Unknown task type:', dialogTitle);
                return;
        }
 
        createFunction(newItem);
        setShowDialog(false);
    };
 
    const handleCancel = () => {
        console.log('Canceling...');
        setShowDialog(false);
    }
 
    const handlePositive = () => {
        setPositive(!positive);
    };
 
    const handleNegative = () => {
        setNegative(!negative);
    };
 
    return (
        <div className="task-button-container">
            <div className="add-task-button-container" >
                <button className="add-task-button" onClick={handleButtonClick}>
                    Add Task
                </button>
            </div>
 
            {showMenu && (
                <div className="menu-container">
                    <button className="menu-item" onClick={() => handleMenuItemClick('Habit')}>
                        <img src="/habit.png" alt="Habit" className="menu-icon" />
                        Habit
                    </button>
                    <button className="menu-item" onClick={() => handleMenuItemClick('Daily')}>
                        <img src="/daily.png" alt="Daily" className="menu-icon" />
                        Daily
                    </button>
                    <button className="menu-item" onClick={() => handleMenuItemClick('To Do')}>
                        <img src="/todo.png" alt="To Do" className="menu-icon" />
                        To Do
                    </button>
                    <button className="menu-item" onClick={() => handleMenuItemClick('Reward')}>
                        <img src="/reward.png" alt="Reward" className="menu-icon" />
                        Reward
                    </button>
 
                </div>
            )}
 
            {showDialog && (
                <div>
                    <div className="overlay"></div>
                    <TaskDialog title={dialogTitle} onCancel={handleCancel} onClick={handleCreateTask}>
                        <div className="input-container">
                            <label>Title*:</label>
                            <br />
                            <input type="text" className="habit-input" value={habitTitle} placeholder={defaultTitle} onChange={(e) => setHabitTitle(e.target.value)} />
                        </div>
                        <div className="input-container">
                            <label>Notes:</label>
                            <br />
                            <textarea className="habit-input" value={habitNotes} placeholder={defaultNotes} onChange={(e) => setHabitNotes(e.target.value)} />
                        </div>
                        {dialogTitle === 'Create Reward' && (
                            <div className="input-container">
                                <label>Price:</label>
                                <br />
                                <input type="number" className="habit-input" value={rewardPrice} placeholder={defaultPrice} onChange={(e) => setRewardPrice(e.target.value)} />
                            </div>)}
 
                        {dialogTitle === 'Create Habit' && (
                            <div>
                                <div className="dialog-buttons-circle">
                                    <button className={`round-button ${positive ? 'selected' : 'unselected'}`} onClick={handlePositive}>+</button>
                                    <button className={`round-button ${negative ? 'selected' : 'unselected'}`} onClick={handleNegative}>-</button>
                                </div>
                                <div className="dialog-description">
                                    <span>Positive</span>
                                    <span>Negative</span>
                                </div>
                            </div>
                        )}
                    </TaskDialog>
                </div>
            )}
        </div>
    );
};
 
export default TaskButton;