Browse Source

fix disabled chan update -> moved joinChat to chat comp

master
nicolas-arnaud 2 years ago
parent
commit
33405a7b59
  1. 9
      front/volume/src/App.svelte
  2. 18
      front/volume/src/components/Channels.svelte
  3. 9
      front/volume/src/components/Chat.svelte

9
front/volume/src/App.svelte

@ -47,7 +47,7 @@
await formatChannelNames(channels); await formatChannelNames(channels);
const channel = channels.find((c: ChannelsType) => c.name === currentChannelName); const channel = channels.find((c: ChannelsType) => c.name === currentChannelName);
if (channel) { if (channel) {
//chan.selectChat(channel.id); // disabled as it causes a bug where joining a channel happen twice chan.selectChat(channel.id); // disabled as it causes a bug where joining a channel happen twice
} else { } else {
alert("Failed loading channel"); alert("Failed loading channel");
} }
@ -167,10 +167,7 @@
setAppState(APPSTATE.CHANNELS); setAppState(APPSTATE.CHANNELS);
} }
let selectedChannel: ChannelsType; let selectedChannel: ChannelsType;
let selectedMessages: Array<chatMessagesType>; const handleSelectChannel = (channel: ChannelsType) => {
const handleSelectChannel = (channel: ChannelsType, messages: Array<chatMessagesType>) => {
selectedChannel = channel;
selectedMessages = messages;
setAppState(APPSTATE.CHANNELS + "#" + channel.name); setAppState(APPSTATE.CHANNELS + "#" + channel.name);
}; };
@ -225,8 +222,8 @@
on:keydown={() => setAppState(APPSTATE.CHANNELS)} on:keydown={() => setAppState(APPSTATE.CHANNELS)}
> >
<Chat <Chat
{setAppState}
channel={selectedChannel} channel={selectedChannel}
messages={selectedMessages}
on:view-profile={openIdProfile} on:view-profile={openIdProfile}
on:add-friend={addFriend} on:add-friend={addFriend}
on:invite-to-game={pong.inviteToGame} on:invite-to-game={pong.inviteToGame}

18
front/volume/src/components/Channels.svelte

@ -115,8 +115,9 @@
//--------------------------------------------------------------------------------/ //--------------------------------------------------------------------------------/
export let onSelectChannel: (channel: ChannelsType, messages: Array<chatMessagesType>) => void; export let onSelectChannel: (channel: ChannelsType) => void;
let channel: ChannelsType; let channel: ChannelsType;
export const selectChat = (id: number) => { export const selectChat = (id: number) => {
console.log("channel: ", id) console.log("channel: ", id)
getChannels().then(() => { getChannels().then(() => {
@ -126,21 +127,10 @@
} else { } else {
show_popup("Did not find channel", false) show_popup("Did not find channel", false)
} }
}) })
}; };
socket.on("messages", (msgs: Array<chatMessagesType>) => {
console.log("You are joining channel: ", channel.name)
onSelectChannel(channel, msgs);
channel = undefined;
});
socket.on("failedJoin", (error: string) => {
show_popup(`Failed to join channel: ${error}`, false)
channel = undefined;
});
const createChannel = async () => { const createChannel = async () => {
let name: string; let name: string;
let password = ""; let password = "";
@ -268,7 +258,7 @@
<li> <li>
<span>{channel.name} : {channel.id}</span> <span>{channel.name} : {channel.id}</span>
<div style="display:block; margin-right:10%"> <div style="display:block; margin-right:10%">
<button on:click={() => selectChat(channel.id)}>🔌</button> <button on:click={() => onSelectChannel(channel)}>🔌</button>
<button <button
on:click={() => removeChannel(channel.id)} on:click={() => removeChannel(channel.id)}
on:keydown={() => removeChannel(channel.id)}>🗑️</button on:keydown={() => removeChannel(channel.id)}>🗑️</button

9
front/volume/src/components/Chat.svelte

@ -3,6 +3,7 @@
import { store, API_URL } from "../Auth"; import { store, API_URL } from "../Auth";
import { socket } from "../socket"; import { socket } from "../socket";
import { show_popup, content } from "./Alert/content"; import { show_popup, content } from "./Alert/content";
import { APPSTATE } from "../App.svelte";
import type { ChannelsType, chatMessagesType } from "./Channels.svelte"; import type { ChannelsType, chatMessagesType } from "./Channels.svelte";
import type User from "./Profile.svelte"; import type User from "./Profile.svelte";
</script> </script>
@ -15,6 +16,7 @@
export let messages: Array<chatMessagesType> = []; export let messages: Array<chatMessagesType> = [];
let newText = ""; let newText = "";
export let setAppState: (newState: APPSTATE | string) => void;
onMount(async () => { onMount(async () => {
getMembers(); getMembers();
usersInterval = setInterval(async () => { usersInterval = setInterval(async () => {
@ -22,7 +24,6 @@
}, 1000); }, 1000);
}); });
async function getMembers() { async function getMembers() {
if (!channel) return; if (!channel) return;
let res = await fetch(API_URL + "/users/blocked/", { let res = await fetch(API_URL + "/users/blocked/", {
@ -42,6 +43,12 @@
messages = [...messages, msg]; messages = [...messages, msg];
}); });
socket.on("failedJoin", (error: string) => {
show_popup(`Failed to join channel: ${error}`, false)
setAppState("/channels")
});
onDestroy(() => { onDestroy(() => {
clearInterval(usersInterval) clearInterval(usersInterval)
socket.disconnect(); socket.disconnect();

Loading…
Cancel
Save