ui: Refactor stores

This commit is contained in:
nemunaire 2022-11-02 10:07:44 +01:00
commit ffd43ac8e1
6 changed files with 136 additions and 86 deletions

View file

@ -5,24 +5,15 @@ import { stop_refresh } from './common';
let refresh_interval_teams = null;
function createTeamsStore() {
const { subscribe, set, update } = writable({teams:{}, teams_count: 0, rank: []});
const { subscribe, set, update } = writable({});
function updateFunc(res_teams, cb=null) {
if (res_teams.status === 200) {
res_teams.json().then((teams) => {
const teams_count = Object.keys(teams).length
const rank = [];
for (const tid in teams) {
teams[tid].id = Number(tid);
rank.push(teams[tid]);
}
rank.sort((a, b) => (a.rank > b.rank ? 1 : (a.rank == b.rank ? 0 : -1)));
update((t) => (Object.assign(t, {teams, teams_count, rank})));
update((t) => teams);
if (cb) {
cb(teams, teams_count, rank);
cb(teams);
}
});
}
@ -55,15 +46,33 @@ export const teamsStore = createTeamsStore();
export const teams = derived(
teamsStore,
($teamsStore) => ($teamsStore.teams)
($teamsStore) => {
const teams = {};
for (const tid in $teamsStore) {
teams[tid] = $teamsStore[tid];
teams[tid].id = Number(tid);
}
return teams;
}
);
export const teams_count = derived(
teamsStore,
($teamsStore) => ($teamsStore.teams_count)
($teamsStore) => Object.keys(teams).length
);
export const rank = derived(
teamsStore,
($teamsStore) => ($teamsStore.rank)
teams,
($teams) => {
const rank = [];
for (const tid in $teams) {
rank.push($teams[tid]);
}
rank.sort((a, b) => (a.rank > b.rank ? 1 : (a.rank == b.rank ? 0 : -1)));
return rank;
}
);