✨ Improved ClassQuizController
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
<!--
|
||||
- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
-->
|
||||
<script lang="ts">
|
||||
import type { PageData } from './$types';
|
||||
import BrownButton from '$lib/components/buttons/brown.svelte';
|
||||
|
||||
import { getLocalization } from '$lib/i18n';
|
||||
const { t } = getLocalization();
|
||||
export let data: PageData;
|
||||
|
||||
console.log(data.controllers);
|
||||
@@ -12,24 +18,35 @@
|
||||
{#if controllers.length === 0}
|
||||
<div class="w-full h-full flex">
|
||||
<div class="m-auto">
|
||||
<BrownButton>Add new controller</BrownButton>
|
||||
<BrownButton href="/account/controllers/add"
|
||||
>{$t('controllers.add_new_controller')}</BrownButton
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="p-2">
|
||||
<div class="flex">
|
||||
<div class="mx-auto">
|
||||
<BrownButton href="/account/controllers/add"
|
||||
>{$t('controllers.add_new_controller')}</BrownButton
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
<table class="w-full">
|
||||
<tr class="border-b-2 dark:border-gray-500 text-left border-gray-300">
|
||||
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300">Name</th>
|
||||
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300"
|
||||
>Player name</th
|
||||
>{$t('words.name')}</th
|
||||
>
|
||||
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300"
|
||||
>First seen</th
|
||||
>
|
||||
>{$t('controllers.player_name')}
|
||||
</th>
|
||||
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300"
|
||||
>Last seen</th
|
||||
>
|
||||
<th class="mx-auto p-1">Version</th>
|
||||
>{$t('controllers.first_seen')}
|
||||
</th>
|
||||
<th class="border-r dark:border-gray-500 p-1 mx-auto border-gray-300"
|
||||
>{$t('controllers.last_seen')}
|
||||
</th>
|
||||
<th class="mx-auto p-1">{$t('words.version')}</th>
|
||||
</tr>
|
||||
{#each data.controllers as controller}
|
||||
<tr class="text-left">
|
||||
@@ -45,14 +62,14 @@
|
||||
<td class="border-r dark:border-gray-500 p-1 border-gray-300"
|
||||
>{controller.first_seen
|
||||
? new Date(controller.first_seen).toLocaleString()
|
||||
: 'Never'}</td
|
||||
: $t('words.never')}</td
|
||||
>
|
||||
<td class="border-r dark:border-gray-500 p-1 border-gray-300"
|
||||
>{controller.last_seen
|
||||
? new Date(controller.last_seen).toLocaleString()
|
||||
: 'Never'}</td
|
||||
: $t('words.never')}</td
|
||||
>
|
||||
<td class="mx-auto p-1">{controller.os_version ?? 'Unknown'}</td>
|
||||
<td class="mx-auto p-1">{controller.os_version ?? $t('words.unknown')}</td>
|
||||
</tr>
|
||||
{/each}
|
||||
</table>
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
<!--
|
||||
- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
-->
|
||||
<script lang="ts">
|
||||
import type { PageData } from './$types';
|
||||
import { SaveStatus } from './commons';
|
||||
import SaveIndicator from './SaveIndicator.svelte';
|
||||
import Spinner from '$lib/Spinner.svelte';
|
||||
import Cookies from 'js-cookie';
|
||||
import { browser } from '$app/environment';
|
||||
import BrownButton from '$lib/components/buttons/brown.svelte';
|
||||
import { getLocalization } from '$lib/i18n';
|
||||
|
||||
const { t } = getLocalization();
|
||||
export let data: PageData;
|
||||
const controller = data.controller;
|
||||
|
||||
let newest_version: string | undefined = undefined;
|
||||
|
||||
let saved_status = SaveStatus.Unchanged;
|
||||
|
||||
let save_player_timer;
|
||||
const save_player_name_debounce = () => {
|
||||
clearTimeout(save_player_timer);
|
||||
save_player_timer = setTimeout(save_names, 750);
|
||||
};
|
||||
|
||||
const save_names = async () => {
|
||||
if (!browser) {
|
||||
return;
|
||||
}
|
||||
saved_status = SaveStatus.Saving;
|
||||
const res = await fetch(`/api/v1/box-controller/web/modify`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: controller.id,
|
||||
player_name: controller.player_name,
|
||||
name: controller.name
|
||||
})
|
||||
});
|
||||
if (res.ok) {
|
||||
saved_status = SaveStatus.Saved;
|
||||
} else {
|
||||
saved_status = SaveStatus.Error;
|
||||
}
|
||||
};
|
||||
const get_latest_version = async () => {
|
||||
if (!browser) {
|
||||
return;
|
||||
}
|
||||
const cookie_data = Cookies.get('latest_cqc_version');
|
||||
if (cookie_data) {
|
||||
newest_version = cookie_data;
|
||||
return;
|
||||
}
|
||||
const res = await fetch(
|
||||
'https://api.github.com/repos/mawoka-myblock/ClassQuizController/releases'
|
||||
);
|
||||
const json = await res.json();
|
||||
newest_version = json[0].tag_name.replace('v', '');
|
||||
Cookies.set('latest_cqc_version', newest_version, { path: '', expires: 3600 });
|
||||
};
|
||||
|
||||
const allow_update_to_version = async () => {
|
||||
if (newest_version === controller.os_version) {
|
||||
return;
|
||||
}
|
||||
let wanted_version;
|
||||
if (newest_version === controller.wanted_os_version) {
|
||||
wanted_version = controller.os_version;
|
||||
} else {
|
||||
wanted_version = newest_version;
|
||||
}
|
||||
await fetch('/api/v1/box-controller/web/set_update', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: controller.id, version: wanted_version })
|
||||
});
|
||||
window.location.reload();
|
||||
};
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<div class="w-full flex flex-col">
|
||||
<h2 class="text-2xl mx-auto">{$t('controllers.controller_name')}</h2>
|
||||
<div class="mx-auto flex">
|
||||
<input
|
||||
class="rounded p-1 transition-all outline-none text-center dark:bg-gray-700"
|
||||
bind:value={controller.name}
|
||||
on:keyup={save_player_name_debounce}
|
||||
/>
|
||||
<SaveIndicator status={saved_status} />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex flex-col">
|
||||
<h2 class="mx-auto text-2xl">{$t('controllers.player_name')}</h2>
|
||||
<div class="mx-auto flex">
|
||||
<input
|
||||
class="rounded p-1 transition-all outline-none text-center dark:bg-gray-700"
|
||||
bind:value={controller.player_name}
|
||||
on:keyup={save_player_name_debounce}
|
||||
/>
|
||||
<SaveIndicator status={saved_status} />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex flex-col">
|
||||
<h2 class="mx-auto text-2xl">{$t('words.update')}</h2>
|
||||
{#await get_latest_version()}
|
||||
<Spinner my_20={false} />
|
||||
{:then _}
|
||||
<p class="mx-auto">
|
||||
{$t('controllers.version_overview', {
|
||||
newest_version,
|
||||
current_version: controller.os_version
|
||||
})}
|
||||
</p>
|
||||
{/await}
|
||||
{#if newest_version && controller.os_version}
|
||||
<div class="mx-auto">
|
||||
<BrownButton
|
||||
disabled={newest_version === controller.os_version}
|
||||
on:click={allow_update_to_version}
|
||||
>
|
||||
{#if newest_version === controller.os_version}
|
||||
{$t('controllers.already_latest_version')}
|
||||
{:else if newest_version === controller.wanted_os_version}
|
||||
{$t('controllers.cancel_update')}
|
||||
{:else}
|
||||
{$t('controllers.update_from_to', {
|
||||
newest_version,
|
||||
current_version: controller.os_version
|
||||
})}
|
||||
{/if}
|
||||
</BrownButton>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
import type { PageLoad } from './$types';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const load = (async ({ fetch, params }) => {
|
||||
const res = await fetch(`/api/v1/box-controller/web/controller?id=${params.controller_id}`);
|
||||
if (res.status !== 200) {
|
||||
throw error(res.status, await res.text());
|
||||
}
|
||||
const json: {
|
||||
id: string;
|
||||
player_name: string;
|
||||
last_seen?: string;
|
||||
first_seen?: string;
|
||||
name: string;
|
||||
os_version?: string;
|
||||
wanted_os_version?: string;
|
||||
} = await res.json();
|
||||
return {
|
||||
controller: json
|
||||
};
|
||||
}) satisfies PageLoad;
|
||||
@@ -0,0 +1,37 @@
|
||||
<!--
|
||||
- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
-->
|
||||
<script lang="ts">
|
||||
import { SaveStatus } from './commons';
|
||||
|
||||
export let status: SaveStatus;
|
||||
</script>
|
||||
|
||||
<div class="h-6 w-6 grow-0 my-auto">
|
||||
{#if status === SaveStatus.Saved}
|
||||
<svg
|
||||
class="h-full w-full"
|
||||
aria-hidden="true"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
viewBox="0 0 24 24"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path d="M5 13l4 4L19 7" stroke-linecap="round" stroke-linejoin="round" />
|
||||
</svg>
|
||||
{:else if status === SaveStatus.Saving}
|
||||
<svg class="max-h-full max-w-full h-full animate-spin w-full" viewBox="3 3 18 18">
|
||||
<path
|
||||
class="fill-black"
|
||||
d="M12 5C8.13401 5 5 8.13401 5 12C5 15.866 8.13401 19 12 19C15.866 19 19 15.866 19 12C19 8.13401 15.866 5 12 5ZM3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12Z"
|
||||
/>
|
||||
<path
|
||||
class="fill-blue-100"
|
||||
d="M16.9497 7.05015C14.2161 4.31648 9.78392 4.31648 7.05025 7.05015C6.65973 7.44067 6.02656 7.44067 5.63604 7.05015C5.24551 6.65962 5.24551 6.02646 5.63604 5.63593C9.15076 2.12121 14.8492 2.12121 18.364 5.63593C18.7545 6.02646 18.7545 6.65962 18.364 7.05015C17.9734 7.44067 17.3403 7.44067 16.9497 7.05015Z"
|
||||
/>
|
||||
</svg>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
/* eslint-disable no-unused-vars */
|
||||
export enum SaveStatus {
|
||||
Unchanged,
|
||||
Saved,
|
||||
Saving,
|
||||
Error
|
||||
}
|
||||
|
||||
/* eslint-enable */
|
||||
@@ -3,7 +3,6 @@
|
||||
import CodeDisplay from '$lib/components/controller/code.svelte';
|
||||
import Spinner from '$lib/Spinner.svelte';
|
||||
import { onMount } from 'svelte';
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
export let data: PageData;
|
||||
let controller_seen = false;
|
||||
|
||||
Reference in New Issue
Block a user