generated from HackYourFuture-CPH/hyf-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
pahanan
wants to merge
2
commits into
main
Choose a base branch
from
javascript-javascript2-week1/andriiK
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
const recipeObject = { | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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/