+page.svelte (3271B)
1 <script lang="ts"> 2 import { db, notif, route } from "$lib/utils/app"; 3 import { ls } from "$lib/utils/i18n"; 4 import { ProfileEdit } from "@radroots/apps-lib-pwa"; 5 import { qp_field, qp_keynostr } from "@radroots/apps-lib-pwa/stores/app"; 6 import type { IViewProfileEditData } from "@radroots/apps-lib-pwa/types/views/profile"; 7 import { parse_view_profile_field_key } from "@radroots/apps-lib-pwa/utils/profile"; 8 import { handle_err, throw_err } from "@radroots/utils"; 9 import { onMount } from "svelte"; 10 11 type LoadData = IViewProfileEditData | undefined; 12 let data: LoadData = $state(undefined); 13 14 let val_field_init = $state(``); 15 let val_field = $state(``); 16 17 onMount(async () => { 18 try { 19 data = await load_data(); 20 } catch (e) { 21 handle_err(e, `on_mount`); 22 await route(`/`); 23 } 24 }); 25 26 const load_data = async (): Promise<LoadData | undefined> => { 27 const field = parse_view_profile_field_key($qp_field); 28 if (!field || !$qp_keynostr) throw_err("*-params"); 29 const nostr_profile = await db.nostr_profile_find_one({ 30 on: { 31 public_key: $qp_keynostr, 32 }, 33 }); 34 if ("err" in nostr_profile) throw_err(nostr_profile); 35 if (field in nostr_profile.result) 36 val_field = nostr_profile.result[field] || ``; 37 val_field_init = val_field; 38 const data: LoadData = { 39 public_key: nostr_profile.result.public_key, 40 field, 41 }; 42 return data; 43 }; 44 </script> 45 46 <ProfileEdit 47 bind:val_field 48 basis={{ 49 data, 50 on_handle_back: async ({ field, public_key }) => { 51 try { 52 if (val_field_init === val_field) 53 return void (await route(`/profile`)); 54 const confirm = await notif.confirm({ 55 message: `${$ls( 56 `notification.profile.update_name_confirmation`, 57 )}. ${$ls(`common.do_you_want_to_continue_q`)}`, 58 }); 59 if (!confirm) return; 60 const nostr_profile_update = await db.nostr_profile_update({ 61 on: { 62 public_key, 63 }, 64 fields: { 65 [field]: val_field, 66 }, 67 }); 68 if ("err" in nostr_profile_update) 69 throw_err(nostr_profile_update); 70 const tb_nostr_profile = await db.nostr_profile_find_one({ 71 on: { public_key }, 72 }); 73 console.log( 74 JSON.stringify(tb_nostr_profile, null, 4), 75 `tb_nostr_profile`, 76 ); 77 if ("err" in tb_nostr_profile) throw_err(tb_nostr_profile); 78 //nostr_sync.metadata({ metadata: tb_nostr_profile.result }); // no await 79 await route(`/profile`); 80 } catch (e) { 81 handle_err(e, `on_handle_back`); 82 } 83 }, 84 on_handle_input: async () => { 85 try { 86 } catch (e) { 87 handle_err(e, `on_handle_input`); 88 } 89 }, 90 }} 91 />