insert ingredents
This commit is contained in:
parent
22e2dab830
commit
8af791deb9
6 changed files with 28 additions and 32 deletions
|
@ -70,10 +70,10 @@ app.post("/add-recipe", async (req, res) => {
|
||||||
const existingIngredient = existingIngredients.find(ing => ing.name === ingredient.name);
|
const existingIngredient = existingIngredients.find(ing => ing.name === ingredient.name);
|
||||||
if (!existingIngredient) {
|
if (!existingIngredient) {
|
||||||
// create the ingredient if there is no entry
|
// create the ingredient if there is no entry
|
||||||
const [ingredientId] = await db('ingredients').insert({
|
const [newIngredient] = await db('ingredients').insert({
|
||||||
name: ingredient.name
|
name: ingredient.name
|
||||||
}, ['id']);
|
}, ['id']);
|
||||||
ingredientData.push({ id: ingredientId, quantity: ingredient.quantity, unit: ingredient.unit });
|
ingredientData.push({ id: newIngredient.id, quantity: ingredient.quantity, unit: ingredient.unit });
|
||||||
} else {
|
} else {
|
||||||
// if the ingredient exists use existing entry
|
// if the ingredient exists use existing entry
|
||||||
ingredientData.push({ id: existingIngredient.id, quantity: ingredient.quantity, unit: ingredient.unit });
|
ingredientData.push({ id: existingIngredient.id, quantity: ingredient.quantity, unit: ingredient.unit });
|
||||||
|
|
|
@ -1,55 +1,49 @@
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { type Ingredient } from "../types/Recipe"
|
import { type Ingredient } from "../types/Recipe";
|
||||||
|
|
||||||
|
interface AddBulkIngredientsProps {
|
||||||
|
onChange?: (ingredients: { quantity: number; unit: string; name: string }[]) => void;
|
||||||
|
}
|
||||||
|
|
||||||
const BulkIngredientsForm: React.FC = () => {
|
const AddBulkIngredients: React.FC<AddBulkIngredientsProps> = ({ onChange }) => {
|
||||||
const [ingredients, setIngredients] = useState<Ingredient[]>([]);
|
const [ingredients, setIngredients] = useState<{ quantity: number; unit: string; name: string }[]>([]);
|
||||||
|
|
||||||
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||||
// Split the input by newline to get individual ingredients
|
|
||||||
const lines = e.target.value.split('\n').filter(line => line.trim() !== '');
|
const lines = e.target.value.split('\n').filter(line => line.trim() !== '');
|
||||||
const pattern = /^([0-9/.]+)?\s*(\S+)\s*((\w+\s*)*)$/;
|
const pattern = /^([0-9/.]+)?\s*(\S+)\s*((\w+\s*)*)$/;
|
||||||
const parsedIngredients: Ingredient[] = lines.map(line => {
|
const parsedIngredients = lines.map(line => {
|
||||||
const parts = line.match(pattern); // Updated regex pattern for fractions
|
const parts = line.match(pattern);
|
||||||
let quantity;
|
let quantity = 0;
|
||||||
if (parts?.[1]) {
|
if (parts?.[1]) {
|
||||||
// Try to parse the quantity as a fraction first
|
|
||||||
const [num, denom] = parts[1].split('/');
|
const [num, denom] = parts[1].split('/');
|
||||||
if (denom) {
|
if (denom) {
|
||||||
quantity = parseFloat(num) / parseFloat(denom);
|
quantity = parseFloat(num) / parseFloat(denom);
|
||||||
} else {
|
} else {
|
||||||
quantity = parseFloat(parts[1]);
|
quantity = parseFloat(parts[1]);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
quantity = 0; // Default to zero if no quantity is found
|
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
quantity: +quantity.toFixed(2),
|
quantity: +quantity.toFixed(2),
|
||||||
name: parts?.[3]?.trim() || '',
|
unit: parts?.[2]?.trim() || '',
|
||||||
unit: parts?.[2]?.trim() || ''
|
name: parts?.[3]?.trim() || ''
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
setIngredients(parsedIngredients);
|
setIngredients(parsedIngredients);
|
||||||
|
if (onChange) onChange(parsedIngredients); // Notify parent of change
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<p>Please enter ingredients in the following order: Quantity, Unit, Name</p> {/* Prompt for correct input format */}
|
<p>Please enter ingredients in the following order: Quantity, Unit, Name</p> {/* Prompt for correct input format */}
|
||||||
<textarea
|
<textarea rows={4} cols={50} onChange={handleInputChange} placeholder="Enter ingredients separated by newline..." />
|
||||||
rows={4} // Adjust the number of rows based on your input size expectations
|
|
||||||
cols={50} // Adjust the number of columns based on your input width expectations
|
|
||||||
onChange={handleInputChange}
|
|
||||||
placeholder="Enter ingredients separated by newline..."
|
|
||||||
/>
|
|
||||||
<div>
|
<div>
|
||||||
<ul>
|
<ul>
|
||||||
{ingredients.map((ing, index) => (
|
{ingredients.map((ing, index) => (
|
||||||
<li key={index}>{`${ing.quantity} ${ing.unit} ${ing.name}`}</li> // Changed order to quantity, unit, name
|
<li key={index}>{`${ing.quantity} ${ing.unit} ${ing.name}`}</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
export default AddBulkIngredients;
|
||||||
export default BulkIngredientsForm;
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ function RecipeCard({ recipe }: { recipe: Recipe }) {
|
||||||
<h4>Ingredients:</h4>
|
<h4>Ingredients:</h4>
|
||||||
<ul>
|
<ul>
|
||||||
{recipe.ingredients.map((ingredient: Ingredient, index) => (
|
{recipe.ingredients.map((ingredient: Ingredient, index) => (
|
||||||
<li key={index}>{ingredient.name} {ingredient.quantity} {ingredient.unit}</li>
|
<li key={index}>{ingredient.quantity} {ingredient.unit} {ingredient.name}</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
<h4>Steps:</h4>
|
<h4>Steps:</h4>
|
||||||
|
|
|
@ -1,29 +1,30 @@
|
||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import { addRecipe } from "../services/frontendApi.js";
|
import { addRecipe } from "../services/frontendApi.js";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import AddIngredientsForm from "../components/AddIngredientsForm.tsx"
|
|
||||||
import AddBulkIngredients from "../components/AddBulkIngredients.tsx"
|
import AddBulkIngredients from "../components/AddBulkIngredients.tsx"
|
||||||
|
|
||||||
function AddRecipe() {
|
function AddRecipe() {
|
||||||
const [newRecipeId, setNewRecipeId] = useState<number | null>(null);
|
const [newRecipeId, setNewRecipeId] = useState<number | null>(null);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const [ingredients, setIngredients] = useState<{ quantity: number; unit: string; name: string }[]>([]);
|
||||||
|
|
||||||
const addRecipeForm = async (event: React.FormEvent) => {
|
const addRecipeForm = async (event: React.FormEvent) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const nameElement = document.getElementById('ar-name') as HTMLInputElement | null;
|
const nameElement = document.getElementById('ar-name') as HTMLInputElement | null;
|
||||||
const cuisineElement = document.getElementById('ar-cuisine') as HTMLInputElement | null;
|
const cuisineElement = document.getElementById('ar-cuisine') as HTMLInputElement | null;
|
||||||
|
|
||||||
if (nameElement && cuisineElement) {
|
if (nameElement && cuisineElement) {
|
||||||
const recipeData = {
|
const recipeData = {
|
||||||
name: nameElement.value,
|
name: nameElement.value,
|
||||||
cuisine: cuisineElement.value
|
cuisine: cuisineElement.value,
|
||||||
|
ingredients: ingredients,
|
||||||
|
steps: { 1: 'step 1', 2: 'step 2' }
|
||||||
}
|
}
|
||||||
|
console.log(recipeData)
|
||||||
const data = await addRecipe(recipeData);
|
const data = await addRecipe(recipeData);
|
||||||
setNewRecipeId(data.id);
|
setNewRecipeId(data.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (newRecipeId !== null) {
|
if (newRecipeId !== null) {
|
||||||
navigate(`/recipe/${newRecipeId}`);
|
navigate(`/recipe/${newRecipeId}`);
|
||||||
|
@ -50,8 +51,7 @@ function AddRecipe() {
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
<div>
|
<div>
|
||||||
<AddIngredientsForm />
|
<AddBulkIngredients onChange={setIngredients} />
|
||||||
<AddBulkIngredients />
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -46,7 +46,7 @@ function Cookbook() {
|
||||||
) : (
|
) : (
|
||||||
<div className="recipes-grid">
|
<div className="recipes-grid">
|
||||||
{recipes.map((recipe: Recipe) => (
|
{recipes.map((recipe: Recipe) => (
|
||||||
<CookbookRecipeTile recipe={recipe} key={recipe.details.id} handleDelete={handleDelete} />
|
<CookbookRecipeTile recipe={recipe} key={recipe.id} handleDelete={handleDelete} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -11,6 +11,8 @@ export const getRecipeById = async (id) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const addRecipe = async (recipeData) => {
|
export const addRecipe = async (recipeData) => {
|
||||||
|
console.log(JSON.stringify(recipeData))
|
||||||
|
// return
|
||||||
const response = await fetch("http://localhost:6063/add-recipe", {
|
const response = await fetch("http://localhost:6063/add-recipe", {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue