Skip to content

Commit

Permalink
[server,client] [js] theme updates now supported for sync accross dev…
Browse files Browse the repository at this point in the history
…ices
  • Loading branch information
arjunpat committed Jul 7, 2018
1 parent e7bcfec commit 9bc448d
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 25 deletions.
54 changes: 54 additions & 0 deletions app/api/bell-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,66 @@ class BellData {

user.settings.period_names = values;

let now = Date.now();
let arr = user.stats;

// if last period update was within the last 5 minutes, save stat as one
if (typeof arr.updated_period_names === 'object') {
if (arr.updated_period_names.length > 8)
for (let i = 1; i < arr.updated_period_names.length; i++)
arr.updated_period_names.splice(i, 1);

for (let i = arr.updated_period_names.length - 1; i >= 0; i--)
if (now - 300000 < arr.updated_period_names[i])
arr.updated_period_names.splice(i, 1);
else
break;
} else
arr.updated_period_names = [];

arr.updated_period_names.push(now);


await this.setObjectToUser('settings', user.email, user.settings);
await this.setObjectToUser('stats', user.email, user.stats);

return {};

}

async updateTheme(device_id, theme) {

let user = await this.getUserByDeviceId(device_id);

if (user.error) return { error: user.error };

user.settings.theme = theme;

let now = Date.now();
let arr = user.stats;

// if last theme update was within the last 5 minutes, save stat as one
if (typeof arr.updated_theme === 'object') {
if (arr.updated_theme.length > 8)
for (let i = 1; i < arr.updated_theme.length; i++)
arr.updated_theme.splice(i, 1);

for (let i = arr.updated_theme.length - 1; i >= 0; i--)
if (now - 300000 < arr.updated_theme[i])
arr.updated_theme.splice(i, 1);
else
break;
} else
arr.updated_theme = [];

arr.updated_theme.push(now);

await this.setObjectToUser('settings', user.email, user.settings);
await this.setObjectToUser('stats', user.email, user.stats);

return {};
}

updateUser(vals) {
return this.query(
'UPDATE users SET first_name = ?, last_name = ?, profile_pic = ? WHERE email = ?',
Expand Down
6 changes: 1 addition & 5 deletions app/api/plans/bell-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,12 @@ var schema = {
6: 'Spanish',
7: 'Survey Comp/Lit'
},
theme: [
'default', 'default_reverse' // last being most recent, delete when too long
]
theme: 'MVHS Light'
},
devices: {
'HJnbG8jDRG': 1526165118086 // date added to this profile
},
stats: {
created: 1526165118086,
viewed_last: 1526165118086,
updated_period_names: [1526165118086, 1526165189886]
}
}
Expand Down
37 changes: 26 additions & 11 deletions app/api/v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,26 @@ module.exports = async (path, postData) => {
if (!postData.data) return responses.missing_data;

let device_id = postData.device_id;
let data = postData.data;

switch (path) {
case '/init':

let {user_agent, platform, browser} = postData.data;
let {user_agent, platform, browser} = data;

if (user_agent && platform && browser) {
// if we need to register a new device

if (typeof browser === 'object') {
let keys = Object.keys(browser);
if (keys.length === 0)
postData.data.browser = 'unknown';
data.browser = 'unknown';
else
postData.data.browser = Object.keys(browser)[0];
data.browser = Object.keys(browser)[0];
}

return generateResponse(true, null, {
device_id: await bellData.createNewDevice(postData.data)
device_id: await bellData.createNewDevice(data)
});

} else if (device_id) {
Expand All @@ -75,7 +76,7 @@ module.exports = async (path, postData) => {
break;
case '/write/login':

let {email, first_name, last_name, profile_pic} = postData.data;
let {email, first_name, last_name, profile_pic} = data;

if (!device_id || !email || !first_name || !last_name || !profile_pic)
return responses.missing_data;
Expand All @@ -84,8 +85,8 @@ module.exports = async (path, postData) => {

if (emailUserData !== false) { // already have an account

if (!bellData.isThisMe(postData.data, emailUserData))
await bellData.updateUser(postData.data);
if (!bellData.isThisMe(data, emailUserData))
await bellData.updateUser(data);

await bellData.registerDevice(device_id, email);

Expand All @@ -101,7 +102,7 @@ module.exports = async (path, postData) => {
});
} else {

await bellData.createNewUser(postData.data);
await bellData.createNewUser(data);
await bellData.registerDevice(device_id, email);

return generateResponse(true, null, {
Expand All @@ -127,11 +128,10 @@ module.exports = async (path, postData) => {
return responses.bad_data;



break;
case '/write/analytics':

let {pathname, prefs, referrer, speed, new_load, registered_to} = postData.data;
let {pathname, prefs, referrer, speed, new_load, registered_to} = data;

if (!device_id || !pathname || !prefs || typeof referrer !== 'string' || !speed || typeof new_load !== 'boolean')
return responses.missing_data;
Expand Down Expand Up @@ -172,7 +172,6 @@ module.exports = async (path, postData) => {

break;
case '/update/period_names':
let {data} = postData;

if (!device_id || !data)
return responses.missing_data;
Expand All @@ -189,6 +188,22 @@ module.exports = async (path, postData) => {
else
return responses.success;

break;
case '/update/theme':

if (!device_id || !data)
return responses.missing_data;

if (typeof data.new_theme !== 'string' || data.new_theme.length > 20)
return responses.bad_data;

let val = await bellData.updateTheme(device_id, data.new_theme);

if (val.error)
return generateResponse(false, res.error);
else
return responses.success;

break;
default:
return responses.bad_path;
Expand Down
2 changes: 1 addition & 1 deletion build-client.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# dist/client/js cannot exist! delete before running
# client-dist/js cannot exist! delete before running


# javascript ---------------------------
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<span id="title">Your Settings</span>
<br>
<div id="changes-saved">
<span>In order to change settings, you must <a class="google-login-button">sign in to your Google account.</a></span>
<span><span style="text-transform: none;"><a class="google-login-button">Click here</a> to sign into Google and change settings</span></span>
</div>
<div id="choose-settings">
<form class="material-form">
Expand Down
16 changes: 13 additions & 3 deletions public/js/PrefManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PrefManager {
if (Storage.prefsExist())
this.setAllPreferences(Storage.getPrefs());
else // default prefs
this.setTheme(1);
this.setTheme(0);
}

initVars() {
Expand Down Expand Up @@ -108,11 +108,21 @@ class PrefManager {
this.theme.color.background = this.theme_options.background[num];
this.theme.color.text = this.theme_options.text[num];
this.theme.name = this.theme_options.name[num];
this.save();

if (this.isLoggedIn())
return RequestManager.updateTheme(this.theme.name).then(data => {
if (data.success) {
this.save();
return true;
}
return false;
});
else
this.save();
}

setThemeByName(name) {
this.setTheme(this.theme_options.name.indexOf(name));
return this.setTheme(this.theme_options.name.indexOf(name));
}

getThemeName() { return this.theme.name }
Expand Down
11 changes: 11 additions & 0 deletions public/js/RequestManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ class RequestManager {
}).then(res => res.json);
}

static updateTheme(new_theme) {
return this.ajax({
url: '/api/v1/update/theme',
type: 'POST',
data: JSON.stringify({
device_id: Storage.getDeviceId(),
data: { new_theme }
})
}).then(res => res.json);
}

static getTime() {
return this.ajax({
url: '/api/time'
Expand Down
10 changes: 6 additions & 4 deletions public/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ render.settings = () => {
elements.settings.themeSelector.onchange = () => {
let val = elements.settings.themeSelector.value;

prefManager.setThemeByName(val);

elements.applyPreferencesToElements(prefManager.getAllPreferences());

prefManager.setThemeByName(val).then(success => {
if (success)
elements.applyPreferencesToElements(prefManager.getAllPreferences());
else
window.alert('We are having trouble saving your theme change. Try again later.'); // TODO give some error!
});
}
}

Expand Down

0 comments on commit 9bc448d

Please sign in to comment.