aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/funder/+page.svelte
blob: 09b25d2d3abcd4f141c8d1d13dbe3090e249e002 (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
<script lang="ts">
  import { onMount } from "svelte";
  let here = false;
  let ready = false;
  let success = false;
  let controller: AbortController | null = null;
  let NfcWriter: any;

  async function write() {
    NfcWriter = (window as any).Capacitor?.Plugins?.NfcWriter;
    ready = true;
    console.log((window as any).Capacitor);
    if (!NfcWriter) return;
    here = true;

    try {
      await NfcWriter.enableExclusiveNfcMode();

      if (controller) controller.abort();
      controller = new AbortController();

      ready = true;

      const ndef = new NDEFReader();
      await ndef.write({
        records: [
          {
            recordType: "url",
            data: "https://coinos.io",
          },
        ],
      });

      success = true;
    } catch (err: any) {
      if (err.name === "AbortError") {
        console.warn("Previous write aborted");
      } else {
        console.error("Write failed", err);
        alert("❌ Write failed: " + err.message);
      }
    } finally {
      await NfcWriter.disableExclusiveNfcMode();
    }
  }
</script>

HI
<button class="p-4 btn" on:click={write}>write</button>
{#if here}HERE{/if}
{#if ready}go{/if}
{#if success}done{/if}