feat: use search input for post creation, fix #24
This commit is contained in:
1 parent
9c9a3dc6a2
commit
9a77880305
2 files changed
+103
-16
No files matched your search
@@ -0,0 +1,49 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
|
||||||
|
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||||
|
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||||
|
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||||
|
import { createEventDispatcher } from 'svelte'
|
||||||
|
|
||||||
|
export let query: string
|
||||||
|
export let debounceTime: number = 300
|
||||||
|
|
||||||
|
type T = $$Generic
|
||||||
|
|
||||||
|
export let options: T[]
|
||||||
|
export let selected: T | null = null
|
||||||
|
export let extractSelected: (item: T | null) => any
|
||||||
|
export let extractName: (item: T) => string
|
||||||
|
|
||||||
|
const debounce = (fn: Function, ms = 300) => {
|
||||||
|
let timeoutId: ReturnType<typeof setTimeout>
|
||||||
|
return function (this: any, ...args: any[]) {
|
||||||
|
clearTimeout(timeoutId)
|
||||||
|
timeoutId = setTimeout(() => fn.apply(this, args), ms)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const dispatcher = createEventDispatcher<{ search: string }>()
|
||||||
|
|
||||||
|
const debounceFunc = debounce(() => {
|
||||||
|
dispatcher('search', query)
|
||||||
|
}, debounceTime)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div class="relative">
|
||||||
|
<TextInput
|
||||||
|
bind:value={query}
|
||||||
|
on:input={() => {
|
||||||
|
extractSelected(null)
|
||||||
|
debounceFunc()
|
||||||
|
}}
|
||||||
|
{...$$restProps}
|
||||||
|
/>
|
||||||
|
<Menu open={options.length != 0} alignment="bottom-left">
|
||||||
|
{#each options as option}
|
||||||
|
<MenuButton on:click={() => extractSelected(option)}>
|
||||||
|
{extractName(option)}
|
||||||
|
</MenuButton>
|
||||||
|
{/each}
|
||||||
|
</Menu>
|
||||||
|
</div>
|
||||||
@@ -10,6 +10,8 @@
|
|||||||
import Button from '$lib/components/input/Button.svelte'
|
import Button from '$lib/components/input/Button.svelte'
|
||||||
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
|
||||||
import { goto } from '$app/navigation'
|
import { goto } from '$app/navigation'
|
||||||
|
import SearchInput from '$lib/components/input/SearchInput.svelte'
|
||||||
|
import { Check, Icon } from 'svelte-hero-icons'
|
||||||
|
|
||||||
export let edit = false
|
export let edit = false
|
||||||
|
|
||||||
@@ -53,7 +55,7 @@
|
|||||||
const placeholder2 =
|
const placeholder2 =
|
||||||
placeholders[Math.floor(Math.random() * placeholders.length)]
|
placeholders[Math.floor(Math.random() * placeholders.length)]
|
||||||
|
|
||||||
let communities: Community[] | undefined
|
let communities: Community[] = []
|
||||||
|
|
||||||
const dispatcher = createEventDispatcher<{ submit: PostView }>()
|
const dispatcher = createEventDispatcher<{ submit: PostView }>()
|
||||||
|
|
||||||
@@ -66,8 +68,8 @@
|
|||||||
|
|
||||||
const list = await getClient().listCommunities({
|
const list = await getClient().listCommunities({
|
||||||
auth: $authData?.token,
|
auth: $authData?.token,
|
||||||
type_: 'Subscribed',
|
type_: 'All',
|
||||||
sort: 'New',
|
sort: 'Active',
|
||||||
limit: 40,
|
limit: 40,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -75,7 +77,14 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
async function submit() {
|
async function submit() {
|
||||||
if ((!data.community && !edit) || !data.title || !$authData) return
|
if (!data.community && !edit) {
|
||||||
|
toast({
|
||||||
|
type: ToastType.warning,
|
||||||
|
content: 'You need to set a community.',
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!data.title || !$authData) return
|
||||||
|
|
||||||
data.loading = true
|
data.loading = true
|
||||||
|
|
||||||
@@ -119,24 +128,53 @@
|
|||||||
toast({ content: err as any, type: ToastType.error })
|
toast({ content: err as any, type: ToastType.error })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let communitySearch = ''
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<form on:submit|preventDefault={submit} class="flex flex-col gap-4">
|
<form on:submit|preventDefault={submit} class="flex flex-col gap-4">
|
||||||
<h1 class="font-bold text-xl">{edit ? 'Edit' : 'Create'} Post</h1>
|
<h1 class="font-bold text-xl">{edit ? 'Edit' : 'Create'} Post</h1>
|
||||||
{#if !edit}
|
{#if !edit}
|
||||||
<div>
|
<div>
|
||||||
<span class="block my-1 font-bold text-sm">
|
<div class="flex flex-row">
|
||||||
Community <span class="text-red-500">*</span>
|
<span class="block my-1 font-bold text-sm">
|
||||||
</span>
|
Community <span class="text-red-500">*</span>
|
||||||
{#if communities}
|
</span>
|
||||||
<SelectMenu
|
{#if data.community}
|
||||||
bind:selected={data.community}
|
<Icon
|
||||||
options={communities.map((c) => c.id)}
|
src={Check}
|
||||||
optionNames={new Map(communities.map((c) => [c.id, c.title]))}
|
mini
|
||||||
/>
|
size="20"
|
||||||
{:else}
|
class="text-green-400 ml-auto inline"
|
||||||
<Dots />
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
|
</div>
|
||||||
|
<SearchInput
|
||||||
|
options={communities}
|
||||||
|
on:search={async () => {
|
||||||
|
const results = await getClient().search({
|
||||||
|
q: communitySearch,
|
||||||
|
auth: $authData?.token,
|
||||||
|
type_: 'Communities',
|
||||||
|
limit: 20,
|
||||||
|
sort: 'Active',
|
||||||
|
})
|
||||||
|
|
||||||
|
communities = results.communities.map((c) => c.community)
|
||||||
|
}}
|
||||||
|
debounceTime={600}
|
||||||
|
extractName={(c) => c.title}
|
||||||
|
bind:query={communitySearch}
|
||||||
|
extractSelected={(c) => {
|
||||||
|
if (!c) {
|
||||||
|
data.community = null
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data.community = c.id
|
||||||
|
communitySearch = c.name
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<TextInput required label="Title" bind:value={data.title} {placeholder} />
|
<TextInput required label="Title" bind:value={data.title} {placeholder} />
|
||||||
|
|||||||
Reference in new issue
Block a user