feat [4.5/6]: add piefed poll creation
This commit is contained in:
8 files changed
+179
-17
No files matched your search
@@ -644,6 +644,8 @@ export interface CreatePost {
|
|||||||
nsfw?: boolean
|
nsfw?: boolean
|
||||||
language_id?: LanguageId
|
language_id?: LanguageId
|
||||||
custom_thumbnail?: string
|
custom_thumbnail?: string
|
||||||
|
poll?: PostPoll
|
||||||
|
event?: PostEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CreatePostLike {
|
export interface CreatePostLike {
|
||||||
@@ -801,6 +803,8 @@ export interface EditPost {
|
|||||||
nsfw?: boolean
|
nsfw?: boolean
|
||||||
language_id?: LanguageId
|
language_id?: LanguageId
|
||||||
custom_thumbnail?: string
|
custom_thumbnail?: string
|
||||||
|
poll?: PostPoll
|
||||||
|
event?: PostEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EditPrivateMessage {
|
export interface EditPrivateMessage {
|
||||||
|
|||||||
@@ -146,7 +146,12 @@
|
|||||||
"unset": "Unset",
|
"unset": "Unset",
|
||||||
"readMore": "Read more",
|
"readMore": "Read more",
|
||||||
"flair": "Add flair",
|
"flair": "Add flair",
|
||||||
"fetching": "Fetching site metadata"
|
"fetching": "Fetching site metadata",
|
||||||
|
"types": {
|
||||||
|
"normal": "Discussion",
|
||||||
|
"poll": "Poll",
|
||||||
|
"event": "Event"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"profile": {
|
"profile": {
|
||||||
"displayName": "Display name",
|
"displayName": "Display name",
|
||||||
@@ -587,7 +592,11 @@
|
|||||||
"show": "Show results",
|
"show": "Show results",
|
||||||
"hide": "Hide results",
|
"hide": "Hide results",
|
||||||
"ends": "Ends {{time}}"
|
"ends": "Ends {{time}}"
|
||||||
}
|
},
|
||||||
|
"choices": "Choices",
|
||||||
|
"single": "Single choice",
|
||||||
|
"multiple": "Multiple choice",
|
||||||
|
"mode": "Mode"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
|
|||||||
@@ -5,12 +5,17 @@
|
|||||||
import { errorMessage } from '$lib/app/error'
|
import { errorMessage } from '$lib/app/error'
|
||||||
import { t } from '$lib/app/i18n'
|
import { t } from '$lib/app/i18n'
|
||||||
import MarkdownEditor from '$lib/app/markdown/MarkdownEditor.svelte'
|
import MarkdownEditor from '$lib/app/markdown/MarkdownEditor.svelte'
|
||||||
|
import { settings } from '$lib/app/settings.svelte'
|
||||||
import { placeholders } from '$lib/app/util.svelte'
|
import { placeholders } from '$lib/app/util.svelte'
|
||||||
|
import Duration from '$lib/ui/form/Duration.svelte'
|
||||||
import FreeTextInput from '$lib/ui/form/FreeTextInput.svelte'
|
import FreeTextInput from '$lib/ui/form/FreeTextInput.svelte'
|
||||||
import ObjectAutocomplete from '$lib/ui/form/ObjectAutocomplete.svelte'
|
import ObjectAutocomplete from '$lib/ui/form/ObjectAutocomplete.svelte'
|
||||||
|
import MultiSelect from '$lib/ui/form/Switch.svelte'
|
||||||
import Avatar from '$lib/ui/generic/Avatar.svelte'
|
import Avatar from '$lib/ui/generic/Avatar.svelte'
|
||||||
import ErrorContainer, { pushError } from '$lib/ui/info/ErrorContainer.svelte'
|
import ErrorContainer, { pushError } from '$lib/ui/info/ErrorContainer.svelte'
|
||||||
import { Header } from '$lib/ui/layout'
|
import { CommonList, Header } from '$lib/ui/layout'
|
||||||
|
import EndPlaceholder from '$lib/ui/layout/EndPlaceholder.svelte'
|
||||||
|
import { publishedToDate } from '$lib/ui/util/date'
|
||||||
import {
|
import {
|
||||||
Badge,
|
Badge,
|
||||||
Button,
|
Button,
|
||||||
@@ -36,6 +41,8 @@
|
|||||||
Photo,
|
Photo,
|
||||||
Plus,
|
Plus,
|
||||||
Sparkles,
|
Sparkles,
|
||||||
|
Tag,
|
||||||
|
Trash,
|
||||||
XMark,
|
XMark,
|
||||||
} from 'svelte-hero-icons/dist'
|
} from 'svelte-hero-icons/dist'
|
||||||
import { autofillPost, PostFormState } from './postform.svelte'
|
import { autofillPost, PostFormState } from './postform.svelte'
|
||||||
@@ -50,6 +57,9 @@
|
|||||||
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>(init ?? new PostFormState())
|
||||||
|
let type = $derived<'normal' | 'poll' | 'event'>(
|
||||||
|
init?.poll ? 'poll' : init?.event ? 'event' : 'normal',
|
||||||
|
)
|
||||||
|
|
||||||
let extendedCommunity = $derived.by(() => {
|
let extendedCommunity = $derived.by(() => {
|
||||||
const api = client()
|
const api = client()
|
||||||
@@ -64,6 +74,20 @@
|
|||||||
let uploadImage = $state(false)
|
let uploadImage = $state(false)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
{#if client() instanceof PiefedClient}
|
||||||
|
<div class="mb-3">
|
||||||
|
<MultiSelect
|
||||||
|
options={['normal', 'poll', 'event']}
|
||||||
|
optionNames={[
|
||||||
|
$t('form.post.types.normal'),
|
||||||
|
$t('form.post.types.poll'),
|
||||||
|
$t('form.post.types.event'),
|
||||||
|
]}
|
||||||
|
bind:selected={type}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
{#if uploadImage}
|
{#if uploadImage}
|
||||||
{#await import('$lib/ui/form/ImageInputModal.svelte') then { default: UploadModal }}
|
{#await import('$lib/ui/form/ImageInputModal.svelte') then { default: UploadModal }}
|
||||||
<UploadModal
|
<UploadModal
|
||||||
@@ -147,8 +171,9 @@
|
|||||||
onsubmit={(e) => {
|
onsubmit={(e) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
loading = true
|
loading = true
|
||||||
|
|
||||||
form
|
form
|
||||||
.submit(editPost)
|
.submit(type, 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' }),
|
||||||
@@ -208,6 +233,7 @@
|
|||||||
bind:value={form.title}
|
bind:value={form.title}
|
||||||
placeholder={placeholders.get('post')}
|
placeholder={placeholders.get('post')}
|
||||||
label={$t('form.post.title')}
|
label={$t('form.post.title')}
|
||||||
|
class="font-display font-medium text-2xl"
|
||||||
/>
|
/>
|
||||||
<MarkdownEditor
|
<MarkdownEditor
|
||||||
label={$t('form.post.body')}
|
label={$t('form.post.body')}
|
||||||
@@ -216,7 +242,7 @@
|
|||||||
previewButton
|
previewButton
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{#if form.url !== undefined}
|
{#if 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}
|
||||||
@@ -242,11 +268,93 @@
|
|||||||
</TextInput>
|
</TextInput>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
|
{#if type == 'poll' && form.poll}
|
||||||
|
<div>
|
||||||
|
<Label>{$t('post.poll.choices')}</Label>
|
||||||
|
<CommonList>
|
||||||
|
{#each form.poll.choices as choice, index}
|
||||||
|
<li class="px-4 py-1 flex flex-row items-center xs">
|
||||||
|
<div class="p-0! font-medium flex-1">
|
||||||
|
<FreeTextInput
|
||||||
|
bind:value={form.poll.choices[index].choice_text}
|
||||||
|
class="w-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<Button
|
||||||
|
onclick={() => form.poll?.choices.splice(index, 1)}
|
||||||
|
size="square-md"
|
||||||
|
aria-label={$t('post.actions.more.delete')}
|
||||||
|
>
|
||||||
|
<Icon src={Trash} size="16" mini />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
<li class="xs">
|
||||||
|
<button
|
||||||
|
onclick={() =>
|
||||||
|
form.poll?.choices.push({
|
||||||
|
choice_text: `Option ${form.poll?.choices.length + 1}`,
|
||||||
|
id: Math.max(...form.poll.choices.map((i) => i.id)) + 1,
|
||||||
|
num_votes: 0,
|
||||||
|
sort_order: 2,
|
||||||
|
})}
|
||||||
|
type="button"
|
||||||
|
class="font-medium w-full px-4 py-2! flex flex-row items-center gap-1 justify-center cursor-pointer"
|
||||||
|
>
|
||||||
|
<Icon src={Plus} size="16" micro />
|
||||||
|
{$t('common.add')}
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
</CommonList>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<EndPlaceholder margin="bottom-lg">
|
||||||
|
<Select label={$t('post.poll.mode')} bind:value={form.poll!.mode}>
|
||||||
|
<Option value="single">
|
||||||
|
{$t('post.poll.single')}
|
||||||
|
</Option>
|
||||||
|
<Option value="multiple">
|
||||||
|
{$t('post.poll.multiple')}
|
||||||
|
</Option>
|
||||||
|
</Select>
|
||||||
|
{#snippet action()}
|
||||||
|
{#if !editPost}
|
||||||
|
<Duration
|
||||||
|
bind:value={
|
||||||
|
() => {
|
||||||
|
return form.poll?.end_poll
|
||||||
|
? Math.floor(
|
||||||
|
Date.now() -
|
||||||
|
publishedToDate(form.poll?.end_poll).getTime(),
|
||||||
|
)
|
||||||
|
: Date.now() + 24 * 60 * 60
|
||||||
|
},
|
||||||
|
(v) => {
|
||||||
|
if (v == -1) form.poll!.end_poll = undefined
|
||||||
|
else
|
||||||
|
form.poll!.end_poll = new Date(
|
||||||
|
Date.now() + v * 1000,
|
||||||
|
).toISOString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
{/if}
|
||||||
|
{/snippet}
|
||||||
|
</EndPlaceholder>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
{#if settings.debugInfo}
|
||||||
|
{form.poll?.end_poll}
|
||||||
|
{/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'}
|
||||||
<Button
|
<Button
|
||||||
onclick={() => (form.url = '')}
|
onclick={() => (form.url = '')}
|
||||||
disabled={form.url !== undefined}
|
disabled={form.url !== undefined}
|
||||||
@@ -273,6 +381,7 @@
|
|||||||
{$t('form.post.altText')}
|
{$t('form.post.altText')}
|
||||||
</Button>
|
</Button>
|
||||||
{/if}
|
{/if}
|
||||||
|
{/if}
|
||||||
{#if form.language === undefined}
|
{#if form.language === undefined}
|
||||||
<Button
|
<Button
|
||||||
class="animate-pop-in"
|
class="animate-pop-in"
|
||||||
@@ -289,7 +398,7 @@
|
|||||||
class="animate-pop-in"
|
class="animate-pop-in"
|
||||||
onclick={() =>
|
onclick={() =>
|
||||||
modal({ title: $t('form.post.flair'), snippet: flairs })}
|
modal({ title: $t('form.post.flair'), snippet: flairs })}
|
||||||
icon={ChatBubbleBottomCenterText}
|
icon={Tag}
|
||||||
>
|
>
|
||||||
{$t('form.post.flair')}
|
{$t('form.post.flair')}
|
||||||
</Button>
|
</Button>
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
import { client } from '$lib/api/client.svelte'
|
import { client } from '$lib/api/client.svelte'
|
||||||
import { PiefedClient } from '$lib/api/piefed/adapter'
|
import { PiefedClient } from '$lib/api/piefed/adapter'
|
||||||
import type { Community, CommunityFlair, PostView } from '$lib/api/types'
|
import type {
|
||||||
|
Community,
|
||||||
|
CommunityFlair,
|
||||||
|
PostEvent,
|
||||||
|
PostPoll,
|
||||||
|
PostView,
|
||||||
|
} from '$lib/api/types'
|
||||||
|
|
||||||
export type PostFormInit = {
|
export type PostFormInit = {
|
||||||
community?: Community
|
community?: Community
|
||||||
@@ -11,6 +17,8 @@ export type PostFormInit = {
|
|||||||
alt_text?: string
|
alt_text?: string
|
||||||
thumbnail?: string
|
thumbnail?: string
|
||||||
language_id?: number
|
language_id?: number
|
||||||
|
poll?: PostPoll
|
||||||
|
event?: PostEvent
|
||||||
flair_list?: CommunityFlair[]
|
flair_list?: CommunityFlair[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,6 +31,8 @@ export class PostFormState {
|
|||||||
altText?: string
|
altText?: string
|
||||||
thumbnail?: string
|
thumbnail?: string
|
||||||
language?: string
|
language?: string
|
||||||
|
poll?: PostPoll
|
||||||
|
event?: PostEvent
|
||||||
flairList: CommunityFlair[]
|
flairList: CommunityFlair[]
|
||||||
|
|
||||||
constructor(post?: PostFormInit) {
|
constructor(post?: PostFormInit) {
|
||||||
@@ -33,6 +43,27 @@ export class PostFormState {
|
|||||||
this.nsfw = $state(post?.nsfw ?? false)
|
this.nsfw = $state(post?.nsfw ?? false)
|
||||||
this.altText = $state(post?.alt_text)
|
this.altText = $state(post?.alt_text)
|
||||||
this.language = $state(post?.language_id?.toString())
|
this.language = $state(post?.language_id?.toString())
|
||||||
|
this.poll = $state(
|
||||||
|
post?.poll ?? {
|
||||||
|
mode: 'single',
|
||||||
|
local_only: false,
|
||||||
|
choices: [
|
||||||
|
{
|
||||||
|
choice_text: 'Option 1',
|
||||||
|
id: 1,
|
||||||
|
num_votes: 0,
|
||||||
|
sort_order: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
choice_text: 'Option 2',
|
||||||
|
id: 2,
|
||||||
|
num_votes: 0,
|
||||||
|
sort_order: 1,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
this.event = $state(post?.event)
|
||||||
this.flairList = $state(post?.flair_list ?? [])
|
this.flairList = $state(post?.flair_list ?? [])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +74,10 @@ export class PostFormState {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async submit(postId?: number): Promise<PostView> {
|
async submit(
|
||||||
|
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')
|
||||||
|
|
||||||
@@ -61,6 +95,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,
|
||||||
|
event: type == 'event' ? this.event : undefined,
|
||||||
})
|
})
|
||||||
).post_view
|
).post_view
|
||||||
} else {
|
} else {
|
||||||
@@ -74,6 +110,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,
|
||||||
|
event: type == 'event' ? this.event : undefined,
|
||||||
})
|
})
|
||||||
).post_view
|
).post_view
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,6 @@
|
|||||||
{required}
|
{required}
|
||||||
{...rest}
|
{...rest}
|
||||||
rows={1}
|
rows={1}
|
||||||
class="font-semibold text-2xl focus:outline-hidden w-full bg-transparent resize-none {clazz}"
|
class={['focus:outline-hidden w-full bg-transparent resize-none', clazz]}
|
||||||
></textarea>
|
></textarea>
|
||||||
</label>
|
</label>
|
||||||
@@ -314,21 +314,25 @@
|
|||||||
.btn-square-sm {
|
.btn-square-sm {
|
||||||
width: calc(var(--spacing) * 6);
|
width: calc(var(--spacing) * 6);
|
||||||
height: calc(var(--spacing) * 6);
|
height: calc(var(--spacing) * 6);
|
||||||
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn-square-md {
|
.btn-square-md {
|
||||||
width: calc(var(--spacing) * 8);
|
width: calc(var(--spacing) * 8);
|
||||||
height: calc(var(--spacing) * 8);
|
height: calc(var(--spacing) * 8);
|
||||||
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn-square-lg {
|
.btn-square-lg {
|
||||||
width: calc(var(--spacing) * 9.5);
|
width: calc(var(--spacing) * 9.5);
|
||||||
height: calc(var(--spacing) * 9.5);
|
height: calc(var(--spacing) * 9.5);
|
||||||
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn-square-xl {
|
.btn-square-xl {
|
||||||
width: calc(var(--spacing) * 12);
|
width: calc(var(--spacing) * 12);
|
||||||
height: calc(var(--spacing) * 12);
|
height: calc(var(--spacing) * 12);
|
||||||
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.btn-disabled {
|
.btn-disabled {
|
||||||
|
|||||||
@@ -39,7 +39,7 @@
|
|||||||
|
|
||||||
.btn-group.btn-group-horizontal :global(> *) {
|
.btn-group.btn-group-horizontal :global(> *) {
|
||||||
position: relative;
|
position: relative;
|
||||||
border-radius: 8px 8px 8px 8px !important;
|
border-radius: 8px !important;
|
||||||
|
|
||||||
&:first-child {
|
&:first-child {
|
||||||
border-radius: 24px 12px 12px 24px !important;
|
border-radius: 24px 12px 12px 24px !important;
|
||||||
|
|||||||
@@ -7,13 +7,9 @@
|
|||||||
let { children } = $props()
|
let { children } = $props()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Header pageHeader>
|
{#if !site || !(site.data?.site_view.local_site.community_creation_admin_only && !profile.isAdmin)}
|
||||||
{$t('nav.create.label')}
|
|
||||||
{#snippet extended()}
|
|
||||||
{#if !site || !(site.data?.site_view.local_site.community_creation_admin_only && !profile.isAdmin)}
|
|
||||||
<Tabs
|
<Tabs
|
||||||
margin={false}
|
margin={false}
|
||||||
style="subpage"
|
|
||||||
routes={[
|
routes={[
|
||||||
{
|
{
|
||||||
href: '/create/post',
|
href: '/create/post',
|
||||||
@@ -25,8 +21,10 @@
|
|||||||
},
|
},
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
{/snippet}
|
|
||||||
|
<Header pageHeader>
|
||||||
|
{$t('nav.create.label')}
|
||||||
</Header>
|
</Header>
|
||||||
|
|
||||||
{@render children?.()}
|
{@render children?.()}
|
||||||
Reference in new issue
Block a user