Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Javascript javascript2 week1/andriK #130

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions javascript/javascript2/week1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recipe</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="recipe-container"></div>

<button id="add-new-recipe">Add new recipe</button>

<script src="script.js"></script>
</body>
</html>
190 changes: 190 additions & 0 deletions javascript/javascript2/week1/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
const recipeObject = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better follow naming convention : https://jsonapi.org/recommendations/

id: 1,
title: "Glögg",
picture_url:
"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e2/Gl%C3%B6gg_kastrull.JPG/800px-Gl%C3%B6gg_kastrull.JPG",
ingredients: [
{ NAME: "Orange zest", AMOUNT: "0.5" },
{ NAME: "Water", AMOUNT: "200 ml" },
{ NAME: "Sugar", AMOUNT: "275 g" },
{ NAME: "Whole cloves", AMOUNT: "5" },
{ NAME: "Cinnamon sticks", AMOUNT: "2" },
],
description: "Mix everything, heat it, and you are good to go!",
};

const addNewButton = document.querySelector(`#add-new-recipe`);
addNewButton.addEventListener(`click`, addButtonforForm);
let ingredientCount;

function showRecipe(recipe) {
const recipeForm = document.querySelector(`#recipe-container`);
const newRecipeContainer = document.createElement("div");

const recipeTitle = document.createElement("h1");
recipeTitle.textContent = recipe.title;
newRecipeContainer.appendChild(recipeTitle);

const recipeImg = document.createElement("img");
recipeImg.src = recipe.picture_url;
recipeImg.alt = recipe.description;
recipeImg.width = 400;
newRecipeContainer.appendChild(recipeImg);

if (recipe.ingredients && recipe.ingredients.length > 0) {
const ingredientTitle = document.createElement("p");
ingredientTitle.textContent = "Ingredients:";
newRecipeContainer.appendChild(ingredientTitle);

const recipeIngredients = document.createElement("ul");
recipe.ingredients.forEach((ingredient) => {
const listItem = document.createElement("li");
listItem.textContent = `${ingredient.NAME}: ${ingredient.AMOUNT}`;
recipeIngredients.appendChild(listItem);
});
newRecipeContainer.appendChild(recipeIngredients);
}

const recipeDescription = document.createElement("p");
recipeDescription.textContent = recipe.description;
newRecipeContainer.appendChild(recipeDescription);

recipeForm.appendChild(newRecipeContainer);
}

showRecipe(recipeObject);

function addButtonforForm() {
addNewButton.style.display = "none";
const recipeForm = document.querySelector(`#recipe-container`);
const newForm = document.createElement("div");

const formTitle = document.createElement("h1");
formTitle.textContent = "New recipe";
newForm.appendChild(formTitle);

const formName = document.createElement("label");
formName.textContent = "Add name of dish";
const nameInput = document.createElement("input");
nameInput.id = "recipe-title";
formName.appendChild(nameInput);
newForm.appendChild(formName);

newForm.appendChild(document.createElement("br"));

const formImg = document.createElement("label");
formImg.textContent = "Add image of dish";
const imgInput = document.createElement("input");
imgInput.id = "recipe-image";
formImg.appendChild(imgInput);
newForm.appendChild(formImg);

newForm.appendChild(document.createElement("br"));

const labelIngredients = document.createElement("label");
labelIngredients.textContent = "Ingredients (min 5):";
newForm.appendChild(document.createElement("br"));
newForm.appendChild(labelIngredients);

const ingredientsContainer = document.createElement("div");
ingredientsContainer.id = "ingredients-container";
newForm.appendChild(ingredientsContainer);

const addIngredientButton = document.createElement("button");
addIngredientButton.type = "button";
addIngredientButton.textContent = "Add ingredient";
addIngredientButton.addEventListener(`click`, addFormForIngredient);

newForm.appendChild(addIngredientButton);
newForm.appendChild(document.createElement("br"));

function addFormForIngredient() {
const ingredientInput = document.createElement("div");
ingredientInput.classList.add("ingredient");

const nameInput = document.createElement("input");
nameInput.classList.add("ingredient-name");
nameInput.placeholder = "Ingredient name";

const amountInput = document.createElement("input");
amountInput.classList.add("ingredient-amount");
amountInput.placeholder = "Amount";

ingredientInput.appendChild(nameInput);
ingredientInput.appendChild(amountInput);
ingredientsContainer.appendChild(ingredientInput);
}

for (let i = 0; i < 5; i++) {
addFormForIngredient();
}

newForm.appendChild(document.createElement("br"));

const formDescription = document.createElement("label");
formDescription.textContent = "Description:";
const descriptionInput = document.createElement("input");
descriptionInput.id = "recipe-description";
formDescription.appendChild(descriptionInput);
newForm.appendChild(formDescription);

newForm.appendChild(document.createElement("br"));

const addRecipe = document.createElement("button");
addRecipe.textContent = "Add recipe";
addRecipe.type = "button";

addRecipe.addEventListener(`click`, function (event) {
event.preventDefault();

const title = nameInput.value.trim();
const image = imgInput.value.trim();
const description = descriptionInput.value.trim();

if (!title || !image || !description) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job checking if anything is not filled in, plus having value.trim() so no empty spaces.

alert("Please fill in all required fields: title, image URL, and description.");
return;
}

const ingredients = [];
let hasEmptyIngredient = false;

document.querySelectorAll(".ingredient").forEach((ingredientInput) => {
const name = ingredientInput.querySelector(".ingredient-name").value.trim();
const amount = ingredientInput.querySelector(".ingredient-amount").value.trim();
if (!name || !amount) {
hasEmptyIngredient = true;
} else {
ingredients.push({ NAME: name, AMOUNT: amount });
}
});

if (hasEmptyIngredient) {
alert("Please fill in all ingredient fields.");
return;
}

if (ingredients.length < 5) {
alert("Please add at least 5 ingredients!");
return;
}

const newRecipe = {
title,
picture_url: image,
ingredients,
description,
};

showRecipe(newRecipe);

newForm.innerHTML = "";

addNewButton.style.display = "block";
});



newForm.appendChild(addRecipe);
recipeForm.appendChild(newForm);
}