aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/settings/+layout.svelte
blob: edf256abaadbb37b7e82246697243029464a827c (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
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
<script>
  import { run, preventDefault } from "svelte/legacy";
  import { setContext } from "svelte";

  import { browser } from "$app/environment";
  import { onMount, tick } from "svelte";
  import { fly } from "svelte/transition";
  import { applyAction, deserialize } from "$app/forms";

  import Icon from "$comp/Icon.svelte";
  import Spinner from "$comp/Spinner.svelte";
  import Pin from "$comp/Pin.svelte";
  import { loading, t } from "$lib/translations";
  import { fail, auth, post, sleep, warning, success } from "$lib/utils";
  import { avatar, banner, password, pin, save } from "$lib/store";
  import { upload } from "$lib/upload";
  import { page } from "$app/stores";
  import { sign, send, reEncryptEntropy, setNsec } from "$lib/nostr";
  import { invalidateAll } from "$app/navigation";
  import { PUBLIC_COINOS_URL } from "$env/static/public";

  let { data, form } = $props();

  let formElement = $state();

  let { user, token, rates, cookies, subscriptions } = $state(data);
  let { tab } = $derived(data);
  let prev = $derived({ ...user });

  let justUpdated;
  let throttledSuccess = () => {
    if (!justUpdated) {
      justUpdated = true;
      success($t("user.settings.saved"), false);
      setTimeout(() => (justUpdated = false), 5000);
    }
  };

  let tabs = [
    { name: "account", key: "ACCOUNT" },
    { name: "preferences", key: "POINT_OF_SALE" },
    { name: "security", key: "SECURITY" },
    { name: "shopify", key: "SHOPIFY" },
  ];

  let { address, id, username } = user;
  let submitting = $state();

  async function handleSubmit() {
    try {
      submitting = true;
      let data = new FormData(formElement);

      if (data.get("newNsec")) {
        await setNsec(user, data.get("newNsec"));
        data.set("nsec", user.nsec);
        data.set("pubkey", user.pubkey);
      }

      if (data.get("password")) {
        try {
          data.set(
            "cipher",
            await reEncryptEntropy(user, data.get("password")),
          );
        } catch (e) {
          console.log("Failed to encrypt keys with new password");
          throw e;
        }
      }

      if ($avatar) {
        try {
          let { hash } = JSON.parse(
            await upload($avatar.file, $avatar.type, $avatar.progress, token),
          );

          data.set("profile", hash);

          await fetch(`/api/public/${hash}.webp`, {
            cache: "reload",
            mode: "no-cors",
          });
        } catch (e) {
          console.log("problem upsubmitting avatar", e);
        }
      }

      if ($banner) {
        try {
          let { hash } = JSON.parse(
            await upload($banner.file, $banner.type, $banner.progress, token),
          );

          data.set("banner", hash);

          await fetch(`/api/public/${hash}.webp`, {
            cache: "reload",
            mode: "no-cors",
          });
        } catch (e) {
          console.log("problem upsubmitting banner", e);
        }
      }

      if (["username", "address", "profile"].some((a) => user[a] !== prev[a])) {
        let event = {
          pubkey: user.pubkey,
          created_at: Math.floor(Date.now() / 1000),
          kind: 0,
          content: JSON.stringify({
            name: user.username,
            about: user.address,
            picture: `${$page.url.origin}/public/${user.profile}.webp`,
          }),
          tags: [],
        };

        try {
          await sign({ event, user });
          await send(event);
        } catch (e) {
          warning("Nostr profile could not be updated");
        }
      }

      if (user.email !== prev.email) {
        try {
          cookies.get = function (n) {
            return this.find((c) => c.name === n).value;
          };

          user.verified = false;

          await post(
            "/api/request",
            { id: user.id, email: user.email },
            auth(cookies),
          );

          warning($t("user.settings.verifying"), false);
        } catch (e) {
          fail(e.message);
          console.log(e);
        }
      }

      const response = await fetch(formElement.action, {
        method: "POST",
        body: data,
      });

      const result = deserialize(await response.text());

      if (result.type === "success") {
        await invalidateAll();
        if (data.get("password")) $password = data.get("password");
      }

      applyAction(result);
    } catch (e) {
      console.log(e);
      fail("Something went wrong");
    }

    submitting = false;
  }
  run(() => {
    form?.user && ({ user } = form);
  });
  run(() => {
    form?.success && throttledSuccess();
  });
  run(() => {
    form?.message && fail(form.message);
  });
  run(() => {
    if (form?.message?.startsWith("Pin")) {
      fail("Wrong pin, try again");
      $pin = "";
    }
  });
  run(() => {
    if (!$loading && $page.url.searchParams.get("verified"))
      success($t("user.settings.verified"));
  });
</script>

{#if user.haspin && $pin?.length !== 6}
  <Pin />
{/if}

<form
  method="POST"
  class="mb-[154px] settings"
  onsubmit={preventDefault(handleSubmit)}
  bind:this={formElement}
>
  <input type="hidden" name="pin" value={$pin} />
  <input type="hidden" name="tab" value={tab} />

  <div class="mt-24 mb-20 px-3 md:px-0 w-full md:max-w-lg mx-auto space-y-8">
    <div class="header">
      <h1 class="text-center text-3xl md:text-4xl font-semibold mb-10">
        {$t("user.settings.header")}
      </h1>

      <div class="flex justify-around items-center border-b pb-3">
        {#each tabs.filter((t) => t.name !== "shopify") as { name, key }}
          <a href={`/settings/${name}`}>
            <button
              type="button"
              class="hover:opacity-80"
              class:font-bold={tab === name ||
                (tab === "shopify" && name === "pos")}
              >{$t(`user.settings.${key}`)}</button
            >
          </a>
        {/each}
      </div>
    </div>

    <slot />
  </div>

  <div
    class="z-10 fixed bottom-0 bg-base-100 shadow border-t border-t-8 border-accent w-full flex justify-center items-center py-3"
  >
    <button
      bind:this={$save}
      type="submit"
      class="btn !w-auto"
      class:bg-base-200={submitting}
    >
      {#if submitting}
        <Spinner />
      {:else}
        <div class="my-auto">{$t("user.settings.saveSettings")}</div>
      {/if}
    </button>
  </div>
</form>