flotilla/src/app/components/ProfileEdit.svelte

56 lines
1.9 KiB
Svelte
Raw Normal View History

2024-11-15 22:25:18 +00:00
<script lang="ts">
import {nthNe} from "@welshman/lib"
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,
2025-06-10 20:35:57 +00:00
makeEvent,
2024-11-15 22:25:18 +00:00
makeProfile,
editProfile,
createProfile,
isPublishedProfile,
2025-04-15 16:36:39 +00:00
uniqTags,
2024-11-15 22:25:18 +00:00
} from "@welshman/util"
import {Router} from "@welshman/router"
import {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}) => {
const router = Router.get()
2025-02-14 18:53:36 +00:00
const template = isPublishedProfile(profile) ? editProfile(profile) : createProfile(profile)
const scenarios = [router.FromRelays(getMembershipUrls($userMembership))]
2025-04-15 16:36:39 +00:00
2025-04-15 18:03:27 +00:00
if (shouldBroadcast) {
scenarios.push(router.FromUser(), router.Index())
template.tags = template.tags.filter(nthNe(0, "-"))
2025-04-15 18:03:27 +00:00
} else {
2025-04-15 16:36:39 +00:00
template.tags = uniqTags([...template.tags, PROTECTED])
}
2025-06-10 20:35:57 +00:00
const event = makeEvent(template.kind, template)
const relays = router.merge(scenarios).getUrls()
2024-11-15 22:25:18 +00:00
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>