<script>
    import { Input, InputAddon, ButtonGroup, Button } from 'flowbite-svelte';
    import { CaretRightSolid } from 'flowbite-svelte-icons';

    import { tick } from 'svelte';
    import { postToChannel } from '../apiServer';
    import { activeChannel } from '../store';

    let self;
    let input;
    $: disabled = $activeChannel == null;

    async function handleSubmit(event) {
        disabled = true;
        // TODO try/catch:
        await postToChannel($activeChannel?.id, input);
        input = '';
        disabled = false;
        await tick();
        self.focus();
    }
</script>

<form on:submit|preventDefault={handleSubmit}>
    <ButtonGroup class="w-full">
        <Input disabled={disabled} bind:this={self} bind:value={input} />
        <Button color="primary" class="!p-2.5" type="submit">
            <CaretRightSolid class="w-5 h-5" />
        </Button>
    </ButtonGroup>
</form>
