frontend: Refactor waitDiff func

This commit is contained in:
nemunaire 2023-11-04 20:12:53 +01:00
parent ac39fe2493
commit a0c34018cf
2 changed files with 31 additions and 25 deletions

View File

@ -16,8 +16,8 @@
import { blake2b } from 'hash-wasm'; import { blake2b } from 'hash-wasm';
import { my } from '$lib/stores/my.js'; import { my } from '$lib/stores/my.js';
import { teamsStore } from '$lib/stores/teams.js';
import { settings } from '$lib/stores/settings.js'; import { settings } from '$lib/stores/settings.js';
import { timeouted, waitDiff, waitInProgress } from '$lib/wait.js';
import DateFormat from './DateFormat.svelte'; import DateFormat from './DateFormat.svelte';
import FlagKey from './FlagKey.svelte'; import FlagKey from './FlagKey.svelte';
@ -27,24 +27,10 @@
export let flags = []; export let flags = [];
export let readonly = false; export let readonly = false;
function waitDiff(i) {
my.refresh((my) => {
if (my && (my.exercices[exercice.id].tries != exercice.tries || my.exercices[exercice.id].solved_rank != exercice.solved_rank || my.exercices[exercice.id].solved_time != exercice.solved_time)) {
submitInProgress = false;
teamsStore.refresh();
} else if (i > 0) {
setTimeout(waitDiff, (12-i)*50+440, i-1);
} else {
timeouted = true;
submitInProgress = false;
}
})
}
export let forcesolved = false; export let forcesolved = false;
let responses = { }; let responses = { };
async function submitFlags() { async function submitFlags() {
submitInProgress = true; waitInProgress.set(true);
sberr = ""; sberr = "";
message = ""; message = "";
@ -91,7 +77,7 @@
exercice.solved_time = new Date(); exercice.solved_time = new Date();
exercice = exercice; exercice = exercice;
submitInProgress = false; waitInProgress.set(false);
} else { } else {
const response = await fetch( const response = await fetch(
"submit/" + exercice.id, "submit/" + exercice.id,
@ -106,9 +92,9 @@
const data = await response.json(); const data = await response.json();
messageClass = 'text-success'; messageClass = 'text-success';
message = data.errmsg; message = data.errmsg;
waitDiff(13); waitDiff(13, exercice);
} else { } else {
submitInProgress = false; waitInProgress.set(false);
messageClass = 'text-danger'; messageClass = 'text-danger';
@ -146,8 +132,6 @@
let sberr = ""; let sberr = "";
let message = ""; let message = "";
let messageClass = "text-danger"; let messageClass = "text-danger";
let timeouted = false;
let submitInProgress = false;
</script> </script>
<Card class="border-danger mb-2"> <Card class="border-danger mb-2">
@ -165,7 +149,7 @@
{exercice.flags.filter((f) => f.type !== undefined).length}/{exercice.nb_flags} {exercice.flags.filter((f) => f.type !== undefined).length}/{exercice.nb_flags}
</Progress> </Progress>
{/if} {/if}
{#if exercice.tries || exercice.solved_time || exercice.submitted || sberr || timeouted} {#if exercice.tries || exercice.solved_time || exercice.submitted || sberr || $timeouted}
<ListGroup class="border-dark"> <ListGroup class="border-dark">
{#if exercice.solved_time || exercice.tries} {#if exercice.solved_time || exercice.tries}
<ListGroupItem class="text-warning rounded-0"> <ListGroupItem class="text-warning rounded-0">
@ -187,7 +171,7 @@
{/if} {/if}
</ListGroupItem> </ListGroupItem>
{/if} {/if}
{#if timeouted} {#if $timeouted}
<ListGroupItem class="text-danger rounded-0"> <ListGroupItem class="text-danger rounded-0">
<strong>Oops</strong> <strong>Oops</strong>
La requête a dépassé le délai d'attente. Vous devriez réessayer dans quelques instant&hellip; La requête a dépassé le délai d'attente. Vous devriez réessayer dans quelques instant&hellip;
@ -225,9 +209,9 @@
type="submit" type="submit"
color="danger" color="danger"
id="submission-{exercice.id}" id="submission-{exercice.id}"
disabled={submitInProgress || $settings.disablesubmitbutton || readonly} disabled={$waitInProgress || $settings.disablesubmitbutton || readonly}
> >
{#if submitInProgress} {#if $waitInProgress}
<Spinner size="sm" class="me-2" /> <Spinner size="sm" class="me-2" />
{/if} {/if}
Soumettre Soumettre

View File

@ -0,0 +1,22 @@
import { writable } from 'svelte/store';
import { my } from '$lib/stores/my.js';
import { teamsStore } from '$lib/stores/teams.js';
export let waitInProgress = writable(false);
export let timeouted = writable(false);
export function waitDiff(i, exercice) {
timeouted.set(false);
my.refresh((my) => {
if (my && (my.exercices[exercice.id].tries != exercice.tries || my.exercices[exercice.id].solved_rank != exercice.solved_rank || my.exercices[exercice.id].solved_time != exercice.solved_time)) {
waitInProgress.set(false);
teamsStore.refresh();
} else if (i > 0) {
setTimeout(waitDiff, (12-i)*50+440, i-1, exercice);
} else {
timeouted.set(true);
waitInProgress.set(false);
}
})
}