flotilla/src/app/components/ProfileEdit.svelte

51 lines
1.7 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"
2024-11-15 22:25:18 +00:00
import {
2025-04-15 16:36:39 +00:00
getTag,
2024-11-15 22:25:18 +00:00
createEvent,
makeProfile,
editProfile,
createProfile,
isPublishedProfile,
2025-04-15 16:36:39 +00:00
uniqTags,
2024-11-15 22:25:18 +00:00
} from "@welshman/util"
2025-04-09 22:32:18 +00:00
import {Router, pubkey, profilesByPubkey, publishThunk} from "@welshman/app"
2024-11-15 22:25:18 +00:00
import Button from "@lib/components/Button.svelte"
2025-02-14 18:53:36 +00:00
import ProfileEditForm from "@app/components/ProfileEditForm.svelte"
import {clearModals} from "@app/modal"
2024-11-15 22:25:18 +00:00
import {pushToast} from "@app/toast"
2025-04-15 16:36:39 +00:00
import {PROTECTED, getMembershipUrls, userMembership} from "@app/state"
2024-11-15 22:25:18 +00:00
2025-04-15 16:36:39 +00:00
const profile = $profilesByPubkey.get($pubkey!) || makeProfile()
const shouldBroadcast = !getTag(PROTECTED, profile.event?.tags || [])
const initialValues = {profile, shouldBroadcast}
2024-11-15 22:25:18 +00:00
const back = () => history.back()
2025-04-15 16:36:39 +00:00
const onsubmit = ({profile, shouldBroadcast}: {profile: Profile; shouldBroadcast: boolean}) => {
2025-02-14 18:53:36 +00:00
const template = isPublishedProfile(profile) ? editProfile(profile) : createProfile(profile)
2025-04-15 18:03:27 +00:00
const relays = [...getMembershipUrls($userMembership)]
2025-04-15 16:36:39 +00:00
2025-04-15 18:03:27 +00:00
if (shouldBroadcast) {
relays.push(...Router.get().FromUser().getUrls())
} else {
2025-04-15 16:36:39 +00:00
template.tags = uniqTags([...template.tags, PROTECTED])
}
2024-11-15 22:25:18 +00:00
const event = createEvent(template.kind, template)
publishThunk({event, relays})
pushToast({message: "Your profile has been updated!"})
clearModals()
}
</script>
2025-02-14 18:53:36 +00:00
<ProfileEditForm {initialValues} {onsubmit}>
{#snippet footer()}
<div class="mt-4 flex flex-row items-center justify-between gap-4">
<Button class="btn btn-neutral" onclick={back}>Discard Changes</Button>
<Button type="submit" class="btn btn-primary">Save Changes</Button>
</div>
{/snippet}
</ProfileEditForm>