Compare commits
4 Commits
5665bdeaa9
...
4591a47dba
| Author | SHA1 | Date | |
|---|---|---|---|
| 4591a47dba | |||
| 50a3b1db92 | |||
| c652eee096 | |||
| a15deb9602 |
@@ -178,7 +178,7 @@
|
|||||||
<script>
|
<script>
|
||||||
// lunr = window.lunr
|
// lunr = window.lunr
|
||||||
// mitt = window.mitt
|
// mitt = window.mitt
|
||||||
(function ({ mitt, lunr }, data) {
|
(function ({ mitt, lunr, localStorage }, data) {
|
||||||
const emitter = mitt();
|
const emitter = mitt();
|
||||||
const stringAscending = (a, b) => {
|
const stringAscending = (a, b) => {
|
||||||
const aa = a.toLowerCase();
|
const aa = a.toLowerCase();
|
||||||
@@ -189,6 +189,23 @@
|
|||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
const getMultiValues = node => Array.from(node.selectedOptions).map(o => o.value);
|
const getMultiValues = node => Array.from(node.selectedOptions).map(o => o.value);
|
||||||
|
const store = {
|
||||||
|
get: id => JSON.parse(localStorage.getItem(id)),
|
||||||
|
set: (id, val) => localStorage.setItem(id, JSON.stringify(val)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// listeners
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// vue helpers
|
// vue helpers
|
||||||
Vue.filter('capitalize', value => {
|
Vue.filter('capitalize', value => {
|
||||||
@@ -222,9 +239,22 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
Vue.component('strain-card', {
|
Vue.component('strain-card', {
|
||||||
|
data() {
|
||||||
|
const favs = store.get('favorites') || [];
|
||||||
|
|
||||||
|
return {
|
||||||
|
favorite: favs.indexOf(this.strain.id) >= 0,
|
||||||
|
};
|
||||||
|
},
|
||||||
props: {
|
props: {
|
||||||
strain: Object,
|
strain: Object,
|
||||||
},
|
},
|
||||||
|
methods: {
|
||||||
|
toggleFavorite(id) {
|
||||||
|
this.favorite = !this.favorite;
|
||||||
|
emitter.emit('favorite', { id: this.strain.id, isFav: this.favorite });
|
||||||
|
}
|
||||||
|
},
|
||||||
template: `
|
template: `
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<header class="card-header">
|
<header class="card-header">
|
||||||
@@ -242,6 +272,11 @@
|
|||||||
<tag-list title="Conditions" :tags="strain.conditions" />
|
<tag-list title="Conditions" :tags="strain.conditions" />
|
||||||
<tag-list title="Flavors" :tags="strain.flavors" />
|
<tag-list title="Flavors" :tags="strain.flavors" />
|
||||||
</div>
|
</div>
|
||||||
|
<footer class="card-footer">
|
||||||
|
<div class="card-footer-item">
|
||||||
|
<button class="button" :class="{ 'is-success': favorite }" @click.prevent="toggleFavorite(strain.id)">Save</button>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
`,
|
`,
|
||||||
});
|
});
|
||||||
@@ -259,7 +294,10 @@
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
created() {
|
created() {
|
||||||
emitter.on('error', msg => this.error = msg);
|
emitter.on('error', this.setError);
|
||||||
|
},
|
||||||
|
beforeDestroy() {
|
||||||
|
emitter.off('error', this.setError);
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
handleSubmit() {
|
handleSubmit() {
|
||||||
@@ -276,7 +314,10 @@
|
|||||||
},
|
},
|
||||||
resetForm() {
|
resetForm() {
|
||||||
this.$el.reset();
|
this.$el.reset();
|
||||||
}
|
},
|
||||||
|
setError(msg) {
|
||||||
|
this.error = msg
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user