misc: add settings pages

This commit is contained in:
Xyphyn committed 2023-08-13 13:00:00 -07:00
1 parent 7e76b1684c
commit f493f19c60
12 files changed
+270 -148

No files matched your search

@@ -1,6 +1,7 @@
<script lang="ts">
import { profile } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import { amMod } from '$lib/components/lemmy/moderation/moderation.js'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte'
import Card from '$lib/components/ui/Card.svelte'
@@ -10,10 +11,12 @@
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import { getClient } from '$lib/lemmy.js'
import { addSubscription } from '$lib/lemmy/user.js'
import { fullCommunityName } from '$lib/util.js'
import type { CommunityView } from 'lemmy-js-client'
import {
Calendar,
ChatBubbleOvalLeftEllipsis,
Cog6Tooth,
Icon,
PencilSquare,
UserGroup,
@@ -127,6 +130,19 @@
>
{community_view.blocked ? 'Unblock' : 'Block'}
</Button>
{#if $profile.user && amMod($profile.user, community_view.community)}
<div class="flex flex-row gap-2 ml-auto">
<Button
href="/c/{fullCommunityName(
community_view.community.name,
community_view.community.actor_id
)}/settings"
size="square-md"
>
<Icon src={Cog6Tooth} mini size="16" slot="icon" />
</Button>
</div>
{/if}
</div>
{/if}
<Markdown source={community_view.community.description} />
@@ -0,0 +1,142 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { profile } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import Checkbox from '$lib/components/input/Checkbox.svelte'
import FileInput from '$lib/components/input/FileInput.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient, uploadImage } from '$lib/lemmy.js'
import { addSubscription } from '$lib/lemmy/user.js'
/**
* The community ID to edit.
*/
export let edit: number | undefined = undefined
export let formData: {
name: string
displayName: string
icon: FileList | null
banner: FileList | null
sidebar: string
nsfw: boolean
postsLockedToModerators: boolean
submitting: boolean
} = {
name: '',
displayName: '',
icon: null,
banner: null,
sidebar: '',
nsfw: false,
postsLockedToModerators: false,
submitting: false,
}
async function submit() {
if (!$profile?.jwt) return
if ((!edit && formData.name == '') || formData.displayName == '') return
formData.submitting = true
try {
let icon = formData.icon ? await uploadImage(formData.icon[0]) : undefined
let banner = formData.banner
? await uploadImage(formData.banner[0])
: undefined
const res = edit
? await getClient().editCommunity({
auth: $profile.jwt,
title: formData.displayName,
description: formData.sidebar,
nsfw: formData.nsfw,
posting_restricted_to_mods: formData.postsLockedToModerators,
icon: icon,
banner: banner,
community_id: edit,
})
: await getClient().createCommunity({
auth: $profile.jwt,
name: formData.name,
title: formData.displayName,
description: formData.sidebar,
nsfw: formData.nsfw,
posting_restricted_to_mods: formData.postsLockedToModerators,
icon: icon,
banner: banner,
})
toast({
content: `Your community was ${edit ? 'saved' : 'created'}.`,
type: 'success',
})
if ($profile.user) {
$profile.user = {
...$profile.user,
moderates: [
...$profile.user.moderates,
{
community: res.community_view.community,
moderator: $profile.user.local_user_view.person,
},
],
}
addSubscription(res.community_view.community, true)
}
if (!edit)
goto(
`/c/${res.community_view.community.name}@${
new URL(res.community_view.community.actor_id).hostname
}`
)
} catch (err) {
toast({
content: err as any,
type: 'error',
})
}
formData.submitting = false
}
</script>
<form on:submit|preventDefault={submit} class="flex flex-col gap-4 h-full">
<slot name="formtitle">
<h1 class="text-2xl font-bold">Create Community</h1>
</slot>
<TextInput
required
label="Name"
bind:value={formData.name}
on:input={() => {
formData.name = formData.name.toLowerCase().replaceAll(' ', '_')
}}
disabled={edit}
/>
<TextInput required label="Display name" bind:value={formData.displayName} />
<FileInput label="Icon" bind:files={formData.icon} image />
<FileInput label="Banner" bind:files={formData.banner} image />
<MarkdownEditor previewButton label="Sidebar" bind:value={formData.sidebar} />
<Checkbox bind:checked={formData.nsfw}>NSFW</Checkbox>
<Checkbox bind:checked={formData.postsLockedToModerators}>
Only moderators can post
</Checkbox>
<Button
submit
color="primary"
size="lg"
class="mt-auto"
loading={formData.submitting}
disabled={formData.submitting}
>
{edit ? 'Save' : 'Create'}
</Button>
</form>
@@ -0,0 +1,16 @@
<script lang="ts">
import Avatar from '$lib/components/ui/Avatar.svelte'
import type { Community } from 'lemmy-js-client'
export let community: Community
</script>
<div class="flex flex-row gap-3 items-center">
<Avatar width={48} url={community.icon} alt={community.name} />
<div class="flex flex-col gap-0">
<h1 class="font-bold text-xl">{community.title}</h1>
<span class="dark:text-zinc-400 text-slate-600 text-sm">
!{community.name}@{new URL(community.actor_id).hostname}
</span>
</div>
</div>
@@ -61,7 +61,7 @@
}
if (passedCommunity) {
data.community = passedCommunity.id
data.community.community_view = passedCommunity.id
communitySearch = passedCommunity.name
} else {
const list = await getClient().listCommunities({
@@ -88,7 +88,7 @@
})
async function submit() {
if ((!data.community || communitySearch == '') && !edit) {
if ((!data.community.community_view || communitySearch == '') && !edit) {
toast({
type: 'warning',
content: 'You need to set a community.',
@@ -135,7 +135,7 @@
data.url = image || data.url || undefined
const post = await getClient().createPost({
auth: $profile.jwt,
community_id: data.community!,
community_id: data.community.community_view!,
name: data.title,
body: data.body,
url: data.url,
@@ -183,7 +183,7 @@
<span class="block my-1 font-bold text-sm">
Community <span class="text-red-500">*</span>
</span>
{#if data.community}
{#if data.community.community_view}
<Icon
src={Check}
mini
@@ -211,11 +211,11 @@
bind:query={communitySearch}
extractSelected={(c) => {
if (!c) {
data.community = null
data.community.community_view = null
return
}
data.community = c.id
data.community.community_view = c.id
communitySearch = `${c.name}@${new URL(c.actor_id).hostname}`
}}
/>
+1 -1
View File
@@ -86,7 +86,7 @@
}}
/>
<Checkbox
bind:checked={formData.community_creation_admin_only}
bind:checked={formdata.community.community_view_creation_admin_only}
defaultValue={true}
>
Only admins can create communities
+9
View File
@@ -0,0 +1,9 @@
<script lang="ts">
export let data
</script>
<svelte:head>
<title>{data.community.community_view.community.title}</title>
</svelte:head>
<slot />
@@ -21,11 +21,9 @@ export async function load(req: any) {
sort: sort,
auth: get(profile)?.jwt,
}),
community: (
await getClient(undefined, req.fetch).getCommunity({
name: req.params.name,
auth: get(profile)?.jwt,
})
).community_view,
community: await getClient(undefined, req.fetch).getCommunity({
name: req.params.name,
auth: get(profile)?.jwt,
}),
}
}
+18 -13
View File
@@ -25,10 +25,10 @@
onMount(() => {
setSessionStorage('lastSeenCommunity', {
id: data.community.community.id,
id: data.community.community_view.community.id,
name: fullCommunityName(
data.community.community.name,
data.community.community.actor_id
data.community.community_view.community.name,
data.community.community_view.community.actor_id
),
})
})
@@ -36,11 +36,14 @@
<svelte:head>
<title>{data.posts.posts[0]?.community.name}</title>
<meta name="og:title" content={data.community.community.name} />
{#if data.community.community.description}
<meta
name="og:title"
content={data.community.community_view.community.name}
/>
{#if data.community.community_view.community.description}
<meta
name="og:description"
content={data.community.community.description}
content={data.community.community_view.community.description}
/>
{/if}
<meta name="og:url" content={$page.url.toString()} />
@@ -49,7 +52,7 @@
<Modal bind:open={sidebar}>
<span slot="title">About</span>
<div class="mx-auto">
<CommunityCard community_view={data.community} />
<CommunityCard community_view={data.community.community_view} />
</div>
</Modal>
@@ -58,14 +61,16 @@
<div class="flex flex-row gap-3 items-center">
<Avatar
width={48}
url={data.community.community.icon}
alt={data.community.community.name}
url={data.community.community_view.community.icon}
alt={data.community.community_view.community.name}
/>
<div class="flex flex-col gap-0">
<h1 class="font-bold text-xl">{data.community.community.title}</h1>
<h1 class="font-bold text-xl">
{data.community.community_view.community.title}
</h1>
<span class="dark:text-zinc-400 text-slate-600 text-sm">
!{data.community.community.name}@{new URL(
data.community.community.actor_id
!{data.community.community_view.community.name}@{new URL(
data.community.community_view.community.actor_id
).hostname}
</span>
</div>
@@ -85,6 +90,6 @@
/>
</div>
<div class="hidden xl:block">
<CommunityCard community_view={data.community} />
<CommunityCard community_view={data.community.community_view} />
</div>
</div>
@@ -0,0 +1,27 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { page } from '$app/stores'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import { fullCommunityName } from '$lib/util.js'
import type { PageData } from './$types.js'
export let data: PageData
$: communityUrl = `/c/${fullCommunityName(
data.community.community_view.community.name,
data.community.community_view.community.actor_id
)}`
</script>
<div class="flex flex-col gap-4 h-full">
<MultiSelect
headless
options={[`${communityUrl}/settings`, `${communityUrl}/settings/team`]}
optionNames={['Settings', 'Team']}
selected={$page.url.pathname}
on:select={(e) => {
goto(e.detail)
}}
/>
<slot />
</div>
+27
View File
@@ -0,0 +1,27 @@
<script lang="ts">
import CommunityForm from '$lib/components/lemmy/community/CommunityForm.svelte'
import CommunityTitle from '$lib/components/lemmy/community/CommunityTitle.svelte'
export let data
</script>
<div class="flex flex-col gap-4">
<CommunityTitle community={data.community.community_view.community} />
<h1 class="font-bold text-2xl">Settings</h1>
<CommunityForm
edit={data.community.community_view.community.id}
formData={{
name: data.community.community_view.community.name,
displayName: data.community.community_view.community.title,
nsfw: data.community.community_view.community.nsfw,
postsLockedToModerators:
data.community.community_view.community.posting_restricted_to_mods,
sidebar: data.community.community_view.community.description ?? '',
icon: null,
banner: null,
submitting: false,
}}
>
<svelte:fragment slot="formtitle">{''}</svelte:fragment>
</CommunityForm>
</div>
Whitespace-only changes.
+4 -122
View File
@@ -1,125 +1,7 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { profile } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import Checkbox from '$lib/components/input/Checkbox.svelte'
import FileInput from '$lib/components/input/FileInput.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient, uploadImage } from '$lib/lemmy.js'
import { addSubscription } from '$lib/lemmy/user.js'
let formData: {
name: string
displayName: string
icon: FileList | null
banner: FileList | null
sidebar: string
nsfw: boolean
postsLockedToModerators: boolean
submitting: boolean
} = {
name: '',
displayName: '',
icon: null,
banner: null,
sidebar: '',
nsfw: false,
postsLockedToModerators: false,
submitting: false,
}
async function submit() {
if (!$profile?.jwt) return
if (formData.name == '' || formData.displayName == '') return
formData.submitting = true
try {
let icon = formData.icon ? await uploadImage(formData.icon[0]) : undefined
let banner = formData.banner
? await uploadImage(formData.banner[0])
: undefined
const res = await getClient().createCommunity({
auth: $profile.jwt,
name: formData.name,
title: formData.displayName,
description: formData.sidebar,
nsfw: formData.nsfw,
posting_restricted_to_mods: formData.postsLockedToModerators,
icon: icon,
banner: banner,
})
toast({
content: 'Your community was created.',
type: 'success',
})
if ($profile.user) {
$profile.user = {
...$profile.user,
moderates: [
...$profile.user.moderates,
{
community: res.community_view.community,
moderator: $profile.user.local_user_view.person,
},
],
}
addSubscription(res.community_view.community, true)
}
goto(
`/c/${res.community_view.community.name}@${
new URL(res.community_view.community.actor_id).hostname
}`
)
} catch (err) {
toast({
content: err as any,
type: 'error',
})
}
formData.submitting = false
}
import CommunityForm from '$lib/components/lemmy/community/CommunityForm.svelte'
</script>
<form
on:submit|preventDefault={submit}
class="flex flex-col gap-4 h-full max-w-2xl mx-auto"
>
<h1 class="text-2xl font-bold">Create Community</h1>
<TextInput
required
label="Name"
bind:value={formData.name}
on:input={() => {
formData.name = formData.name.toLowerCase().replaceAll(' ', '_')
}}
/>
<TextInput required label="Display name" bind:value={formData.displayName} />
<FileInput label="Icon" bind:files={formData.icon} image />
<FileInput label="Banner" bind:files={formData.banner} image />
<MarkdownEditor previewButton label="Sidebar" bind:value={formData.sidebar} />
<Checkbox bind:checked={formData.nsfw}>NSFW</Checkbox>
<Checkbox bind:checked={formData.postsLockedToModerators}>
Only moderators can post
</Checkbox>
<Button
submit
color="primary"
size="lg"
class="mt-auto"
loading={formData.submitting}
disabled={formData.submitting}
>
Create
</Button>
</form>
<div class=" max-w-2xl mx-auto">
<CommunityForm />
</div>