aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/AppForm.svelte
blob: 76f703f1f81198bcc8f9c8d1942c51dba02b4e65 (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
<script>
  import { browser } from "$app/environment";
  import { applyAction, deserialize } from "$app/forms";
  import { page } from "$app/stores";
  import AmountField from "$comp/AmountField.svelte";
  import Numpad from "$comp/Numpad.svelte";
  import Toggle from "$comp/Toggle.svelte";
  import { getPublicKey } from "nostr-tools";
  import { onMount, tick } from "svelte";
  import { bytesToHex, randomBytes } from "@noble/hashes/utils";
  import { goto } from "$app/navigation";
  import { copy, focus, fail, post } from "$lib/utils";
  import { t } from "$lib/translations";
  import { enhance } from "$app/forms";
  import {
    PUBLIC_COINOS_PUBKEY as walletPubkey,
    PUBLIC_COINOS_RELAY as relayUrl,
  } from "$env/static/public";

  let {
    rate,
    user,
    name,
    max_fee,
    max_amount,
    budget_renewal,
    pubkey,
    secret,
    notify,
  } = $props();
  let { currency } = $derived(user);

  onMount(() => {
    if (!pubkey) secret || generate();
    if (!budget_renewal) budget_renewal = "never";
  });

  let generate = () => {
    secret = bytesToHex(randomBytes(32));
    pubkey = getPublicKey(secret);
  };

  let lud16 = $derived(`${user.username}@${$page.url.host}`);

  let el = $state();
  let submit = async (e) => {
    e.preventDefault();
    let body = new FormData(el);
    let params = { method: "POST", body };
    let url = $page.url.pathname;

    const response = await fetch(url, params);
    const result = deserialize(await response.text());

    if (result.type === "redirect") {
      let type = "nwc:success";
      let msg = { relayUrl, lud16, walletPubkey, type };
      if (browser && window.opener) window.opener.postMessage(msg, "*");
    }

    applyAction(result);
  };

  let del = async () => {
    try {
      await post(`/post/apps/delete`, { pubkey });
      goto("/settings/nostr");
    } catch (e) {
      fail(e.message);
    }
  };
</script>

<form method="POST" class="space-y-5" onsubmit={submit} bind:this={el}>
  <div>
    <label for="name" class="font-bold mb-1 block">{$t("accounts.name")}</label>
    <input type="text" name="name" bind:value={name} use:focus />
  </div>

  <AmountField
    {currency}
    {rate}
    bind:amount={max_amount}
    name={"max_amount"}
    label={$t("accounts.budget")}
  />

  <AmountField
    none="1%"
    {currency}
    {rate}
    bind:amount={max_fee}
    name={"max_fee"}
    label={$t("accounts.maxfee")}
  />

  <div>
    <label for="name" class="font-bold mb-1 block"
      >{$t("accounts.renewal")}</label
    >
    <select name="budget_renewal" bind:value={budget_renewal}>
      <option value="never" selected>{$t("accounts.never")}</option>
      <option value="daily">{$t("accounts.daily")}</option>
      <option value="weekly">{$t("accounts.weekly")}</option>
      <option value="monthly">{$t("accounts.monthly")}</option>
      <option value="yearly">{$t("accounts.yearly")}</option>
    </select>
  </div>

  <div>
    <label for="notify" class="font-bold mb-1 block"
      >{$t("accounts.notifications")}</label
    >
    <select name="notify" bind:value={notify}>
      <option value={false} selected>{$t("accounts.off")}</option>
      <option value={true}>{$t("accounts.on")}</option>
    </select>
  </div>

  <div>
    <label for="pubkey" class="font-bold mb-1 block"
      >{$t("accounts.pubkey")}</label
    >
    <div class="flex gap-1 items-center">
      <textarea
        rows={3}
        name="pubkey"
        bind:value={pubkey}
        class="grow"
        readonly={secret}
      />
      <button
        type="button"
        onclick={() => copy(pubkey)}
        class="btn !w-auto shrink"
      >
        <iconify-icon icon="ph:copy-bold" width="32"></iconify-icon>
      </button>
    </div>
  </div>

  {#if !pubkey || secret}
    <div>
      <label for="secret" class="font-bold mb-1 block"
        >{$t("accounts.secret")}</label
      >
      <div class="flex gap-1 items-center">
        <textarea rows={3} name="secret" bind:value={secret} lcass="grow" />

        <div class="space-y-2 w-24">
          <button type="button" onclick={generate} class="btn">
            <iconify-icon noobserver icon="ph:dice-three-bold" width="32"
            ></iconify-icon>
          </button>
          <button type="button" onclick={() => copy(secret)} class="btn">
            <iconify-icon icon="ph:copy-bold" width="32"></iconify-icon>
          </button>
        </div>
      </div>
    </div>
  {/if}

  <div class="space-y-2">
    <button type="submit" class="btn btn-accent">{$t("payments.submit")}</button
    >
    {#if pubkey}
      <button type="button" class="btn" onclick={del}>
        <iconify-icon icon="ph:trash-bold" width="32"></iconify-icon>
        {$t("accounts.delete")}</button
      >
    {/if}
  </div>
</form>