fix: extract type selection to PostFormState class for consistency
This commit is contained in:
4 files changed
+28
-20
No files matched your search
@@ -89,7 +89,10 @@
|
|||||||
post = e
|
post = e
|
||||||
onedit?.(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}
|
{#snippet title()}{/snippet}
|
||||||
</PostForm>
|
</PostForm>
|
||||||
|
|||||||
@@ -56,9 +56,8 @@
|
|||||||
|
|
||||||
let { editPost, init, title, onsubmit }: Props = $props()
|
let { editPost, init, title, onsubmit }: Props = $props()
|
||||||
|
|
||||||
let form = $state<PostFormState>(init ?? new PostFormState())
|
let form = $state<PostFormState>(
|
||||||
let type = $derived<'normal' | 'poll' | 'event'>(
|
init ?? new PostFormState({ type: 'normal' }),
|
||||||
init?.poll ? 'poll' : init?.event ? 'event' : 'normal',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
let extendedCommunity = $derived.by(() => {
|
let extendedCommunity = $derived.by(() => {
|
||||||
@@ -83,7 +82,7 @@
|
|||||||
$t('form.post.types.poll'),
|
$t('form.post.types.poll'),
|
||||||
$t('form.post.types.event'),
|
$t('form.post.types.event'),
|
||||||
]}
|
]}
|
||||||
bind:selected={type}
|
bind:selected={form.type}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
@@ -173,7 +172,7 @@
|
|||||||
loading = true
|
loading = true
|
||||||
|
|
||||||
form
|
form
|
||||||
.submit(type, editPost)
|
.submit(editPost)
|
||||||
.then(onsubmit)
|
.then(onsubmit)
|
||||||
.catch((err) =>
|
.catch((err) =>
|
||||||
pushError({ message: errorMessage(err as string), scope: 'post-form' }),
|
pushError({ message: errorMessage(err as string), scope: 'post-form' }),
|
||||||
@@ -242,7 +241,7 @@
|
|||||||
previewButton
|
previewButton
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{#if type == 'normal' && form.url !== undefined}
|
{#if form.type == 'normal' && form.url !== undefined}
|
||||||
<TextInput
|
<TextInput
|
||||||
label={$t('form.post.url')}
|
label={$t('form.post.url')}
|
||||||
bind:value={form.url}
|
bind:value={form.url}
|
||||||
@@ -268,11 +267,12 @@
|
|||||||
</TextInput>
|
</TextInput>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
{#if type == 'poll' && form.poll}
|
{#if form.type == 'poll' && form.poll}
|
||||||
<div>
|
<div>
|
||||||
<Label>{$t('post.poll.choices')}</Label>
|
<Label>{$t('post.poll.choices')}</Label>
|
||||||
<CommonList>
|
<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">
|
<li class="px-4 py-1 flex flex-row items-center xs">
|
||||||
<div class="p-0! font-medium flex-1">
|
<div class="p-0! font-medium flex-1">
|
||||||
<FreeTextInput
|
<FreeTextInput
|
||||||
@@ -343,18 +343,18 @@
|
|||||||
{/if}
|
{/if}
|
||||||
{/snippet}
|
{/snippet}
|
||||||
</EndPlaceholder>
|
</EndPlaceholder>
|
||||||
{/if}
|
|
||||||
|
|
||||||
{#if settings.debugInfo}
|
{#if settings.debugInfo}
|
||||||
{form.poll?.end_poll}
|
{form.poll?.end_poll}
|
||||||
{/if}
|
{/if}
|
||||||
|
{/if}
|
||||||
|
|
||||||
<div class="flex flex-row flex-wrap gap-2">
|
<div class="flex flex-row flex-wrap gap-2">
|
||||||
<ButtonGroup
|
<ButtonGroup
|
||||||
orientation="horizontal"
|
orientation="horizontal"
|
||||||
class="flex flex-row flex-wrap w-full"
|
class="flex flex-row flex-wrap w-full"
|
||||||
>
|
>
|
||||||
{#if type == 'normal'}
|
{#if form.type == 'normal'}
|
||||||
<Button
|
<Button
|
||||||
onclick={() => (form.url = '')}
|
onclick={() => (form.url = '')}
|
||||||
disabled={form.url !== undefined}
|
disabled={form.url !== undefined}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import type {
|
|||||||
} from '$lib/api/types'
|
} from '$lib/api/types'
|
||||||
|
|
||||||
export type PostFormInit = {
|
export type PostFormInit = {
|
||||||
|
type?: 'normal' | 'poll' | 'event'
|
||||||
community?: Community
|
community?: Community
|
||||||
name?: string
|
name?: string
|
||||||
body?: string
|
body?: string
|
||||||
@@ -23,6 +24,8 @@ export type PostFormInit = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class PostFormState {
|
export class PostFormState {
|
||||||
|
type?: 'normal' | 'poll' | 'event'
|
||||||
|
|
||||||
community?: Community
|
community?: Community
|
||||||
title: string
|
title: string
|
||||||
body?: string
|
body?: string
|
||||||
@@ -36,6 +39,7 @@ export class PostFormState {
|
|||||||
flairList: CommunityFlair[]
|
flairList: CommunityFlair[]
|
||||||
|
|
||||||
constructor(post?: PostFormInit) {
|
constructor(post?: PostFormInit) {
|
||||||
|
this.type = $state(post?.type ?? 'normal')
|
||||||
this.community = $state(post?.community)
|
this.community = $state(post?.community)
|
||||||
this.title = $state(post?.name ?? '')
|
this.title = $state(post?.name ?? '')
|
||||||
this.body = $state(post?.body)
|
this.body = $state(post?.body)
|
||||||
@@ -74,10 +78,7 @@ export class PostFormState {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async submit(
|
async submit(postId?: number): Promise<PostView> {
|
||||||
type: 'normal' | 'poll' | 'event' = 'normal',
|
|
||||||
postId?: number,
|
|
||||||
): Promise<PostView> {
|
|
||||||
if (!this.validate(postId ? 'edit' : 'create'))
|
if (!this.validate(postId ? 'edit' : 'create'))
|
||||||
throw new Error('failed validation')
|
throw new Error('failed validation')
|
||||||
|
|
||||||
@@ -95,8 +96,8 @@ export class PostFormState {
|
|||||||
alt_text: this.altText,
|
alt_text: this.altText,
|
||||||
custom_thumbnail: this.thumbnail,
|
custom_thumbnail: this.thumbnail,
|
||||||
language_id: Number(this.language) || undefined,
|
language_id: Number(this.language) || undefined,
|
||||||
poll: type == 'poll' ? this.poll : undefined,
|
poll: this.type == 'poll' ? this.poll : undefined,
|
||||||
event: type == 'event' ? this.event : undefined,
|
event: this.type == 'event' ? this.event : undefined,
|
||||||
})
|
})
|
||||||
).post_view
|
).post_view
|
||||||
} else {
|
} else {
|
||||||
@@ -110,8 +111,8 @@ export class PostFormState {
|
|||||||
custom_thumbnail: this.thumbnail,
|
custom_thumbnail: this.thumbnail,
|
||||||
nsfw: this.nsfw,
|
nsfw: this.nsfw,
|
||||||
language_id: Number(this.language) || undefined,
|
language_id: Number(this.language) || undefined,
|
||||||
poll: type == 'poll' ? this.poll : undefined,
|
poll: this.type == 'poll' ? this.poll : undefined,
|
||||||
event: type == 'event' ? this.event : undefined,
|
event: this.type == 'event' ? this.event : undefined,
|
||||||
})
|
})
|
||||||
).post_view
|
).post_view
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,11 @@
|
|||||||
</svelte:head>
|
</svelte:head>
|
||||||
|
|
||||||
<PostForm
|
<PostForm
|
||||||
init={new PostFormState(crosspost ?? { community: community?.community })}
|
init={crosspost
|
||||||
|
? new PostFormState(crosspost)
|
||||||
|
: community
|
||||||
|
? new PostFormState({ community: community?.community })
|
||||||
|
: undefined}
|
||||||
onsubmit={(post) => goto(postLink(post.post))}
|
onsubmit={(post) => goto(postLink(post.post))}
|
||||||
>
|
>
|
||||||
{#snippet title()}{/snippet}
|
{#snippet title()}{/snippet}
|
||||||
|
|||||||
Reference in new issue
Block a user