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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
<script>
import { run } from "svelte/legacy";
import { theme } from "$lib/store";
import { onMount, tick } from "svelte";
import { browser } from "$app/environment";
import Numpad from "$comp/Numpad.svelte";
import LocaleSelector from "$comp/LocaleSelector.svelte";
import Toggle from "$comp/Toggle.svelte";
import { locale, t } from "$lib/translations";
import { post, success, fail } from "$lib/utils";
import { page } from "$app/stores";
import { PUBLIC_VAPID_PUBKEY } from "$env/static/public";
let { data } = $props();
let { user } = $state(data);
let { connect, rates, subscriptions } = data;
let { currency, email, tip, verified } = $state(user);
let rate = rates[currency];
let fiats = Object.keys(rates).sort((a, b) => a.localeCompare(b));
let keypress = (e) => e.key === "Enter" && (e.preventDefault() || el.click());
let editingReserve = $state(),
editingThreshold = $state(),
doneReserve = $state(),
doneThreshold;
let doneEditing = () => {
editingReserve = false;
editingThreshold = false;
};
let editReserve = async () => {
editingReserve = true;
await tick();
reserveEl.focus();
};
let editThreshold = async () => {
editingThreshold = true;
await tick();
thresholdEl.focus();
};
if (!user.threshold) user.threshold = 1000000;
if (!user.reserve) user.reserve = 100000;
let reserveEl = $state(),
thresholdEl = $state();
let push = $state(),
pm,
subscription;
onMount(async () => {
if (!browser) return;
pm =
navigator?.serviceWorker &&
(await navigator.serviceWorker.getRegistration()).pushManager;
permission = await pm.permissionState({
userVisibleOnly: true,
applicationServerKey: PUBLIC_VAPID_PUBKEY,
});
if (permission === "granted") {
subscription = await pm.getSubscription();
if (subscriptions.includes(JSON.stringify(subscription))) push = true;
}
});
let permission = $state();
let updateNotifications = async (push) => {
if (!browser || !pm) return (push = false);
if (subscription && !push) {
return post("/subscription/delete", { subscription });
}
if (push && permission !== "denied") {
subscription = await pm.subscribe({
userVisibleOnly: true,
applicationServerKey: PUBLIC_VAPID_PUBKEY,
});
}
if (subscription) {
await post("/subscription", { subscription });
}
};
run(() => {
user.language = $locale;
});
run(() => {
updateNotifications(push);
});
$effect(() => {
if (email !== user.email) verified = false;
});
let revoke = () => {};
</script>
<div>
<label for="language" class="font-bold block mb-1"
>{$t("user.settings.locale")}</label
>
<LocaleSelector style="select-styles block py-3 w-full" />
</div>
<div>
<label for="currency" class="font-bold block mb-1"
>{$t("user.settings.localCurrency")}</label
>
<select name="currency" value={currency}>
{#each fiats as fiat}
<option value={fiat}>{fiat}</option>
{/each}
</select>
</div>
<div class="space-y-1 relative">
<label for="email" class="font-bold block">{$t("user.settings.email")}</label>
<p class="text-secondary">
{$t("user.settings.emailDescription")}
</p>
<label
class="input input-bordered border-primary input-lg rounded-2xl flex items-center gap-2"
>
<input type="text" name="email" class="clean" bind:value={email} />
{#if verified}
<iconify-icon
noobserver
icon="ph:check-bold"
class="text-success ml-auto"
width="32"
></iconify-icon>
{:else if email}
<iconify-icon
noobserver
icon="ph:clock-bold"
class="text-warning ml-auto"
width="32"
></iconify-icon>
{/if}
</label>
</div>
<div>
<div class="flex justify-between items-center">
<span class="font-bold">{$t("user.settings.notifications")}</span>
<Toggle id="notify" bind:value={user.notify} />
</div>
<p class="text-secondary mt-1 w-9/12">
{$t("user.settings.notificationsDesc")}
</p>
</div>
<div>
<div class="flex justify-between items-center">
<span class="font-bold">{$t("user.settings.pushNotifications")}</span>
{#if permission !== "denied"}
<Toggle id="push" bind:value={push} />
{/if}
</div>
<p class="text-secondary mt-1 w-9/12">
{#if permission === "denied"}
{$t("user.settings.pushNotificationsDisabled")}
{:else}
{$t("user.settings.pushNotificationsDesc")}
{/if}
</p>
</div>
<div>
<div class="flex justify-between items-center">
<span class="font-bold">{$t("user.settings.tipPrompt")}</span>
<Toggle id="prompt" bind:value={user.prompt} />
</div>
<p class="text-secondary mt-1 w-9/12">
{$t("user.settings.tipPromptDescription")}
</p>
</div>
<div class="space-y-1 relative">
<label for="tip" class="font-bold block">{$t("user.settings.tip")}</label>
<p class="text-secondary">
{$t("user.settings.tipDesc")}
</p>
<label
class="input input-bordered border-primary input-lg rounded-2xl flex items-center gap-2"
>
<input type="text" name="tip" class="clean" bind:value={tip} />
</label>
</div>
<div>
<div class="flex justify-between items-center">
<span class="font-bold">{$t("user.settings.autoWithdraw")}</span>
<Toggle id="autowithdraw" bind:value={user.autowithdraw} />
</div>
<p class="text-secondary mt-1 w-9/12">
{$t("user.settings.autoWithdrawDescription")}
</p>
</div>
<div class:hidden={!user.autowithdraw}>
<div class="mb-2">
<label for="display" class="font-bold mb-1 block"
>{$t("user.settings.destination")}</label
>
<textarea
name="destination"
placeholder={$t("user.settings.destinationPlaceholder")}
onkeypress={keypress}
class="w-full p-4 border rounded-xl h-48"
bind:value={user.destination}
></textarea>
</div>
<div>
<label for="display" class="font-bold mb-1 block"
>{$t("user.settings.threshold")}</label
>
<button type="button" class="flex w-full" onclick={editThreshold}>
<div class="p-4 border rounded-2xl rounded-r-none border-r-0 bg-base-200">
<iconify-icon
noobserver
icon="ph:lightning-fill"
class="text-yellow-300"
></iconify-icon>
</div>
<div
class="border-l-0 rounded-l-none pl-2 w-full p-4 border rounded-2xl text-left"
>
{user.threshold}
</div>
<input type="hidden" name="threshold" bind:value={user.threshold} />
</button>
<p class="text-secondary mt-1">
{$t("user.settings.thresholdDesc")}
</p>
</div>
<div>
<label for="display" class="font-bold mb-1 block"
>{$t("user.settings.reserve")}</label
>
<button type="button" class="flex w-full" onclick={editReserve}>
<div class="p-4 border rounded-2xl rounded-r-none border-r-0 bg-base-200">
<iconify-icon
noobserver
icon="ph:lightning-fill"
class="text-yellow-300"
></iconify-icon>
</div>
<div
class="border-l-0 rounded-l-none pl-2 w-full p-4 border rounded-2xl text-left"
>
{user.reserve}
</div>
<input type="hidden" name="reserve" bind:value={user.reserve} />
</button>
<p class="text-secondary mt-1">
{$t("user.settings.reserveDesc")}
</p>
</div>
</div>
{#if editingThreshold}
<div
class="fixed bg-base-100 bg-opacity-90 inset-0 overflow-y-auto h-full w-full z-50 max-w-lg mx-auto"
>
<div class="relative p-5 border shadow-lg rounded-md bg-base-100 space-y-5">
<h1 class="text-center text-2xl font-semibold">
{$t("user.settings.threshold")}
</h1>
<Numpad
bind:amount={user.threshold}
{currency}
bind:rate
bind:submit={doneReserve}
bind:element={thresholdEl}
/>
<button
bind:this={doneReserve}
type="button"
onclick={doneEditing}
class="btn">Ok</button
>
</div>
</div>
{/if}
{#if editingReserve}
<div
class="fixed bg-base-100 bg-opacity-90 inset-0 overflow-y-auto h-full w-full z-50 mx-auto max-w-lg"
>
<div
class="relative mx-auto p-5 border shadow-lg rounded-md bg-base-100 space-y-5 text-center"
>
<h1 class="text-2xl font-semibold">
{$t("user.settings.reserve")}
</h1>
<Numpad
bind:amount={user.reserve}
bind:currency
bind:rate
bind:submit={doneReserve}
bind:element={reserveEl}
/>
<button
bind:this={doneReserve}
type="button"
onclick={doneEditing}
class="btn">Ok</button
>
</div>
</div>
{/if}
{#if connect !== "connected"}
<a href={connect} class="btn flex">
<img src="/images/square.svg" class="w-12" />
<div>{$t("user.settings.connectSquare")}</div></a
>
{:else}
<button type="button" onclick={revoke} class="btn">
<img
src="/images/square.svg"
class="w-12"
class:invert={$theme === "dark"}
/>
<div>{$t("user.settings.revokeSquare")}</div>
</button>
{/if}
|