From 01dbdbe1f478098ad0be6f21dedafb33ae511c15 Mon Sep 17 00:00:00 2001 From: Patrick Cleavelin Date: Tue, 28 Feb 2023 22:43:01 -0600 Subject: [PATCH] show intros in groups by guilds/channels --- src/IntroSelector.svelte | 13 +++++--- src/Login.svelte | 3 +- src/routes/+page.svelte | 70 ++++++++++++++++++++++------------------ src/store.ts | 40 ++++++++++++++--------- 4 files changed, 74 insertions(+), 52 deletions(-) diff --git a/src/IntroSelector.svelte b/src/IntroSelector.svelte index 1781f62..f923f69 100644 --- a/src/IntroSelector.svelte +++ b/src/IntroSelector.svelte @@ -5,6 +5,7 @@ export let guild = null; export let channel = null; + export let guildIntros = null; export let introList = null; export let exclude = null; export let btnLabel = 'Add'; @@ -25,20 +26,21 @@
- {#if !!introList} + {#if !!introList && !!guildIntros} {#if introList.length > 0} {#each introList as intro} {/each} {:else if !!emptyMsg}

{emptyMsg}

{:else} +

No intros

{/if} - {:else if !!exclude} - {#each $intros as intro, i} + {:else if !!exclude && !!guildIntros} + {#each guildIntros as intro, i} {#if (!exclude.map((e) => e.index).includes(i))}
diff --git a/src/Login.svelte b/src/Login.svelte index 83b388a..263c4bf 100644 --- a/src/Login.svelte +++ b/src/Login.svelte @@ -2,8 +2,9 @@ import { member, intros } from './store.ts'; const login = async (username) => { - await intros.fetchIntros(); await member._fakeLogin(username); + + await intros.fetchIntros($member.guilds); } let loginPromise = null; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index dbb539c..98e587e 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -3,42 +3,44 @@ import Login from '../Login.svelte'; import IntroSelector from '../IntroSelector.svelte'; - const addIntros = (event) => { - let guildIndex = $member.guilds.findIndex((e) => e.name === event.detail.guild); - console.log(guildIndex) + let addIntroPromise = null; - if (guildIndex >= 0) { - let channelIndex = $member.guilds[guildIndex].channels.findIndex((e) => e.name === event.detail.channel); - console.log(channelIndex) - - if (channelIndex >= 0) { - $member.guilds[guildIndex].channels[channelIndex].intros = - $member - .guilds[guildIndex] - .channels[channelIndex] - .intros - .concat( - event.detail.intros.map((index) => { - return { index: index, volume: 20 } - })); + const apiAddIntro = async (guild, channel, username, selectedIntros) => { + for (const intro of selectedIntros) { + const response = await fetch( + `http://localhost:7756/intros/${guild}/${channel}/${$member.username}/${intro}`, + { method: 'POST' } + ); + if (!response.ok) { + const body = await response.json(); + throw new Error(`${body}`); } } + + await member._fakeLogin(username); + }; + + const apiRemoveIntro = async (guild, channel, username, selectedIntros) => { + for (const intro of selectedIntros) { + const response = await fetch( + `http://localhost:7756/intros/${guild}/${channel}/${$member.username}/${intro}/remove`, + { method: 'POST' } + ); + if (!response.ok) { + const body = await response.json(); + throw new Error(`${body}`); + } + } + + await member._fakeLogin(username); + }; + + const addIntros = (event) => { + addIntroPromise = apiAddIntro(event.detail.guild, event.detail.channel, $member.username, event.detail.intros); + } const removeIntros = (event) => { - let guildIndex = $member.guilds.findIndex((e) => e.name == event.detail.guild); - - if (guildIndex >= 0) { - let channelIndex = $member.guilds[guildIndex].channels.findIndex((e) => e.name === event.detail.channel); - - if (channelIndex >= 0) { - $member.guilds[guildIndex].channels[channelIndex].intros = - $member - .guilds[guildIndex] - .channels[channelIndex] - .intros - .filter((intro) => !event.detail.intros.includes(intro)); - } - } + addIntroPromise = apiRemoveIntro(event.detail.guild, event.detail.channel, $member.username, event.detail.intros); } @@ -55,7 +57,12 @@ {#each guild.channels as channel}

{channel.name}

+ {#await addIntroPromise then result} + {:catch err} +

Failed to add intro: {err}

+ {/await} Add Intros { - const response = (await (await fetch("http://localhost:7756/intros/588149178912473103")).json()) - if (response !== "NoGuildFound") { - console.log(response.Intros[0].File); - const intros = response.Intros.map((intro) => { - if (!!intro.File) { - return { name: intro.File.friendlyName, filename: intro.File.filename } - } else if (!!intro.Online) { - return { name: intro.Online.friendlyName, url: intro.Online.url } - } - }) - console.log(intros) + fetchIntros: async (guilds) => { + console.debug('Fetching intros'); + console.log(guilds) - set(intros) + let intros = {}; + + for (const guild of guilds) { + const response = (await (await fetch(`http://localhost:7756/intros/${guild.name}`)).json()) + if (response !== "NoGuildFound") { + const guild_intros = response.Intros.map((intro) => { + if (!!intro.File) { + return { name: intro.File.friendlyName, filename: intro.File.filename } + } else if (!!intro.Online) { + return { name: intro.Online.friendlyName, url: intro.Online.url } + } + }) + + intros[guild.name] = guild_intros; + } } - }, + + console.debug('Setting Intros store'); + console.debug(intros); + console.debug(intros[123]); + set(intros) + } }