aboutsummaryrefslogtreecommitdiff
path: root/src/components/Timers.js
blob: 10b2557a73f81c840f71d89a9132e8a17de8aa4b (plain) (blame)
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
import Timer from "./Timer";

const Timers = ({ items, setTimers }) => {
    const setTime = (idx, time) => {
        setTimers(items.map((t, id) => id !== idx ? t : { ...t, time: time, initialTime: time }));
    };
    const initialTimer = (idx) => {
        setTimers(items.map((t, id) => id !== idx ? t : { ...t, started: true, time: t.initialTime, startedAt: (new Date()).getTime() }));
    };
    const stopTimer = (idx) => {
        setTimers(items.map((t, id) => id !== idx ? t : { ...t, started: false }));
    };
    const setTimerName = (idx, name) => {
        setTimers(items.map((t, id) => id !== idx ? t : { ...t, name }));
    };
    const deleteTimer = (idx) => {
        setTimers(items.filter((t, id) => id !== idx));
    };
    const moveTimer = (idx, offset) => {
        [items[idx], items[idx + offset]] = [items[idx + offset], items[idx]];
        setTimers(items);
    };
    return items.map((t, idx) => <Timer
        key={`item-${idx}`}
        index={idx}
        first={idx === 0}
        last={idx === (items.length-1)}
        name={t.name}
        started={t.started}
        time={t.time || 0}
        initialTime={t.initialTime || 0}
        setTime={(time) => setTime(idx, time)}
        start={() => initialTimer(idx)}
        stop={() => stopTimer(idx)}
        setName={(name) => setTimerName(idx, name)}
        remove={() => deleteTimer(idx)}
        moveTimer={(offset) => moveTimer(idx, offset)}
    />);
}

export default Timers;