ui: Fix loading problems when themes arrived to late

This commit is contained in:
nemunaire 2021-09-01 01:42:56 +02:00
commit 941e1c16d5
5 changed files with 40 additions and 41 deletions

View file

@ -1,50 +1,50 @@
import { derived, writable } from 'svelte/store';
function createThemesStore() {
const { subscribe, set, update } = writable({themes: {}, exercices_idx: {}, max_solved: 0});
const { subscribe, set, update } = writable({themes: null, exercices_idx: {}, max_solved: 0});
return {
subscribe,
update: (res_themes, cb=null) => {
update: async (res_themes, cb=null) => {
if (res_themes.status === 200) {
res_themes.json().then((themes) => {
let max_solved = 0;
const exercices_idx = {};
const themes = await res_themes.json();
for (let key in themes) {
const theme = themes[key];
let max_solved = 0;
const exercices_idx = {};
if (theme.solved > max_solved) {
max_solved = theme.solved;
for (let key in themes) {
const theme = themes[key];
if (theme.solved > max_solved) {
max_solved = theme.solved;
}
themes[key].exercice_count = Object.keys(theme.exercices).length;
themes[key].exercice_coeff_max = 0;
themes[key].max_gain = 0;
let last_exercice = null;
for (let k in theme.exercices) {
const exercice = theme.exercices[k];
themes[key].max_gain += exercice.gain;
if (themes[key].exercice_coeff_max < exercice.curcoeff) {
themes[key].exercice_coeff_max = exercice.curcoeff;
}
themes[key].exercice_count = Object.keys(theme.exercices).length;
themes[key].exercice_coeff_max = 0;
themes[key].max_gain = 0;
let last_exercice = null;
for (let k in theme.exercices) {
const exercice = theme.exercices[k];
if (last_exercice != null)
themes[key].exercices[last_exercice].next = k;
last_exercice = k;
themes[key].max_gain += exercice.gain;
if (themes[key].exercice_coeff_max < exercice.curcoeff) {
themes[key].exercice_coeff_max = exercice.curcoeff;
}
exercice.id = k;
exercices_idx[k] = exercice;
}
}
if (last_exercice != null)
themes[key].exercices[last_exercice].next = k;
last_exercice = k;
update((t) => (Object.assign(t, {themes, exercices_idx, max_solved})));
exercice.id = k;
exercices_idx[k] = exercice;
}
}
update((t) => (Object.assign(t, {themes, exercices_idx, max_solved})));
if (cb) {
cb(themes, exercices_idx, max_solved);
}
});
if (cb) {
cb(themes, exercices_idx, max_solved);
}
}
},
};