feat: show errors, sort tags
This commit is contained in:
@@ -70,6 +70,7 @@
|
|||||||
<label class="label">Search By Name</label>
|
<label class="label">Search By Name</label>
|
||||||
<div class="control">
|
<div class="control">
|
||||||
<input ref="name" class="input" type="text" placeholder="Text input">
|
<input ref="name" class="input" type="text" placeholder="Text input">
|
||||||
|
<p v-if="error.length" class="help is-danger">{{error}}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -168,6 +169,12 @@
|
|||||||
// mitt = window.mitt
|
// mitt = window.mitt
|
||||||
(function ({ mitt, lunr }, data) {
|
(function ({ mitt, lunr }, data) {
|
||||||
const emitter = mitt();
|
const emitter = mitt();
|
||||||
|
const stringAscending = (a, b) => {
|
||||||
|
const aa = a.toLowerCase();
|
||||||
|
const bb = b.toLowerCase();
|
||||||
|
if (aa === bb) return 0;
|
||||||
|
return aa < bb ? -1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
// helpers
|
// helpers
|
||||||
const getMultiValues = node => Array.from(node.selectedOptions).map(o => o.value);
|
const getMultiValues = node => Array.from(node.selectedOptions).map(o => o.value);
|
||||||
@@ -233,12 +240,16 @@
|
|||||||
el: '#search-form',
|
el: '#search-form',
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
effects: data.effects,
|
effects: data.effects.sort(stringAscending),
|
||||||
uses: data.uses,
|
uses: data.uses.sort(stringAscending),
|
||||||
conditions: data.conditions,
|
conditions: data.conditions.sort(stringAscending),
|
||||||
flavors: data.flavors,
|
flavors: data.flavors.sort(stringAscending),
|
||||||
|
error: '',
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
created() {
|
||||||
|
emitter.on('error', msg => this.error = msg);
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
handleSubmit() {
|
handleSubmit() {
|
||||||
const requirements = {
|
const requirements = {
|
||||||
@@ -249,6 +260,7 @@
|
|||||||
flavors: getMultiValues(this.$refs.flavors),
|
flavors: getMultiValues(this.$refs.flavors),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.error = '';
|
||||||
emitter.emit('search', requirements);
|
emitter.emit('search', requirements);
|
||||||
},
|
},
|
||||||
resetForm() {
|
resetForm() {
|
||||||
@@ -317,26 +329,31 @@
|
|||||||
this.requirements.conditions.forEach(t => searchParts.push(`+conditions:${t}`));
|
this.requirements.conditions.forEach(t => searchParts.push(`+conditions:${t}`));
|
||||||
this.requirements.flavors.forEach(t => searchParts.push(`+flavors:${t}`));
|
this.requirements.flavors.forEach(t => searchParts.push(`+flavors:${t}`));
|
||||||
|
|
||||||
const hits = this.idx
|
try {
|
||||||
.search(searchParts.join(' '))
|
const hits = this.idx
|
||||||
// .slice(0, limit);
|
.search(searchParts.join(' '))
|
||||||
const refs = hits.map(({ ref }) => parseInt(ref, 10));
|
// .slice(0, limit);
|
||||||
|
const refs = hits.map(({ ref }) => parseInt(ref, 10));
|
||||||
|
|
||||||
this.strains = this
|
this.strains = this
|
||||||
.all_strains
|
.all_strains
|
||||||
.map((strain, i) => {
|
.map((strain, i) => {
|
||||||
const idx = refs.indexOf(strain.id);
|
const idx = refs.indexOf(strain.id);
|
||||||
if (idx < 0) return;
|
if (idx < 0) return;
|
||||||
return Object.assign({ score: hits[idx].score }, strain)
|
return Object.assign({ score: hits[idx].score }, strain)
|
||||||
})
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
if (a.score === b.score) {
|
if (a.score === b.score) {
|
||||||
if (a.rating === b.rating) return 0;
|
if (a.rating === b.rating) return 0;
|
||||||
return a.rating > b.rating ? -1 : 1;
|
return a.rating > b.rating ? -1 : 1;
|
||||||
}
|
}
|
||||||
return a.score > b.score ? -1 : 1;
|
return a.score > b.score ? -1 : 1;
|
||||||
});
|
});
|
||||||
|
} catch(err) {
|
||||||
|
this.strains = [];
|
||||||
|
emitter.emit('error', err.message);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user