aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Lnurlp.svelte
blob: d2e7720e6545c6b5e0c660331ac05e1f493afed8 (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
<script lang="ts">
  import { t } from "$lib/translations";
  import { enhance } from "$app/forms";
  import Numpad from "$comp/Numpad.svelte";
  import Spinner from "$comp/Spinner.svelte";
  import { pin } from "$lib/store";
  import { loc } from "$lib/utils";

  let { data, form, send = $bindable(), comment = $bindable() } = $props();
  let currency = $derived(data.user.currency);
  let locale = $derived(loc(data.user));
  let minSendable = $derived(data.minSendable);
  let maxSendable = $derived(data.maxSendable);
  let commentAllowed = $derived(data.commentAllowed);
  let callback = $derived(data.callback);
  let metadata = $derived(data.metadata);
  let rate = $derived(data.rate);

  let amount = $state((() => Math.round(data.minSendable / 1000))()),
    loading = $state();
  let submit = () => (loading = true);
</script>

<div class="container px-4 mt-20 max-w-xl mx-auto space-y-2">
  <div class="text-center mb-8">
    {#each JSON.parse(metadata) as m}
      <div>
        {#if m[0] === "text/plain"}
          <h1 class="text-3xl md:text-4xl font-semibold mb-2">{m[1]}</h1>
        {/if}
        {#if m[0].includes("image")}
          <img src={`data:image/png;base64,${m[1]}`} alt="Recipient" />
        {/if}
      </div>
    {/each}
  </div>

  {#if form?.error}
    <div class="text-red-600 text-center mb-5">{form.error}</div>
  {/if}

  <Numpad bind:amount {rate} {currency} {locale} bind:submit={send} />

  <form action="?/pay" method="POST" use:enhance onsubmit={submit}>
    <input name="amount" value={amount} type="hidden" />
    <input name="minSendable" value={minSendable} type="hidden" />
    <input name="maxSendable" value={maxSendable} type="hidden" />
    <input name="callback" value={callback} type="hidden" />
    <input name="pin" value={$pin} type="hidden" />

    {#if commentAllowed}
      <textarea
        name="comment"
        placeholder={$t("payments.message")}
        class="w-full p-4 border rounded-xl h-32 text-xl"
        bind:value={comment}
        autocapitalize="none"
      ></textarea>
    {/if}

    <div class="flex w-full">
      <button bind:this={send} type="submit" class="btn">
        {#if loading}
          <Spinner />
        {:else}
          {$t("payments.send")}
        {/if}
      </button>
    </div>
  </form>
</div>