Browse Source

private and public channels fixed, invite to private channel good

master
WalidMoovin 2 years ago
parent
commit
b85a1dfa5b
  1. 80
      front/volume/src/components/Channels.svelte

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

@ -33,6 +33,7 @@
}; };
//--------------------------------------------------------------------------------/ //--------------------------------------------------------------------------------/
const createChannel = async () => { const createChannel = async () => {
const name = prompt("Enter a name for the new channel:"); const name = prompt("Enter a name for the new channel:");
if (name) { if (name) {
@ -41,34 +42,28 @@
); );
if (privacy !== "public" && privacy !== "private") { if (privacy !== "public" && privacy !== "private") {
alert("Invalid privacy setting"); alert("Invalid privacy setting");
return;
} }
let password = ""; let password = "";
if (privacy === "private") { password = prompt("Enter a password for the new channel:");
password = prompt("Enter a password for the new channel:"); const response = await fetch(API_URL + "/channels", {
if (!password) { credentials: "include",
alert("Invalid password"); method: "POST",
} mode: "cors",
} headers: {
if (privacy === "public" || password) { "Content-Type": "application/json",
const response = await fetch(API_URL + "/channels", { },
credentials: "include", body: JSON.stringify({
method: "POST", name: name,
mode: "cors", owner: $store.ftId,
headers: { password: password || "",
"Content-Type": "application/json", isPrivate: privacy,
}, }),
body: JSON.stringify({ });
name: name, if (response.ok) {
owner: $store.ftId, channels.push(await response.json());
password: password || "", } else {
isPrivate: privacy === "private", alert("Error creating channel");
}),
});
if (response.ok) {
channels.push(await response.json());
} else {
alert("Error creating channel");
}
} }
} }
}; };
@ -89,6 +84,38 @@
}; };
//--------------------------------------------------------------------------------/ //--------------------------------------------------------------------------------/
const inviteChannel = async (id: number) => {
let string = prompt("Enter the username of the user you want to invite");
const response = await fetch(API_URL + "/users/" + string + "/byname", {
credentials: "include",
method: "GET",
mode: "cors",
});
if (response.ok) {
const user = await response.json();
const response2 = await fetch(API_URL + "/channels/" + id, {
credentials: "include",
method: "POST",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
userId: user.ftId,
}),
});
if (response2.ok) {
channels.push(await response2.json());
} else {
alert("Error inviting user");
}
} else {
alert("Error getting user infos");
}
};
//--------------------------------------------------------------------------------/
</script> </script>
<div class="overlay"> <div class="overlay">
@ -104,6 +131,7 @@
on:click={() => removeChannel(_channels.id)} on:click={() => removeChannel(_channels.id)}
on:keydown={() => removeChannel(_channels.id)}>delete</button on:keydown={() => removeChannel(_channels.id)}>delete</button
> >
<button on:click={() => inviteChannel(_channels.id)}>invite</button>
</li>{/each} </li>{/each}
{:else} {:else}
<p>No channels available</p> <p>No channels available</p>

Loading…
Cancel
Save