Save favourites to db

This commit is contained in:
Jiiks 2018-08-22 18:49:20 +03:00
parent 58a2b6714e
commit d274fda428
1 changed files with 24 additions and 8 deletions

View File

@ -60,16 +60,18 @@ export default new class EmoteModule extends BuiltinModule {
{ {
text: 'Favourite', text: 'Favourite',
type: 'toggle', type: 'toggle',
checked: target && target.alt ? this.favourites.find(e => e.alt === target.alt) : false, checked: target && target.alt ? this.favourites.find(e => e.name === target.alt.replace(/;/g, '')) : false,
onChange: (checked, target) => { onChange: (checked, target) => {
const { alt } = target; const { alt } = target;
if (checked) { if (!alt) return false;
this.favourites.push({ alt: target.alt });
return true; const name = alt.replace(/;/g, '');
}
if (!this._favourites) this._favourites = []; if (!checked) return this.removeFavourite(name);
this._favourites = this._favourites.filter(e => e.alt !== target.alt);
return false; const emote = this.findByName(name, true);
if (!emote) return false;
return this.addFavourite(emote);
} }
} }
], filter => filter.closest('.bd-emote')); ], filter => filter.closest('.bd-emote'));
@ -91,6 +93,20 @@ export default new class EmoteModule extends BuiltinModule {
MonkeyPatch('BD:EMOTEMODULE', ImageWrapper.component.prototype).after('render', this.beforeRenderImageWrapper.bind(this)); MonkeyPatch('BD:EMOTEMODULE', ImageWrapper.component.prototype).after('render', this.beforeRenderImageWrapper.bind(this));
} }
addFavourite(emote) {
if (this.favourites.find(e => e.name === emote.name)) return true;
this.favourites.push(emote);
Database.insertOrUpdate({ 'id': 'EmoteModule' }, { 'id': 'EmoteModule', favourites: this.favourites, mostused: this.mostUsed })
return true;
}
removeFavourite(name) {
if (!this.favourites.find(e => e.name === name)) return false;
this._favourites = this._favourites.filter(e => e.name !== name);
Database.insertOrUpdate({ 'id': 'EmoteModule' }, { 'id': 'EmoteModule', favourites: this.favourites, mostused: this.mostUsed })
return false;
}
async disabled() { async disabled() {
// Unpatch all patches // Unpatch all patches
for (const patch of Patcher.getPatchesByCaller('BD:EMOTEMODULE')) patch.unpatch(); for (const patch of Patcher.getPatchesByCaller('BD:EMOTEMODULE')) patch.unpatch();