ui: Use $lib instead of ../../../../ mess
This commit is contained in:
parent
3cf92b4798
commit
3a6daa3d04
48 changed files with 81 additions and 81 deletions
67
frontend/ui/src/lib/stores/teams.js
Normal file
67
frontend/ui/src/lib/stores/teams.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
import { derived, writable } from 'svelte/store';
|
||||
|
||||
import { stop_refresh } from './common';
|
||||
|
||||
let refresh_interval_teams = null;
|
||||
|
||||
function createTeamsStore() {
|
||||
const { subscribe, set, update } = writable({teams:{}, teams_count: 0, rank: []});
|
||||
|
||||
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})));
|
||||
|
||||
if (cb) {
|
||||
cb(teams, teams_count, rank);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
|
||||
refresh: async (cb=null, interval=null) => {
|
||||
if (refresh_interval_teams)
|
||||
clearInterval(refresh_interval_teams);
|
||||
if (interval === null) {
|
||||
interval = Math.floor(Math.random() * 24000) + 32000;
|
||||
}
|
||||
if (stop_refresh) {
|
||||
return;
|
||||
}
|
||||
refresh_interval_teams = setInterval(refresh_teams, interval);
|
||||
|
||||
updateFunc(await fetch('teams.json', {headers: {'Accept': 'application/json'}}), cb);
|
||||
},
|
||||
|
||||
update: updateFunc,
|
||||
};
|
||||
}
|
||||
|
||||
export const teamsStore = createTeamsStore();
|
||||
|
||||
export const teams = derived(
|
||||
teamsStore,
|
||||
($teamsStore) => ($teamsStore.teams)
|
||||
);
|
||||
|
||||
export const teams_count = derived(
|
||||
teamsStore,
|
||||
($teamsStore) => ($teamsStore.teams_count)
|
||||
);
|
||||
|
||||
export const rank = derived(
|
||||
teamsStore,
|
||||
($teamsStore) => ($teamsStore.rank)
|
||||
);
|
Reference in a new issue