<script>
    import { postToChannel } from '../apiServer';
    import { activeChannel } from '../store';

    let input;
    let disabled = false;
    let activeChannelId;

    activeChannel.subscribe((value) => {
        activeChannelId = value ? value.id : null;
    });

    async function handleSubmit(event) {
        event.preventDefault();
        disabled = true;
        // TODO try/catch:
        await postToChannel(activeChannelId, input);
        input = '';
        disabled = false;
    }
</script>

<form on:submit={handleSubmit}>
    <input type="text" class="border rounded px-3" bind:value={input} disabled={disabled}>
    <button type="submit">➤</button>
</form>

<style>
    input[type="text"] {
        /* TODO: percentage won't handle zoom in/out well. */
        width: 96%;
    }
</style>
