parsley/app/javascript/components/TheCalculator.vue

136 lines
3.4 KiB
Vue
Raw Permalink Normal View History

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-09-13 14:51:41 -05:00
<div class="box">
<div class="columns">
<app-text-field label="Input" v-model="input" class="column" :validation-error="inputErrors"></app-text-field>
<app-text-field label="Output Unit" v-model="outputUnit" class="column" :validation-error="outputUnitErrors"></app-text-field>
2018-04-02 00:10:06 -05:00
</div>
2018-09-13 14:51:41 -05:00
<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>
2018-04-02 00:10:06 -05:00
</div>
2018-09-13 14:51:41 -05:00
<app-text-field label="Density" v-model="density" class="column" :disabled="ingredient !== null" :validation-error="densityErrors"></app-text-field>
2018-04-02 00:10:06 -05:00
</div>
2018-09-13 14:51:41 -05:00
<app-text-field label="Output" v-model="output" disabled></app-text-field>
2018-04-02 00:10:06 -05:00
</div>
2018-09-13 14:51:41 -05:00
2018-04-02 00:10:06 -05:00
</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: '',
2018-09-13 14:51:41 -05:00
errors: {}
2018-04-02 00:10:06 -05:00
};
},
2018-09-13 14:51:41 -05:00
computed: {
inputErrors() {
if (this.errors.input && this.errors.input.length > 0) {
return this.errors.input.join(", ");
} else {
return null;
}
},
outputUnitErrors() {
if (this.errors.output_unit && this.errors.output_unit.length > 0) {
return this.errors.output_unit.join(", ");
} else {
return null;
}
},
densityErrors() {
if (this.errors.density && this.errors.density.length > 0) {
return this.errors.density.join(", ");
} else {
return null;
}
}
},
2018-04-02 00:10:06 -05:00
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-13 14:51:41 -05:00
if (this.input && this.input.length > 0) {
this.loadResource(api.getCalculate(this.input, this.outputUnit, this.ingredient ? this.ingredient.ingredient_id : null, this.density)
.then(data => {
this.output = data.output;
this.errors = data.errors;
})
);
}
2018-04-02 00:10:06 -05:00
}, 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>