aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/funder/+page.svelte
blob: e6549360dd0a9b0f9b81416c8def68cd45e69f10 (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
<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.Capacitor?.Plugins?.NfcWriter;
    ready = true;
    console.log(window.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',
            },
          ],
        },
        { signal: controller.signal }
      );

      success = true;
    } catch (err) {
      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}