enh: update channel

This commit is contained in:
Timothy Jaeryang Baek
2024-12-22 23:09:51 -07:00
parent e9194d9524
commit 7ad8918cd9
9 changed files with 192 additions and 35 deletions

View File

@@ -1,33 +1,55 @@
<script lang="ts">
import { toast } from 'svelte-sonner';
import { onMount, getContext, createEventDispatcher, tick, onDestroy } from 'svelte';
import { onMount, getContext, tick, onDestroy } from 'svelte';
const i18n = getContext('i18n');
const dispatch = createEventDispatcher();
import { page } from '$app/stores';
import { mobile, showSidebar, user } from '$lib/stores';
import EllipsisHorizontal from '$lib/components/icons/EllipsisHorizontal.svelte';
import { updateChannelById } from '$lib/apis/channels';
import Cog6 from '$lib/components/icons/Cog6.svelte';
import ChannelModal from './ChannelModal.svelte';
export let onUpdate: Function = () => {};
export let className = '';
export let channel;
export let id;
export let name;
let showEditChannelModal = false;
let itemElement;
</script>
<ChannelModal
bind:show={showEditChannelModal}
{channel}
edit={true}
onSubmit={async ({ name, access_control }) => {
const res = await updateChannelById(localStorage.token, channel.id, {
name,
access_control
}).catch((error) => {
toast.error(error.message);
});
if (res) {
toast.success('Channel updated successfully');
}
onUpdate();
}}
/>
<div
bind:this={itemElement}
class=" w-full {className} rounded-lg flex relative group hover:bg-gray-100 dark:hover:bg-gray-900 {$page
.url.pathname === `/channels/${id}`
.url.pathname === `/channels/${channel.id}`
? 'bg-gray-100 dark:bg-gray-900'
: ''} px-2.5 py-1"
>
<a
class=" w-full flex justify-between"
href="/channels/{id}"
href="/channels/{channel.id}"
on:click={() => {
if ($mobile) {
showSidebar.set(false);
@@ -50,7 +72,7 @@
</svg>
<div class=" text-left self-center overflow-hidden w-full line-clamp-1">
{name}
{channel.name}
</div>
</div>
</a>
@@ -60,10 +82,12 @@
class="absolute z-10 right-2 invisible group-hover:visible self-center flex items-center dark:text-gray-300"
on:pointerup={(e) => {
e.stopPropagation();
showEditChannelModal = true;
}}
>
<button class="p-0.5 dark:hover:bg-gray-850 rounded-lg touch-auto" on:click={(e) => {}}>
<EllipsisHorizontal className="size-4" strokeWidth="2.5" />
<Cog6 className="size-3.5" />
</button>
</button>
{/if}

View File

@@ -1,15 +1,19 @@
<script lang="ts">
import { getContext, createEventDispatcher } from 'svelte';
import { getContext, createEventDispatcher, onMount } from 'svelte';
import { createNewChannel } from '$lib/apis/channels';
import Modal from '$lib/components/common/Modal.svelte';
import AccessControl from '$lib/components/workspace/common/AccessControl.svelte';
import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
import { toast } from 'svelte-sonner';
const i18n = getContext('i18n');
export let show = false;
export let onChange: Function = () => {};
export let onSubmit: Function = () => {};
export let channel = null;
export let edit = false;
let name = '';
let accessControl = null;
@@ -22,25 +26,41 @@
const submitHandler = async () => {
loading = true;
const res = await createNewChannel(localStorage.token, {
await onSubmit({
name: name.replace(/\s/g, '-'),
access_control: accessControl
}).catch((error) => {
toast.error(error);
return null;
});
onChange();
show = false;
loading = false;
};
const init = () => {
name = channel.name;
accessControl = channel.access_control;
};
$: if (channel) {
init();
}
let showDeleteConfirmDialog = false;
const deleteHandler = async () => {
showDeleteConfirmDialog = false;
show = false;
};
</script>
<Modal size="sm" bind:show>
<div>
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-1">
<div class=" text-lg font-medium self-center">{$i18n.t('Create Channel')}</div>
<div class=" text-lg font-medium self-center">
{#if edit}
{$i18n.t('Edit Channel')}
{:else}
{$i18n.t('Create Channel')}
{/if}
</div>
<button
class="self-center"
on:click={() => {
@@ -93,6 +113,18 @@
</div>
<div class="flex justify-end pt-3 text-sm font-medium gap-1.5">
{#if edit}
<button
class="px-3.5 py-1.5 text-sm font-medium dark:bg-black dark:hover:bg-black/90 dark:text-white bg-white text-black hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center"
type="button"
on:click={() => {
showDeleteConfirmDialog = true;
}}
>
{$i18n.t('Delete')}
</button>
{/if}
<button
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-950 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full flex flex-row space-x-1 items-center {loading
? ' cursor-not-allowed'
@@ -100,7 +132,11 @@
type="submit"
disabled={loading}
>
{$i18n.t('Create')}
{#if edit}
{$i18n.t('Update')}
{:else}
{$i18n.t('Create')}
{/if}
{#if loading}
<div class="ml-2 self-center">
@@ -136,3 +172,12 @@
</div>
</div>
</Modal>
<DeleteConfirmDialog
bind:show={showDeleteConfirmDialog}
message={$i18n.t('Are you sure you want to delete this channel?')}
confirmLabel={$i18n.t('Delete')}
on:confirm={() => {
deleteHandler();
}}
/>