Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions app/src/api.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import $ from 'jquery';
import * as Cookies from "js-cookie";
import Cache from "./cache";

const URL = "https://hack.battlecode.org";
// const URL = 'http://localhost:8000'; // DEVELOPMENT
Expand Down Expand Up @@ -84,17 +85,26 @@ class Api {
}

static getUserTeam(callback) {
$.get(URL+"/api/userteam/"+encodeURIComponent(Cookies.get('username'))+"/"+LEAGUE+"/").done(function(data, status){
Cookies.set('team_id',data.id);
Cookies.set('team_name',data.name);

$.get(URL+"/api/"+LEAGUE+"/team/"+data.id+"/").done(function(data, status) {
callback(data);
});
}).fail(function(xhr, status, error) {
// possibly dangerous???
callback(null);
});

var requestURL = URL+"/api/userteam/"+encodeURIComponent(Cookies.get('username'))+"/"+LEAGUE+"/";
var data = Cache.getCache(requestURL);

if (data !==null) {
callback(data);
} else {
$.get(requestURL).done(function(data, status){
Cookies.set('team_id',data.id);
Cookies.set('team_name',data.name);

$.get(URL+"/api/"+LEAGUE+"/team/"+data.id+"/").done(function(data, status) {
Cache.setCache(requestURL, data);
callback(data);
});
}).fail(function(xhr, status, error) {
// possibly dangerous???
callback(null);
});
}
}

static updateTeam(params, callback) {
Expand Down Expand Up @@ -256,17 +266,41 @@ class Api {
}

static getUserProfile(callback) {
$.get(URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/").done(function(data, status) {
Cookies.set('user_url',data.url);
$.get(data.url).done(function(data, success) {
callback(data);
}).fail(function(xhr, status, error) {
console.log(error);
var profileURL = URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/";
var data = Cache.getCache(profileURL);

if (data!==null) {
callback(data);
var userURLData = Cache.getCache(data.url);
if (userURLData!==null) {
callback(userURLData);
} else {
$.get(data.url).done(function(userURLData, success) {
Cache.setCache(data.url, userURLData);
callback(userURLData);
}).fail(function(xhr, status, error) {
console.log(error);
});
}
} else {
$.get(profileURL).done(function(data, status) {
Cache.setCache(profileURL, data);
Cookies.set('user_url', data.url);

$.get(data.url).done(function(userURLData, success) {
Cache.setCache(data.url, userURLData);
callback(userURLData);
}).fail(function(xhr, status, error) {
console.log(error);
});
});
});
}
}

static updateUser(profile, callback) {
Cache.deleteCache(URL+"/api/user/profile/"+encodeURIComponent(Cookies.get('username'))+"/");
Cache.deleteCache(Cookies.get('user_url'));

$.ajax({
url:Cookies.get('user_url'),
data:JSON.stringify(profile),
Expand Down Expand Up @@ -297,6 +331,7 @@ class Api {
static logout(callback) {
Cookies.set('token',"");
Cookies.set('refresh',"");
Cache.clearCache();
callback();
}

Expand Down
36 changes: 36 additions & 0 deletions app/src/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

export default class Cache {
static setCache(url, data, timeout = 1800) {
data['expiry_time'] = (new Date()).getTime() + timeout * 1000;
window.localStorage.setItem(url, JSON.stringify(data));
console.log("Store:");
console.log(data);
}

static getCache(url) {
var cachedResult = window.localStorage.getItem(url);
console.log("getCache from url:");
console.log(url);
console.log(cachedResult);
console.log((new Date()).getTime());
if (cachedResult===null) {
return null;
} else {
var parsedResult = JSON.parse(cachedResult);
if ((new Date()).getTime() < parsedResult['expiry_time']) {
console.log("Cache Hit");
return parsedResult;
} else {
return null;
}
}
}

static deleteCache(url) {
window.localStorage.removeItem(url);
}

static clearCache() {
window.localStorage.clear();
}
}