summaryrefslogtreecommitdiff
path: root/packages/merchant-backoffice-ui/src/components/picker/DurationPicker.tsx
blob: 7c1c172aca2557c0a2808d4afe6c772c0071b722 (plain)
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
/*
 This file is part of GNU Taler
 (C) 2021-2024 Taler Systems S.A.

 GNU Taler is free software; you can redistribute it and/or modify it under the
 terms of the GNU General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.

 GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with
 GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

/**
 *
 * @author Sebastian Javier Marchano (sebasjm)
 */

import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { h, VNode } from "preact";
import { useState } from "preact/hooks";
import "../../scss/DurationPicker.scss";

export interface Props {
  hours?: boolean;
  minutes?: boolean;
  seconds?: boolean;
  days?: boolean;
  onChange: (value: number) => void;
  value: number;
}

// inspiration taken from https://github.com/flurmbo/react-duration-picker
export function DurationPicker({
  days,
  hours,
  minutes,
  seconds,
  onChange,
  value,
}: Props): VNode {
  const ss = 1000;
  const ms = ss * 60;
  const hs = ms * 60;
  const ds = hs * 24;
  const { i18n } = useTranslationContext();

  return (
    <div class="rdp-picker">
      {days && (
        <DurationColumn
          unit={i18n.str`days`}
          max={99}
          value={Math.floor(value / ds)}
          onDecrease={value >= ds ? () => onChange(value - ds) : undefined}
          onIncrease={value < 99 * ds ? () => onChange(value + ds) : undefined}
          onChange={(diff) => onChange(value + diff * ds)}
        />
      )}
      {hours && (
        <DurationColumn
          unit={i18n.str`hours`}
          max={23}
          min={1}
          value={Math.floor(value / hs) % 24}
          onDecrease={value >= hs ? () => onChange(value - hs) : undefined}
          onIncrease={value < 99 * ds ? () => onChange(value + hs) : undefined}
          onChange={(diff) => onChange(value + diff * hs)}
        />
      )}
      {minutes && (
        <DurationColumn
          unit={i18n.str`minutes`}
          max={59}
          min={1}
          value={Math.floor(value / ms) % 60}
          onDecrease={value >= ms ? () => onChange(value - ms) : undefined}
          onIncrease={value < 99 * ds ? () => onChange(value + ms) : undefined}
          onChange={(diff) => onChange(value + diff * ms)}
        />
      )}
      {seconds && (
        <DurationColumn
          unit={i18n.str`seconds`}
          max={59}
          value={Math.floor(value / ss) % 60}
          onDecrease={value >= ss ? () => onChange(value - ss) : undefined}
          onIncrease={value < 99 * ds ? () => onChange(value + ss) : undefined}
          onChange={(diff) => onChange(value + diff * ss)}
        />
      )}
    </div>
  );
}

interface ColProps {
  unit: string;
  min?: number;
  max: number;
  value: number;
  onIncrease?: () => void;
  onDecrease?: () => void;
  onChange?: (diff: number) => void;
}

function InputNumber({
  initial,
  onChange,
}: {
  initial: number;
  onChange: (n: number) => void;
}) {
  const [value, handler] = useState<{ v: string }>({
    v: toTwoDigitString(initial),
  });

  return (
    <input
      value={value.v}
      onBlur={(e) => onChange(parseInt(value.v, 10))}
      onInput={(e) => {
        e.preventDefault();
        const n = Number.parseInt(e.currentTarget.value, 10);
        if (isNaN(n)) return handler({ v: toTwoDigitString(initial) });
        return handler({ v: toTwoDigitString(n) });
      }}
      style={{
        width: 50,
        border: "none",
        fontSize: "inherit",
        background: "inherit",
      }}
    />
  );
}

function DurationColumn({
  unit,
  min = 0,
  max,
  value,
  onIncrease,
  onDecrease,
  onChange,
}: ColProps): VNode {
  const cellHeight = 35;
  return (
    <div class="rdp-column-container">
      <div class="rdp-masked-div">
        <hr class="rdp-reticule" style={{ top: cellHeight * 2 - 1 }} />
        <hr class="rdp-reticule" style={{ top: cellHeight * 3 - 1 }} />

        <div class="rdp-column" style={{ top: 0 }}>
          <div class="rdp-cell" key={value - 2}>
            {onDecrease && (
              <button
                style={{ width: "100%", textAlign: "center", margin: 5 }}
                onClick={onDecrease}
              >
                <span class="icon">
                  <i class="mdi mdi-chevron-up" />
                </span>
              </button>
            )}
          </div>
          <div class="rdp-cell" key={value - 1}>
            {value > min ? toTwoDigitString(value - 1) : ""}
          </div>
          <div class="rdp-cell rdp-center" key={value}>
            {onChange ? (
              <InputNumber
                initial={value}
                onChange={(n) => onChange(n - value)}
              />
            ) : (
              toTwoDigitString(value)
            )}
            <div>{unit}</div>
          </div>

          <div class="rdp-cell" key={value + 1}>
            {value < max ? toTwoDigitString(value + 1) : ""}
          </div>

          <div class="rdp-cell" key={value + 2}>
            {onIncrease && (
              <button
                style={{ width: "100%", textAlign: "center", margin: 5 }}
                onClick={onIncrease}
              >
                <span class="icon">
                  <i class="mdi mdi-chevron-down" />
                </span>
              </button>
            )}
          </div>
        </div>
      </div>
    </div>
  );
}

function toTwoDigitString(n: number) {
  if (n < 10) {
    return `0${n}`;
  }
  return `${n}`;
}