2018-03-30 14:31:09 -05:00
|
|
|
<template>
|
2018-04-02 00:10:06 -05:00
|
|
|
<div>
|
|
|
|
<h1 class="title">Calculator</h1>
|
2018-03-30 14:31:09 -05:00
|
|
|
|
2018-04-02 00:10:06 -05:00
|
|
|
<div v-for="err in errors" :key="err" class="notification is-warning">
|
|
|
|
{{err}}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="columns">
|
|
|
|
<div class="field column">
|
|
|
|
<label class="label">Input</label>
|
|
|
|
<div class="control">
|
|
|
|
<input class="input" type="text" placeholder="input" v-model="input">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="field column">
|
|
|
|
<label class="label">Output Unit</label>
|
|
|
|
<div class="control">
|
|
|
|
<input class="input" type="text" placeholder="unit" v-model="outputUnit">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="columns">
|
|
|
|
<div class="field column">
|
|
|
|
<label class="label">Ingredient</label>
|
|
|
|
<div class="control">
|
|
|
|
<app-autocomplete
|
|
|
|
:inputClass="{'is-success': ingredient !== null}"
|
|
|
|
ref="autocomplete"
|
|
|
|
v-model="ingredient_name"
|
|
|
|
:minLength="2"
|
|
|
|
valueAttribute="name"
|
|
|
|
labelAttribute="name"
|
|
|
|
placeholder=""
|
|
|
|
@optionSelected="searchItemSelected"
|
|
|
|
:onGetOptions="updateSearchItems"
|
|
|
|
>
|
|
|
|
</app-autocomplete>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="field column">
|
|
|
|
<label class="label">Density</label>
|
|
|
|
<div class="control">
|
2018-09-12 17:17:15 -05:00
|
|
|
<input class="input" type="text" placeholder="8.345 lb/gallon" v-model="density" :disabled="ingredient !== null">
|
2018-04-02 00:10:06 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="field">
|
|
|
|
<label class="label">Output</label>
|
|
|
|
<div class="control">
|
|
|
|
<input class="input" type="text" disabled="disabled" v-model="output">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-03-30 14:31:09 -05:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
2018-04-02 00:10:06 -05:00
|
|
|
import api from "../lib/Api";
|
|
|
|
import debounce from "lodash/debounce";
|
|
|
|
|
2018-03-30 14:31:09 -05:00
|
|
|
export default {
|
2018-04-02 00:10:06 -05:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
input: '',
|
|
|
|
outputUnit: '',
|
|
|
|
ingredient_name: '',
|
|
|
|
ingredient: null,
|
|
|
|
density: '',
|
|
|
|
output: '',
|
|
|
|
errors: []
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
methods: {
|
|
|
|
updateSearchItems(text) {
|
|
|
|
return api.getSearchIngredients(text);
|
|
|
|
},
|
|
|
|
|
|
|
|
searchItemSelected(ingredient) {
|
2018-09-12 17:17:15 -05:00
|
|
|
this.ingredient = ingredient || null;
|
|
|
|
this.ingredient_name = ingredient.name || null;
|
|
|
|
this.density = ingredient.density || null;
|
2018-04-02 00:10:06 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
updateOutput: debounce(function() {
|
2018-09-12 17:17:15 -05:00
|
|
|
this.loadResource(api.getCalculate(this.input, this.outputUnit, this.ingredient ? this.ingredient.ingredient_id : null, this.density)
|
2018-04-02 00:10:06 -05:00
|
|
|
.then(data => {
|
|
|
|
this.output = data.output;
|
|
|
|
this.errors = data.errors;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}, 500)
|
|
|
|
},
|
|
|
|
|
|
|
|
watch: {
|
|
|
|
'ingredient_name': function(val) {
|
|
|
|
if (this.ingredient && this.ingredient.name !== val) {
|
|
|
|
this.ingredient = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
created() {
|
|
|
|
this.$watch(
|
|
|
|
function() {
|
2018-09-12 17:17:15 -05:00
|
|
|
return [this.input, this.outputUnit, this.density, this.ingredient];
|
2018-04-02 00:10:06 -05:00
|
|
|
},
|
|
|
|
function() {
|
|
|
|
this.updateOutput();
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
2018-03-30 14:31:09 -05:00
|
|
|
|
2018-04-02 00:10:06 -05:00
|
|
|
components: {
|
|
|
|
}
|
2018-03-30 14:31:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
</style>
|