From 1d8b828d1bbe0e0daa64f6fc2689799c7169afa0 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Thu, 19 Sep 2024 23:26:39 -0400 Subject: Add basic browser client Using Svelte. No tests, no linting, yet. This is just starting to get familiar with things. You'll still have to run the dev server and the dev client builder each in their own terminals. Enjoy! --- hi-ui/src/apiServer.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 hi-ui/src/apiServer.js (limited to 'hi-ui/src/apiServer.js') diff --git a/hi-ui/src/apiServer.js b/hi-ui/src/apiServer.js new file mode 100644 index 0000000..92f4dcc --- /dev/null +++ b/hi-ui/src/apiServer.js @@ -0,0 +1,29 @@ +import axios from 'axios'; + +export const apiServer = axios.create({ + baseURL: '/api/', +}); + +export async function boot() { + return apiServer.get('/boot'); +} + +export async function logIn(username, password) { + const data = { + name: username, + password, + }; + return apiServer.post('/auth/login', data); +} + +export async function logOut() { + return apiServer.post('/auth/logout', {}); +} + +export async function listChannels() { + return apiServer.get('/channels'); +} + +export async function createChannel(name) { + return apiServer.post('/channels', { name }); +} -- cgit v1.2.3 From 01d995c731c296292cd3f1f9a4702eb96a0bf628 Mon Sep 17 00:00:00 2001 From: Kit La Touche Date: Mon, 30 Sep 2024 23:14:36 -0400 Subject: Absorb and display events At least message-type ones, and at least without styling or memory-limit concerns. --- hi-ui/src/apiServer.js | 16 ++++++++++++++++ hi-ui/src/lib/ActiveChannel.svelte | 20 ++++++++++++++++---- hi-ui/src/lib/MessageInput.svelte | 17 +++++++++++++++-- hi-ui/src/routes/+page.svelte | 3 ++- hi-ui/src/store.js | 1 + 5 files changed, 50 insertions(+), 7 deletions(-) (limited to 'hi-ui/src/apiServer.js') diff --git a/hi-ui/src/apiServer.js b/hi-ui/src/apiServer.js index 92f4dcc..7365a36 100644 --- a/hi-ui/src/apiServer.js +++ b/hi-ui/src/apiServer.js @@ -1,4 +1,5 @@ import axios from 'axios'; +import { events } from './store'; export const apiServer = axios.create({ baseURL: '/api/', @@ -27,3 +28,18 @@ export async function listChannels() { export async function createChannel(name) { return apiServer.post('/channels', { name }); } + +export async function postToChannel(channelId, message) { + return apiServer.post(`/channels/${channelId}`, { message }); +} + +export function subscribeToEvents() { + const evtSource = new EventSource("/api/events"); + // TODO: this should process all incoming events and store them. + // TODO: eventually we'll need to handle expiring old info, so as not to use + // infinite browser memory. + evtSource.onmessage = (evt) => { + const data = JSON.parse(evt.data); + events.update((value) => [...value, data]); + } +} diff --git a/hi-ui/src/lib/ActiveChannel.svelte b/hi-ui/src/lib/ActiveChannel.svelte index 42aa53f..680a785 100644 --- a/hi-ui/src/lib/ActiveChannel.svelte +++ b/hi-ui/src/lib/ActiveChannel.svelte @@ -1,15 +1,27 @@
- Active channel: {channelName} + {#each messages as message} +
+
{message.at} @{message.sender.name}: {message.message.body}
+
+ {/each}
diff --git a/hi-ui/src/lib/CreateChannelForm.svelte b/hi-ui/src/lib/CreateChannelForm.svelte index 584fa61..70dc13d 100644 --- a/hi-ui/src/lib/CreateChannelForm.svelte +++ b/hi-ui/src/lib/CreateChannelForm.svelte @@ -7,7 +7,6 @@ let disabled = false; async function handleSubmit(event) { - event.preventDefault(); disabled = true; const response = await createChannel(name); if (200 <= response.status && response.status < 300) { @@ -18,7 +17,7 @@ } -
+
diff --git a/hi-ui/src/lib/LogIn.svelte b/hi-ui/src/lib/LogIn.svelte index df734ee..1ec6772 100644 --- a/hi-ui/src/lib/LogIn.svelte +++ b/hi-ui/src/lib/LogIn.svelte @@ -7,7 +7,6 @@ let password = ''; async function handleLogin(event) { - event.preventDefault(); disabled = true; const response = await logIn(username, password); if (200 <= response.status && response.status < 300) { @@ -19,7 +18,7 @@ } -
+
+ {/if} + + + diff --git a/hi-ui/src/lib/MessageInput.svelte b/hi-ui/src/lib/MessageInput.svelte index 96e9577..938e467 100644 --- a/hi-ui/src/lib/MessageInput.svelte +++ b/hi-ui/src/lib/MessageInput.svelte @@ -1,27 +1,25 @@ - - + +
diff --git a/hi-ui/src/routes/+page.svelte b/hi-ui/src/routes/+page.svelte index 646665e..66b4f8d 100644 --- a/hi-ui/src/routes/+page.svelte +++ b/hi-ui/src/routes/+page.svelte @@ -25,6 +25,7 @@ case 200: currentUser.update(() => ({ username: response.data.login.name, + id: response.data.login.id, })); subscribeToEvents(); break; @@ -66,7 +67,7 @@ -- cgit v1.2.3