chore: replace favorite emitter
simply moving code into the right component was all it took
This commit is contained in:
@@ -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));
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user