65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
import underscore from "underscore";
|
|
import "whatwg-fetch";
|
|
|
|
const DEFAULT_INIT = {
|
|
credentials: "include"
|
|
};
|
|
|
|
class DataSource {
|
|
constructor () {
|
|
this._router = null;
|
|
}
|
|
setRouter (router) {
|
|
this._router = router;
|
|
}
|
|
_fetch (requestOrURL, init) {
|
|
init = init || {};
|
|
|
|
let actualInit = underscore.extendOwn(
|
|
underscore.clone(DEFAULT_INIT), init
|
|
);
|
|
|
|
return window.fetch(requestOrURL, actualInit).catch((error) => {
|
|
this._router.push("/error/");
|
|
throw new Error("FetchError: Unable to fetch");
|
|
}).then((response) => {
|
|
if (!response.ok) {
|
|
let statusCode = response.status || "";
|
|
|
|
this._router.push("/error/" + statusCode);
|
|
throw new Error(
|
|
"FetchError: '" + statusCode + "'" + " '" + response.statusText + "'"
|
|
);
|
|
} else {
|
|
return response.json();
|
|
}
|
|
});
|
|
}
|
|
loadDashboardData () {
|
|
return this._fetch("/api/v1/dashboard");
|
|
}
|
|
loadDayChartData (day) {
|
|
return this._fetch("/api/v1/charts/day/" + day);
|
|
}
|
|
loadSessionData (day) {
|
|
return this._fetch("/api/v1/sessions/" + day);
|
|
}
|
|
loadPlayerGameData (player, game) {
|
|
return this._fetch("/api/v1/players/" + player + "/game/" + game);
|
|
}
|
|
loadPlayerGameChartsData (player, game) {
|
|
return this._fetch("/api/v1/charts/player/" + player + "/game/" + game);
|
|
}
|
|
loadPlayersData () {
|
|
return this._fetch("/api/v1/players");
|
|
}
|
|
loadPlayerStatsChartData (player, kind, mode) {
|
|
return this._fetch(
|
|
"/api/v1/charts/player/" + player + "/" + kind + "/" + mode
|
|
);
|
|
}
|
|
}
|
|
|
|
let sharedDataSource = new DataSource();
|
|
export default sharedDataSource;
|