flotilla/src/app/components/ProfileEdit.svelte

55 lines
1.6 KiB
Svelte
Raw Normal View History

2024-11-15 22:25:18 +00:00
<script lang="ts">
2025-02-14 18:53:36 +00:00
import type {Profile} from "@welshman/util"
2026-04-16 18:30:59 +00:00
import {makeProfile} from "@welshman/util"
2026-03-06 22:18:05 +00:00
import {pubkey, profilesByPubkey, waitForThunkError} from "@welshman/app"
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-06-08 21:14:40 +00:00
import {clearModals} from "@app/modal"
import {pushToast} from "@app/toast"
import {updateProfile} from "@app/profiles"
2024-11-15 22:25:18 +00:00
2025-04-15 16:36:39 +00:00
const profile = $profilesByPubkey.get($pubkey!) || makeProfile()
2026-04-16 18:30:59 +00:00
const initialValues = {profile}
2024-11-15 22:25:18 +00:00
const back = () => history.back()
2026-04-16 18:30:59 +00:00
const onsubmit = async ({profile}: {profile: Profile}) => {
2026-03-06 22:18:05 +00:00
loading = true
try {
2026-04-16 18:30:59 +00:00
const error = await waitForThunkError(updateProfile({profile}))
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()
}
} 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()}
2026-02-20 22:21:38 +00:00
<Button class="btn btn-link" onclick={back}>
<Icon icon={AltArrowLeft} />
Go Back
</Button>
2026-03-06 22:18:05 +00:00
<Button type="submit" class="btn btn-primary" disabled={loading}>
<Spinner {loading} />
Save Changes
</Button>
2025-02-14 18:53:36 +00:00
{/snippet}
</ProfileEditForm>