fix: extract type selection to PostFormState class for consistency

This commit is contained in:
Xylight committed 2025-11-15 16:27:01 -08:00
1 parent 939c36158f
commit 1e09647e5d
4 files changed
+30 -22

No files matched your search

@@ -89,7 +89,10 @@
post = e
onedit?.(e)
}}
init={new PostFormState({ ...post.post })}
init={new PostFormState({
type: post.post.poll ? 'poll' : post.post.event ? 'event' : 'normal',
...post.post,
})}
>
{#snippet title()}{/snippet}
</PostForm>
+12 -12
View File
@@ -56,9 +56,8 @@
let { editPost, init, title, onsubmit }: Props = $props()
let form = $state<PostFormState>(init ?? new PostFormState())
let type = $derived<'normal' | 'poll' | 'event'>(
init?.poll ? 'poll' : init?.event ? 'event' : 'normal',
let form = $state<PostFormState>(
init ?? new PostFormState({ type: 'normal' }),
)
let extendedCommunity = $derived.by(() => {
@@ -83,7 +82,7 @@
$t('form.post.types.poll'),
$t('form.post.types.event'),
]}
bind:selected={type}
bind:selected={form.type}
/>
</div>
{/if}
@@ -173,7 +172,7 @@
loading = true
form
.submit(type, editPost)
.submit(editPost)
.then(onsubmit)
.catch((err) =>
pushError({ message: errorMessage(err as string), scope: 'post-form' }),
@@ -242,7 +241,7 @@
previewButton
/>
{#if type == 'normal' && form.url !== undefined}
{#if form.type == 'normal' && form.url !== undefined}
<TextInput
label={$t('form.post.url')}
bind:value={form.url}
@@ -268,11 +267,12 @@
</TextInput>
{/if}
{#if type == 'poll' && form.poll}
{#if form.type == 'poll' && form.poll}
<div>
<Label>{$t('post.poll.choices')}</Label>
<CommonList>
{#each form.poll.choices as choice, index}
<!--eslint-disable-next-line @typescript-eslint/no-unused-vars-->
{#each form.poll.choices as _, index}
<li class="px-4 py-1 flex flex-row items-center xs">
<div class="p-0! font-medium flex-1">
<FreeTextInput
@@ -343,10 +343,10 @@
{/if}
{/snippet}
</EndPlaceholder>
{/if}
{#if settings.debugInfo}
{form.poll?.end_poll}
{#if settings.debugInfo}
{form.poll?.end_poll}
{/if}
{/if}
<div class="flex flex-row flex-wrap gap-2">
@@ -354,7 +354,7 @@
orientation="horizontal"
class="flex flex-row flex-wrap w-full"
>
{#if type == 'normal'}
{#if form.type == 'normal'}
<Button
onclick={() => (form.url = '')}
disabled={form.url !== undefined}
+9 -8
View File
@@ -9,6 +9,7 @@ import type {
} from '$lib/api/types'
export type PostFormInit = {
type?: 'normal' | 'poll' | 'event'
community?: Community
name?: string
body?: string
@@ -23,6 +24,8 @@ export type PostFormInit = {
}
export class PostFormState {
type?: 'normal' | 'poll' | 'event'
community?: Community
title: string
body?: string
@@ -36,6 +39,7 @@ export class PostFormState {
flairList: CommunityFlair[]
constructor(post?: PostFormInit) {
this.type = $state(post?.type ?? 'normal')
this.community = $state(post?.community)
this.title = $state(post?.name ?? '')
this.body = $state(post?.body)
@@ -74,10 +78,7 @@ export class PostFormState {
return true
}
async submit(
type: 'normal' | 'poll' | 'event' = 'normal',
postId?: number,
): Promise<PostView> {
async submit(postId?: number): Promise<PostView> {
if (!this.validate(postId ? 'edit' : 'create'))
throw new Error('failed validation')
@@ -95,8 +96,8 @@ export class PostFormState {
alt_text: this.altText,
custom_thumbnail: this.thumbnail,
language_id: Number(this.language) || undefined,
poll: type == 'poll' ? this.poll : undefined,
event: type == 'event' ? this.event : undefined,
poll: this.type == 'poll' ? this.poll : undefined,
event: this.type == 'event' ? this.event : undefined,
})
).post_view
} else {
@@ -110,8 +111,8 @@ export class PostFormState {
custom_thumbnail: this.thumbnail,
nsfw: this.nsfw,
language_id: Number(this.language) || undefined,
poll: type == 'poll' ? this.poll : undefined,
event: type == 'event' ? this.event : undefined,
poll: this.type == 'poll' ? this.poll : undefined,
event: this.type == 'event' ? this.event : undefined,
})
).post_view
}