From fc0f1654a56d2247728a766f43e72ff169704888 Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Sat, 15 Feb 2025 15:17:03 -0500
Subject: Hoist global state access out of individual components.

Access to "global" (maybe "external?") state is now handled at the top level of the component hierarchy, in `+page.svelte`, `+layout.svelte`, and their associated scripts. State is otherwise passed down through props, and changes are passed up through callbacks.

This is - hopefully - groundwork for refactoring state management a bit. I wanted to move access to state out to a smaller number of places, so that I have fewer places to update to implement reconnect logic. My broader goal is to make it easier to refactor these kinds of external side effects, as well, though no such changes are in this branch.

This change also makes testing a mile easier, since tests can interact with props and callbacks instead of emulating the whole HTTP request stack and the Pilcrow API. This change removes do-very-little tests.
---
 ui/lib/components/CreateChannelForm.svelte | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

(limited to 'ui/lib/components/CreateChannelForm.svelte')

diff --git a/ui/lib/components/CreateChannelForm.svelte b/ui/lib/components/CreateChannelForm.svelte
index 85c85bb..471c2b7 100644
--- a/ui/lib/components/CreateChannelForm.svelte
+++ b/ui/lib/components/CreateChannelForm.svelte
@@ -1,21 +1,22 @@
 <script>
-  import { createChannel } from '$lib/apiServer';
+  let { createChannel = async (name) => {} } = $props();
 
   let name = $state('');
   let disabled = $state(false);
 
-  async function handleSubmit(event) {
+  async function onsubmit(event) {
     event.preventDefault();
     disabled = true;
-    const response = await createChannel(name);
-    if (200 <= response.status && response.status < 300) {
-      name = '';
+    try {
+      await createChannel(name);
+      event.target.reset();
+    } finally {
+      disabled = false;
     }
-    disabled = false;
   }
 </script>
 
-<form onsubmit={handleSubmit}>
+<form {onsubmit}>
   <input type="text" placeholder="create channel" bind:value={name} {disabled} />
   <button type="submit">&#x2795;</button>
 </form>
-- 
cgit v1.2.3

