flotilla/src/app/components/ProfileEdit.svelte

67 lines
2 KiB
Svelte
Raw Normal View History

2024-11-15 22:25:18 +00:00
<script lang="ts">
import {spec} from "@welshman/lib"
2026-02-20 22:21:38 +00:00
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
2026-03-06 22:18:05 +00:00
import {errorMessage} from "@lib/util"
2026-02-20 22:21:38 +00:00
import Icon from "@lib/components/Icon.svelte"
2024-11-15 22:25:18 +00:00
import Button from "@lib/components/Button.svelte"
2026-03-06 22:18:05 +00:00
import Spinner from "@lib/components/Spinner.svelte"
2025-02-14 18:53:36 +00:00
import ProfileEditForm from "@app/components/ProfileEditForm.svelte"
2026-07-28 16:16:26 +00:00
import type {Values} from "@app/components/ProfileEditForm.svelte"
2026-06-08 21:14:40 +00:00
import {clearModals} from "@app/modal"
import {pushToast} from "@app/toast"
2026-07-28 16:16:26 +00:00
import {profiles, user} from "@app/core"
import {getProfileGeohash} from "@app/location"
2024-11-15 22:25:18 +00:00
const existingProfile = $profiles.get($user.pubkey)
const initialValues = {
profile: {...existingProfile?.values},
geohash: getProfileGeohash(existingProfile),
}
2024-11-15 22:25:18 +00:00
const back = () => history.back()
const onsubmit = async ({profile, geohash}: Values) => {
2026-03-06 22:18:05 +00:00
loading = true
try {
const command = await $profiles.update(writer => {
writer.update(profile).dropTags(spec(["g"]))
return geohash ? writer.addTags(["g", geohash]) : writer
})
const error = (await command.publish().settled()).getError()
2026-03-06 22:18:05 +00:00
if (error) {
pushToast({
theme: "error",
message: `Failed to update your profile: ${errorMessage(error)}`,
})
} else {
pushToast({message: "Your profile has been updated!"})
clearModals()
}
} catch (e) {
console.error(e)
pushToast({theme: "error", message: "Failed to update your profile."})
2026-03-06 22:18:05 +00:00
} finally {
loading = false
}
2024-11-15 22:25:18 +00:00
}
2026-03-06 22:18:05 +00:00
let loading = $state(false)
2024-11-15 22:25:18 +00:00
</script>
2025-02-14 18:53:36 +00:00
<ProfileEditForm {initialValues} {onsubmit}>
{#snippet footer()}
<Button class="button button-link" onclick={back}>
2026-02-20 22:21:38 +00:00
<Icon icon={AltArrowLeft} />
Go Back
</Button>
<Button type="submit" class="button button-primary" disabled={loading}>
2026-03-06 22:18:05 +00:00
<Spinner {loading} />
Save Changes
</Button>
2025-02-14 18:53:36 +00:00
{/snippet}
</ProfileEditForm>