Skip to content

Commit

Permalink
refactor(chat): Reflect changes of the schema
Browse files Browse the repository at this point in the history
This also moves / adds some logic
  • Loading branch information
ArtieFuzzz committed Dec 20, 2022
1 parent 8577579 commit 6c76892
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.1.1",
"private": true,
"scripts": {
"dev": "vite dev --port 3000",
"dev": "vite dev --port 3000 --host",
"clean": "rm -r .turbo && rm -rf .svelte-kit",
"build": "vite build",
"preview": "vite preview",
Expand Down
31 changes: 18 additions & 13 deletions apps/chat/src/routes/chat/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { supabaseClient } from '$lib/supabaseClient';
import { onDestroy, onMount } from 'svelte';
type Message = { user: string; message: string; id: Date };
type Message = { user: string; content: string; id: Date };
const channel = supabaseClient.channel('global-messages');
let messages: Array<Message> = [];
Expand All @@ -16,9 +16,9 @@
};
const sendMessage = async () => {
const { data } = await supabaseClient.from('Message').insert({
const { data } = await supabaseClient.from('messages').insert({
user: userId,
message: currentMessage
content: currentMessage
});
currentMessage = '';
Expand All @@ -27,7 +27,7 @@
onMount(async () => {
await getCurrentUser();
const { error, data: preMessages } = await supabaseClient.from('Message').select().limit(10);
const { error, data: preMessages } = await supabaseClient.from('messages').select().order('id', { ascending: false }).limit(20);
if (error) {
console.error(error);
Expand All @@ -38,6 +38,15 @@
messages = [...preMessages];
channel.on(
'postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'messages', filter: '' },
(payload) => {
console.log(payload)
messages = [...messages, payload as unknown as Message];
}
);
channel.subscribe(async (status, err) => {
if (err) {
console.error(err);
Expand All @@ -48,14 +57,6 @@
console.debug('Connected to channel');
}
});
channel.on(
'postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'Message' },
(payload) => {
messages = [...messages, { ...(payload as unknown as Message) }];
}
);
});
onDestroy(() => {
Expand All @@ -65,9 +66,13 @@

<div class="grid grid-row">
<div class="messages-container justify-start items-start">
{#if messages.length > 0}
{#each messages as message}
<p class="message">{message.user} | {message.message}</p>
<p class="message">{message.user} | {message.content}</p>
{/each}
{:else}
<p class="messages">No messages were sent!</p>
{/if}
</div>

<div class="justify-center items-center">
Expand Down

0 comments on commit 6c76892

Please sign in to comment.