chore: replace favorite emitter

simply moving code into the right component was all it took
This commit is contained in:
2018-09-17 18:49:55 -07:00
parent 3ab623fea1
commit b4ce9557f3
2 changed files with 16 additions and 18 deletions

View File

@@ -31,7 +31,6 @@
<script>
import store from '../lib/store.mjs';
import emitter from '../lib/emitter.mjs';
import TagList from './TagList.vue';
export default {
@@ -50,8 +49,22 @@ export default {
},
methods: {
toggleFavorite() {
this.favorite = !this.favorite;
emitter.emit('favorite', { id: this.strain.id, isFav: this.favorite });
const isFav = !this.favorite;
const { id } = this.strain;
const favs = store.get('favorites') || [];
const idx = favs.indexOf(id);
// update component state
this.favorite = isFav;
// update the store
if (idx >= 0 && !isFav) {
// remove previously favorited strain
store.set('favorites', favs.filter(f => f !== id));
} else if (idx === -1 && isFav) {
// add new favorited strain
store.set('favorites', favs.concat(id));
}
},
},
};

View File

@@ -30,8 +30,6 @@
<script>
import SearchForm from '../components/SearchForm.vue';
import StrainList from '../components/StrainList.vue';
import emitter from '../lib/emitter.mjs';
import store from '../lib/store.mjs';
export default {
name: 'SearchPage',
@@ -136,19 +134,6 @@ export default {
},
},
created() {
// listen for favorite changes
emitter.on('favorite', ({ id, isFav }) => {
const favs = store.get('favorites') || [];
const idx = favs.indexOf(id);
// remove previously favorited strain
if (idx >= 0 && !isFav) {
store.set('favorites', favs.filter(f => f !== id));
} else if (idx === -1 && isFav) {
store.set('favorites', favs.concat(id));
}
});
this.updateMatches(); // set initial match list
},
};