summaryrefslogtreecommitdiffstats
path: root/src/components/Timer.tsx
blob: 61a98f5a55ff9047bb493e2993a2fae014ebfa4e (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
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
import { createSignal, For, Match, Show, Switch } from "solid-js";
import styles from "./Timer.module.css";

export const TIME_UNITS = ["s", "m", "h", "d"] as const;
export type Timer = {
  title: string;
  options: {
    time: number;
    unit: (typeof TIME_UNITS)[number];
  }[];
  backgroundColor: string;
  onTill: number | null;
  secondsLeftText: string;
  extraStopActions: { title: string }[];
};

export default function Timer(props: Timer) {
  const [onTill, setOnTill] = createSignal(props.onTill);
  const [unit, setUnit] = createSignal<(typeof TIME_UNITS)[number]>(
    TIME_UNITS[1],
  );
  const [formattedTime, setFormattedTime] = createSignal("");
  const [disabled, setDisabled] = createSignal(onTill() !== null);
  const [customValue, setCustomValue] = createSignal<number | "">("");

  let _intervalId = null;
  function toggleAutoUpdate() {
    if (_intervalId === null) {
      updateTime();
      _intervalId = setInterval(updateTime, 1000);
    } else {
      clearInterval(_intervalId);
      _intervalId = null;
    }
  }
  if (props.onTill !== null) {
    toggleAutoUpdate();
  }
  function updateTime() {
    const s = Math.floor(onTill() - Date.now() / 1000);
    const days = Math.floor(s / (24 * 3600));
    const hours = Math.floor((s % (24 * 3600)) / 3600);
    const minutes = Math.floor((s % 3600) / 60);
    const seconds = s % 60;

    let text = "";
    if (days > 0) {
      text += days + "d : ";
    }
    if (hours > 0 || text.length > 0) {
      text += hours + "h : ";
    }
    if (minutes > 0 || text.length > 0) {
      text += minutes + "m : ";
    }
    if (seconds > 0 || text.length > 0) {
      text += seconds + "s";
    }
    setFormattedTime(text);
  }
  function startStopClick() {
    const val = customValue();
    if (disabled() === false && val !== "") {
      optionClick({ time: val, unit: unit() });
    } else if (disabled() === true) {
      setDisabled(false);
      setOnTill(null);
      toggleAutoUpdate();
    }
  }

  function optionClick(option: Timer["options"][number]) {
    setDisabled(true);
    let newOnTill = Math.floor(Date.now() / 1000);
    if (option.unit === "s") {
      newOnTill += option.time;
    } else if (option.unit === "m") {
      newOnTill += option.time * 60;
    } else if (option.unit === "h") {
      newOnTill += option.time * 3600;
    } else {
      newOnTill += option.time * 3600 * 24;
    }
    setOnTill(newOnTill);
    toggleAutoUpdate();
  }

  function extraStopClick(act: Timer["extraStopActions"][number]) {
    // Some api call for the extra
    startStopClick();
  }
  return (
    <div class={styles.cont}>
      <h2>{props.title}</h2>

      <div class={styles.customOption}>
        <input
          type="number"
          value={customValue()}
          on:input={(e) =>
            setCustomValue(e.target.value ? parseFloat(e.target.value) : "")
          }
          disabled={disabled()}
          min="1"
        />
        <div class={styles.units}>
          <For each={TIME_UNITS}>
            {(u) => (
              <span
                on:click={() => (disabled() ? null : setUnit(u))}
                classList={{
                  [styles.selectedUnit]: unit() === u,
                  [styles.notSelectedUnit]: unit() !== u,
                  [styles.darkgray]: disabled() === true,
                }}
              >
                {u}
              </span>
            )}
          </For>
        </div>
        <button
          class={styles.customOptionBtn}
          on:click={startStopClick}
          disabled={
            disabled()
              ? false
              : customValue() === "" || (customValue() as number) <= 0
          }
          style={{ "background-color": disabled() ? "red" : "green" }}
        >
          {disabled() ? "Wyłącz" : "Włącz"}
        </button>
        {/*<Show when={disabled() === true}>*/}
        <For each={props.extraStopActions}>
          {(act) => (
            <button
              disabled={disabled() === false}
              on:click={[extraStopClick, act]}
              class={styles.extraStopBtn}
            >
              {act.title}
            </button>
          )}
        </For>
        {/*</Show>*/}
      </div>

      <div class={styles.timeLeft}>
        <Switch>
          <Match when={onTill() === null}>
            <span>Wyłączone</span>
            <h2> </h2>
          </Match>
          <Match when={onTill() > 0}>
            <span>{props.secondsLeftText} </span>
            <h2>{formattedTime()}</h2>
          </Match>
        </Switch>
      </div>

      <div class={styles.options}>
        <For each={props.options}>
          {(option) => (
            <button
              class={styles.option}
              on:click={[optionClick, option]}
              style={{ "background-color": props.backgroundColor }}
              disabled={disabled()}
            >
              {option.time} {option.unit}
            </button>
          )}
        </For>
      </div>
    </div>
  );
}