recipe editing

This commit is contained in:
Dan Elbert 2018-04-01 22:32:13 -05:00
parent 1c4fa37778
commit 3b1a9246fd
10 changed files with 63 additions and 7 deletions

View File

@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2016 Dan Elbert Copyright (c) 2018 Dan Elbert
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -11,6 +11,7 @@
:value="rawValue" :value="rawValue"
:class="finalInputClass" :class="finalInputClass"
@click="clickHandler"
@blur="blurHandler" @blur="blurHandler"
@input="inputHandler" @input="inputHandler"
@keydown="keydownHandler" @keydown="keydownHandler"
@ -129,6 +130,10 @@
this.activeListIndex = idx; this.activeListIndex = idx;
}, },
clickHandler(evt) {
this.$emit("inputClick", evt);
},
blurHandler(evt) { blurHandler(evt) {
// blur fires before click. If the blur was fired because the user clicked a list item, immediately hiding the list here // blur fires before click. If the blur was fired because the user clicked a list item, immediately hiding the list here
// would prevent the click event from firing // would prevent the click event from firing

View File

@ -22,6 +22,7 @@
valueAttribute="name" valueAttribute="name"
labelAttribute="name" labelAttribute="name"
placeholder="" placeholder=""
@inputClick="nameClick"
@optionSelected="searchItemSelected" @optionSelected="searchItemSelected"
:onGetOptions="updateSearchItems" :onGetOptions="updateSearchItems"
> >
@ -72,6 +73,12 @@
this.ingredient.ingredient_id = ingredient.id; this.ingredient.ingredient_id = ingredient.id;
this.ingredient.ingredient = ingredient; this.ingredient.ingredient = ingredient;
this.ingredient.name = ingredient.name; this.ingredient.name = ingredient.name;
},
nameClick() {
if (this.ingredient.ingredient_id === null && this.ingredient.name !== null && this.ingredient.name.length > 2) {
this.$refs.autocomplete.updateOptions(this.ingredient.name);
}
} }
}, },

View File

@ -1,5 +1,22 @@
<template> <template>
<div class="content">
<h1 class="title">About</h1>
<p>
A Recipe manager. Source available from my Git repository at <a href="https://source.elbert.us/dan/parsley">https://source.elbert.us</a>.
</p>
<p>
Parsley is released under the MIT License. All code &copy; Dan Elbert 2018.
</p>
<p>
Food data provided by:<br/>
<cite>
US Department of Agriculture, Agricultural Research Service, Nutrient Data Laboratory. USDA National Nutrient Database for Standard Reference, Release 28. Version Current: September 2015. Internet: <a href="http://www.ars.usda.gov/nea/bhnrc/ndl">http://www.ars.usda.gov/nea/bhnrc/ndl</a>
</cite>
</p>
</div>
</template> </template>
<script> <script>

View File

@ -7,8 +7,8 @@
<recipe-show :recipe="recipe"></recipe-show> <recipe-show :recipe="recipe"></recipe-show>
</div> </div>
<router-link :to="{name: 'edit_recipe', params: { id: recipeId }}">Edit</router-link> <router-link class="button" :to="{name: 'edit_recipe', params: { id: recipeId }}">Edit</router-link>
<router-link to="/">Back</router-link> <router-link class="button" to="/">Back</router-link>
</div> </div>
</template> </template>

View File

@ -3,6 +3,9 @@
<recipe-edit :recipe="recipe" action="Creating"></recipe-edit> <recipe-edit :recipe="recipe" action="Creating"></recipe-edit>
<button type="button" class="button is-primary" @click="save">Save</button>
<router-link class="button is-secondary" to="/">Cancel</router-link>
</div> </div>
</template> </template>
@ -11,6 +14,7 @@
import RecipeEdit from "./RecipeEdit"; import RecipeEdit from "./RecipeEdit";
import { mapState } from "vuex"; import { mapState } from "vuex";
import api from "../lib/Api"; import api from "../lib/Api";
import * as Errors from '../lib/Errors';
export default { export default {
data() { data() {
@ -35,6 +39,16 @@
}) })
}, },
methods: {
save() {
this.loadResource(
api.postRecipe(this.recipe)
.then(() => this.$router.push('/'))
.catch(Errors.onlyFor(Errors.ApiValidationError, err => this.validationErrors = err.validationErrors()))
);
}
},
components: { components: {
RecipeEdit RecipeEdit
} }

View File

@ -103,7 +103,7 @@ class Api {
return this.get("/recipes/" + id); return this.get("/recipes/" + id);
} }
patchRecipe(recipe) { buildRecipeParams(recipe) {
const params = { const params = {
recipe: { recipe: {
name: recipe.name, name: recipe.name,
@ -124,7 +124,7 @@ class Api {
return { return {
id: i.id, id: i.id,
name: i.name, name: i.name,
ingredient_id: null, ingredient_id: i.ingredient_id,
quantity: i.quantity, quantity: i.quantity,
units: i.units, units: i.units,
preparation: i.preparation, preparation: i.preparation,
@ -134,8 +134,15 @@ class Api {
}) })
} }
}; };
return params;
}
return this.patch("/recipes/" + recipe.id, params); patchRecipe(recipe) {
return this.patch("/recipes/" + recipe.id, this.buildRecipeParams(recipe));
}
postRecipe(recipe) {
return this.post("/recipes/", this.buildRecipeParams(recipe));
} }
postPreviewSteps(step_text) { postPreviewSteps(step_text) {

View File

@ -12,8 +12,13 @@
@import "./responsive_controls"; @import "./responsive_controls";
html, body {
min-height: 100%;
}
body { body {
background-color: $grey-dark; background-color: $grey-dark;
padding-bottom: 2rem;
} }
#app { #app {

View File

@ -103,7 +103,7 @@ class Recipe < ApplicationRecord
query = active.order(criteria.sort_column => criteria.sort_direction).page(criteria.page).per(criteria.per) query = active.order(criteria.sort_column => criteria.sort_direction).page(criteria.page).per(criteria.per)
if criteria.name.present? if criteria.name.present?
query = query.matches_token(:name, criteria.name) query = query.matches_tokens(:name, criteria.name.split(' '))
end end
if criteria.tags.present? if criteria.tags.present?

View File

@ -17,6 +17,7 @@ module UnitConversion
end end
def parse_value(str) def parse_value(str)
str = str.to_s.strip
if str =~ /^#{INTEGER_REGEX}$/ if str =~ /^#{INTEGER_REGEX}$/
str.to_i str.to_i
elsif str =~ /^#{RATIONAL_REGEX}$/ elsif str =~ /^#{RATIONAL_REGEX}$/