bw-hspc-contest-env/extension/bwcontest/webviews/components/Sidebar.svelte

116 lines
3.7 KiB
Svelte
Raw Normal View History

2023-05-06 00:01:27 -04:00
<script lang="ts">
import { onMount } from "svelte";
2023-05-06 00:01:27 -04:00
function postMessage(message: any) {
vscode.postMessage(message);
}
let teamname: HTMLInputElement;
let password: HTMLInputElement;
let sessionToken: string | undefined;
interface TeamData {
teamId: number,
contestId: number
}
let teamData: TeamData | undefined;
2023-05-06 00:01:27 -04:00
async function fetchTeamData() {
if (sessionToken) {
const res = await fetch(`http://localhost:5173/api/team/${sessionToken}`, {method: "GET"});
const data = await res.json();
if (!data.success) {
postMessage({type: 'onError', value: "BWContest: Failed to fetch team data"});
return;
}
teamData = data.data as TeamData;
}
}
async function onClone() {
if (teamData) {
postMessage({type: 'onClone', value: {contestId: teamData.contestId, teamId: teamData.teamId}});
}
}
2023-05-06 00:01:27 -04:00
async function onLogin() {
try {
const res = await fetch("http://localhost:5173/api/team/login", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
teamname: teamname.value, password: password.value
})});
if (res.status !== 200) {
postMessage({type: 'onError', value: 'Error logging in'});
return;
}
const data = await res.json();
if (data.success === false) {
postMessage({type: 'onError', value: data.message ?? "Unknown error logging in"});
return;
}
sessionToken = data.token;
postMessage({type: 'onLogin', value: sessionToken});
} catch (err) {
console.error('Failed to fetch:', err);
}
fetchTeamData();
}
async function onLogout() {
const res = await fetch("http://localhost:5173/api/team/logout", {method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({
token: sessionToken,
})})
2023-05-06 00:01:27 -04:00
if (res.status !== 200) {
postMessage({type: 'onError', value: 'Error logging out'});
sessionToken = undefined;
2023-05-06 00:01:27 -04:00
return;
}
const data = await res.json();
if (data.success === true) {
postMessage({type: 'onInfo', value: 'BWContest: Logged out'});
sessionToken = undefined;
postMessage({type: 'onLogout'});
} else {
postMessage({type: 'onError', value: 'Log out unsuccessful'});
2023-05-06 00:01:27 -04:00
}
}
window.addEventListener("message", event => {
const message = (event as MessageEvent).data;
if (message.type === "onSession") {
if (message.value !== "") {
sessionToken = message.value;
fetchTeamData();
}
}
})
2023-05-07 11:01:27 -04:00
function onTestAndSubmit() {
postMessage({type: 'onTestAndSubmit'});
}
onMount(() => {
postMessage({type: "onStartup"});
})
2023-05-06 00:01:27 -04:00
</script>
<h1>Contest</h1>
{#if sessionToken === undefined}
<label for="teamname">Team Name</label>
<input bind:this={teamname} id="teamname" type="text"/>
<label for="password">Password</label>
<input bind:this={password} id="password" type="password"/>
<button on:click={onLogin}>Login</button>
{:else}
<button on:click={onLogout}>Logout</button>
{#if teamData}
<p>TeamID: {teamData.teamId}</p>
<p>ContestID: {teamData.contestId}</p>
<button on:click={onClone}>Clone and Open Repo</button>
2023-05-07 11:01:27 -04:00
<button on:click={onTestAndSubmit}>Test & Submit</button>
{/if}
2023-05-06 00:01:27 -04:00
{/if}