|
@@ -1,9 +1,10 @@
|
|
|
<script>
|
|
<script>
|
|
|
import {notify} from "$lib/notifier.svelte.js";
|
|
import {notify} from "$lib/notifier.svelte.js";
|
|
|
|
|
+ import {goto} from "$app/navigation";
|
|
|
|
|
|
|
|
- let nameInput;
|
|
|
|
|
|
|
+ let nameInput, quantityInput, stepsInput;
|
|
|
let name = $state("");
|
|
let name = $state("");
|
|
|
- let quantity = $state(0);
|
|
|
|
|
|
|
+ let quantity = $state();
|
|
|
let unit = $state("");
|
|
let unit = $state("");
|
|
|
let ingredientName = $state("");
|
|
let ingredientName = $state("");
|
|
|
let ingredients = $state([]);
|
|
let ingredients = $state([]);
|
|
@@ -11,9 +12,28 @@
|
|
|
let steps = $state([]);
|
|
let steps = $state([]);
|
|
|
let notes = $state("");
|
|
let notes = $state("");
|
|
|
|
|
|
|
|
- const submit = (event)=>{
|
|
|
|
|
|
|
+ const submit = async (event)=>{
|
|
|
event.preventDefault();
|
|
event.preventDefault();
|
|
|
- console.log("submitting");
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if(!name){
|
|
|
|
|
+ notify("error", "Please enter recipe name");
|
|
|
|
|
+ nameInput.focus();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const data = {name, ingredients, steps, notes};
|
|
|
|
|
+
|
|
|
|
|
+ const response = await fetch("/recipe/new", {
|
|
|
|
|
+ method: "POST",
|
|
|
|
|
+ headers: {"Content-Type": "application/json"},
|
|
|
|
|
+ credentials: "include",
|
|
|
|
|
+ body: JSON.stringify(data)
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const recipe = await response.json();
|
|
|
|
|
+ if(!response.ok) return notify("error", recipe.message);
|
|
|
|
|
+
|
|
|
|
|
+ goto("/dashboard");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const addIngredient = ()=>{
|
|
const addIngredient = ()=>{
|
|
@@ -27,15 +47,18 @@
|
|
|
name: ingredientName
|
|
name: ingredientName
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- quantity = 0;
|
|
|
|
|
|
|
+ quantity = null;
|
|
|
unit = "";
|
|
unit = "";
|
|
|
ingredientName = "";
|
|
ingredientName = "";
|
|
|
|
|
+
|
|
|
|
|
+ quantityInput.focus();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const addStep = ()=>{
|
|
const addStep = ()=>{
|
|
|
if(!stepString) return notify("error", "Please enter some text");
|
|
if(!stepString) return notify("error", "Please enter some text");
|
|
|
steps.push(stepString);
|
|
steps.push(stepString);
|
|
|
stepString = "";
|
|
stepString = "";
|
|
|
|
|
+ stepsInput.focus();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$effect(()=>{
|
|
$effect(()=>{
|
|
@@ -61,7 +84,7 @@
|
|
|
</ul>
|
|
</ul>
|
|
|
|
|
|
|
|
<label>Quantity
|
|
<label>Quantity
|
|
|
- <input type="number" min="0" bind:value={quantity}>
|
|
|
|
|
|
|
+ <input type="number" min="0" bind:value={quantity} bind:this={quantityInput}>
|
|
|
</label>
|
|
</label>
|
|
|
|
|
|
|
|
<label>Unit
|
|
<label>Unit
|
|
@@ -84,12 +107,12 @@
|
|
|
{/each}
|
|
{/each}
|
|
|
</ol>
|
|
</ol>
|
|
|
|
|
|
|
|
- <textarea bind:value={stepString} rows="2"></textarea>
|
|
|
|
|
|
|
+ <textarea bind:value={stepString} rows="2" bind:this={stepsInput}></textarea>
|
|
|
|
|
|
|
|
<button class="button" type="button" onclick={addStep}>Add</button>
|
|
<button class="button" type="button" onclick={addStep}>Add</button>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
- <textarea bind:value={notes} rows="4"></textarea>
|
|
|
|
|
|
|
+ <textarea bind:value={notes} rows="4" placeholder="Notes"></textarea>
|
|
|
|
|
|
|
|
<button>Create Recipe</button>
|
|
<button>Create Recipe</button>
|
|
|
</form>
|
|
</form>
|