You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

146 lines
4.1 KiB

<script lang="ts" context="module">
import type { chatMessagesType } from "./Chat.svelte";
export interface ChannelsType {
id: string;
name: string;
privacy: string;
password: string;
owner: string;
}
import { onMount } from "svelte";
import { API_URL, store } from "../Auth";
import { dataset_dev } from "svelte/internal";
</script>
<script lang="ts">
//--------------------------------------------------------------------------------/
export let channels: Array<ChannelsType> = [];
// onMount(async () => {
// const res = await fetch(API_URL + "/channels" + $store.ftId, {
// method: "GET",
// mode: "cors",
// });
// const data = await res.json();
// channels = data;
// });
//--------------------------------------------------------------------------------/
export let onSelectChannel: (channel: ChannelsType) => void;
const selectChat = (id: string) => {
const channel = channels.find((c) => c.id === id);
if (channel) {
onSelectChannel(channel);
}
};
//--------------------------------------------------------------------------------/
const createChannel = async () => {
const name = prompt("Enter a name for the new channel:");
if (name) {
const privacy = prompt(
"Enter a privacy setting for the new channel (public/private):"
);
if (privacy !== "public" && privacy !== "private") {
alert("Invalid privacy setting");
}
let password = "";
if (privacy === "private") {
password = prompt("Enter a password for the new channel:");
if (!password) {
alert("Invalid password");
}
}
if (privacy === "public" || password) {
const newChannel: ChannelsType = {
id: Math.random().toString(),
name,
owner: $store.username,
password,
privacy,
};
// const response = await fetch(API_URL + "/channels" + $store.ftId , {
// method: "POST",
// mode: "cors",
// body: JSON.stringify(newChannel),
// });
// const data = await response.json();
// if (data.ok) {
// channels = [newChannel, ...channels];
// } else {
// alert("Error creating channel");
// }
channels = [newChannel, ...channels];
}
}
};
//--------------------------------------------------------------------------------/
const removeChannel = async (id: string) => {
let string = prompt("type 'delete' to delete this channel");
if (string === "delete") {
// const response = await fetch(API_URL + "/channels" + $store.ftId + "/" + id, {
// method: "DELETE",
// mode: "cors",
// });
// const data = await response.json();
// if (data.ok) {
// channels = channels.filter((c) => c.id !== id);
// } else {
// alert("Error deleting channel");
// }
channels = channels.filter((c) => c.id !== id);
}
// TODO: save to database
};
//--------------------------------------------------------------------------------/
</script>
<div class="overlay">
<div class="channels" on:click|stopPropagation on:keydown|stopPropagation>
<div>
{#if channels.length > 0}
<h2>Channels</h2>
{#each channels.slice(0, 10) as _channels}
<li>
<span>{_channels.name}</span>
<button on:click={() => selectChat(_channels.id)}>Enter</button>
<button
on:click={() => removeChannel(_channels.id)}
on:keydown={() => removeChannel(_channels.id)}>delete</button
>
</li>{/each}
{:else}
<p>No channels available</p>
{/if}
<button on:click={createChannel}>Create Channel</button>
</div>
</div>
</div>
<style>
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
}
.channels {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 5px;
padding: 1rem;
width: 300px;
}
</style>