Optimization

This commit is contained in:
syuilo 2017-11-12 03:04:04 +09:00
parent 679046f8a2
commit 10cb2a8332
5 changed files with 66 additions and 29 deletions

View file

@ -15,7 +15,7 @@ import User from '../../models/user';
*/ */
module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => {
// Get 'home' parameter // Get 'home' parameter
const [home, homeErr] = $(params.home).array().each( const [home, homeErr] = $(params.home).optional.array().each(
$().strict.object() $().strict.object()
.have('name', $().string()) .have('name', $().string())
.have('id', $().string()) .have('id', $().string())
@ -23,12 +23,38 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, re
.have('data', $().object())).$; .have('data', $().object())).$;
if (homeErr) return rej('invalid home param'); if (homeErr) return rej('invalid home param');
await User.update(user._id, { // Get 'id' parameter
$set: { const [id, idErr] = $(params.id).optional.string().$;
'client_settings.home': home if (idErr) return rej('invalid id param');
}
});
// Send response // Get 'data' parameter
res(); const [data, dataErr] = $(params.data).optional.object().$;
if (dataErr) return rej('invalid data param');
if (home) {
await User.update(user._id, {
$set: {
'client_settings.home': home
}
});
res();
} else {
if (id == null && data == null) return rej('you need to set id and data params if home param unset');
const _home = user.client_settings.home;
const widget = _home.find(w => w.id == id);
if (widget == null) return rej('widget not found');
widget.data = data;
await User.update(user._id, {
$set: {
'client_settings.home': _home
}
});
res();
}
}); });

View file

@ -73,10 +73,13 @@
if (this.view == 2) this.view = 0; if (this.view == 2) this.view = 0;
// Save view state // Save view state
this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.view = this.view;
this.api('i/update_home', { this.api('i/update_home', {
home: this.I.client_settings.home id: this.opts.id,
data: {
view: this.view
}
}).then(() => { }).then(() => {
this.I.client_settings.home.find(w => w.id == this.opts.id).data.view = this.view;
this.I.update(); this.I.update();
}); });
}; };

View file

@ -83,10 +83,13 @@
this.zap(); this.zap();
// Save state // Save state
this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.channel = this.channelId;
this.api('i/update_home', { this.api('i/update_home', {
home: this.I.client_settings.home id: this.opts.id,
data: {
channel: this.channelId
}
}).then(() => { }).then(() => {
this.I.client_settings.home.find(w => w.id == this.opts.id).data.channel = this.channelId;
this.I.update(); this.I.update();
}); });
}; };

View file

@ -81,10 +81,13 @@
if (this.view == 6) this.view = 0; if (this.view == 6) this.view = 0;
// Save view state // Save view state
this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.view = this.view;
this.api('i/update_home', { this.api('i/update_home', {
home: this.I.client_settings.home id: this.opts.id,
data: {
view: this.view
}
}).then(() => { }).then(() => {
this.I.client_settings.home.find(w => w.id == this.opts.id).data.view = this.view;
this.I.update(); this.I.update();
}); });
}; };

View file

@ -88,14 +88,7 @@
if (this.size == 2) this.size = 0; if (this.size == 2) this.size = 0;
this.applySize(); this.applySize();
this.save();
// Save state
this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.size = this.size;
this.api('i/update_home', {
home: this.I.client_settings.home
}).then(() => {
this.I.update();
});
}; };
this.change = () => { this.change = () => {
@ -147,14 +140,23 @@
i.one('selected', folder => { i.one('selected', folder => {
this.folder = folder ? folder.id : null; this.folder = folder ? folder.id : null;
this.fetch(); this.fetch();
this.save();
});
};
// Save state this.save = () => {
this.I.client_settings.home.filter(w => w.id == this.opts.id)[0].data.folder = this.folder; // Save state
this.api('i/update_home', { this.api('i/update_home', {
home: this.I.client_settings.home id: this.opts.id,
}).then(() => { data: {
this.I.update(); folder: this.folder,
}); size: this.size
}
}).then(() => {
const w = this.I.client_settings.home.find(w => w.id == this.opts.id);
w.data.folder = this.folder;
w.data.size = this.size;
this.I.update();
}); });
}; };
</script> </script>