diff --git a/README.md b/README.md index 60f55e53..1050a51f 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,9 @@ # Project Name +The assigment was to create a functional chatbot using Javascript, HTML and CSS. I've made a pizzeria called "Elina's daily surprise pizzeria". -Replace this readme with your own information about your project. - -Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. ## The problem - -Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? +I've struggled alot this week. I've found it very hard to make sense out of the starter code and how to add the right code in general. I tried making buttons and select options but I could never make it work. Therefor I approached the different steps in another way than I think we were suppose to. I kept looking at the videos on disco but for me it was very hard to apply the content to this weeks assigment. Since I didn't figure out how to make buttons and select options I did a simple version of the chatbot. The customer can't pick the type of pizza, pasta or salad only the food type, hence the "Daily Surprise" solution. Tools I used were chatgbpt, stack overflow, the material on disco and google. ## View it live - -Have you deployed your project somewhere? Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about. +https://sprightly-gecko-87af32.netlify.app/ diff --git a/code/index.html b/code/index.html index 316eb187..2e48f169 100644 --- a/code/index.html +++ b/code/index.html @@ -1,32 +1,32 @@ - - - - - - Chatbot - + + + + + + Chatbot + - -

Welcome to my chatbot!

-
-
-
-
- - - -
-
-
+ +

Elina's Pizzeria πŸ‘¨β€πŸ³πŸ•

+
+
+
+
+ + + +
+
+
- - + + - + \ No newline at end of file diff --git a/code/script.js b/code/script.js index 125d6904..eefd95d9 100644 --- a/code/script.js +++ b/code/script.js @@ -1,53 +1,143 @@ -// DOM selectors (variables that point to selected DOM elements) goes here πŸ‘‡ -const chat = document.getElementById('chat') +// DOM selectorsπŸ‘‡ +document.addEventListener("DOMContentLoaded", () => { + const form = document.getElementById("name-form"); + const nameInput = document.getElementById("name-input"); + const chat = document.getElementById("chat"); + let inputWrapper = document.getElementById("inputWrapper"); -// Functions goes here πŸ‘‡ + // Function to handle the name input + const handleNameInput = (event) => { + event.preventDefault(); + const name = nameInput.value; + showMessage(name, "user"); + nameInput.value = ""; + setTimeout(() => askForFood(name), 1000); // Ask for food choice + }; -// A function that will add a chat bubble in the correct place based on who the sender is -const showMessage = (message, sender) => { - // The if statement checks if the sender is the user and if that's the case it inserts - // an HTML section inside the chat with the posted message from the user - if (sender === 'user') { - chat.innerHTML += ` + // Function to ask for food options with buttons + const askForFood = (name) => { + showMessage(`Nice to meet you ${name}, what would you like to order today?`, "bot"); + + inputWrapper.innerHTML = ` + + + + `; + + document.getElementById("pizzaBtn").addEventListener("click", () => handleFoodChoice("Pizza")); + document.getElementById("pastaBtn").addEventListener("click", () => handleFoodChoice("Pasta")); + document.getElementById("saladBtn").addEventListener("click", () => handleFoodChoice("Salad")); + }; + + // Function to handle the food choice and show specific options based on food type + const handleFoodChoice = (food) => { + showMessage(`You've selected ${food}. Please choose an option:`, "bot"); + + let options = ""; + if (food === "Pizza") { + options = ` + + `; + } else if (food === "Pasta") { + options = ` + + `; + } else if (food === "Salad") { + options = ` + + `; + } + + inputWrapper.innerHTML = `${options} `; + + document.getElementById("selectOptionBtn").addEventListener("click", () => { + const selectedFoodOption = document.getElementById("foodOptions").value; + showMessage(`You've chosen ${selectedFoodOption}.`, "user"); + setTimeout(() => askForSize(selectedFoodOption, food), 1000); // Pass food type and option to ask for size + }); + }; + + // Function to ask for the size of the selected food + const askForSize = (selectedFoodOption, foodType) => { + showMessage(`What size would you like for your ${selectedFoodOption}?`, "bot"); + + inputWrapper.innerHTML = ` + + + `; + + document.getElementById("adultSizeBtn").addEventListener("click", () => handleFoodSize(selectedFoodOption, foodType, "Adult")); + document.getElementById("childSizeBtn").addEventListener("click", () => handleFoodSize(selectedFoodOption, foodType, "Child")); + }; + + // Function to handle the size selection and confirm the order + const handleFoodSize = (selectedFoodOption, foodType, size) => { + showMessage(`You've selected ${size} size for your ${selectedFoodOption}.`, "user"); + setTimeout(() => confirmOrder(selectedFoodOption, foodType, size), 1000); + }; + + // Function to confirm the order and show the final message + const confirmOrder = (selectedFoodOption, foodType, size) => { + let price = 0; + + if (foodType === "Pizza") { + price = size === "Adult" ? 150 : 100; + } else if (foodType === "Pasta") { + price = size === "Adult" ? 130 : 80; + } else if (foodType === "Salad") { + price = size === "Adult" ? 120 : 70; + } + + showMessage(`Thank you! Your ${size} ${selectedFoodOption} will be ready in 15 minutes. The price is ${price} SEK. Please pay upon arrival! πŸ˜‹`, "bot"); + + inputWrapper.innerHTML = ""; // Clear inputWrapper for no further input + }; + + // Function to show messages in the chat + const showMessage = (message, sender) => { + if (sender === "user") { + chat.innerHTML += `

${message}

- User + User
- ` - // The else if statement checks if the sender is the bot and if that's the case it inserts - // an HTML section inside the chat with the posted message from the bot - } else if (sender === 'bot') { - chat.innerHTML += ` + `; + } else if (sender === "bot") { + chat.innerHTML += `
Bot

${message}

- ` - } - - // This little thing makes the chat scroll to the last message when there are too many to - // be shown in the chat box - chat.scrollTop = chat.scrollHeight -} - -// A function to start the conversation -const greetUser = () => { - // Here we call the function showMessage, that we declared earlier with the argument: - // "Hello there, what's your name?" for message, and the argument "bot" for sender - showMessage("Hello there, what's your name?", 'bot') - // Just to check it out, change 'bot' to 'user' here πŸ‘† and see what happens -} - -// Eventlisteners goes here πŸ‘‡ - -// Here we invoke the first function to get the chatbot to ask the first question when -// the website is loaded. Normally we invoke functions like this: greeting() -// To add a little delay to it, we can wrap it in a setTimeout (a built in JavaScript function): -// and pass along two arguments: -// 1.) the function we want to delay, and 2.) the delay in milliseconds -// This means the greeting function will be called one second after the website is loaded. -setTimeout(greetUser, 1000) + `; + } + + chat.scrollTop = chat.scrollHeight; + }; + + // Function to start the conversation + const greetUser = () => { + showMessage("Hello there!
Welcome to Elina's Pizzeria!
What's your name?", "bot"); + }; + + // Event listeners go here + form.addEventListener("submit", handleNameInput); + + // Start the bot with a delay when the page loads + setTimeout(greetUser, 1000); +}); diff --git a/code/style.css b/code/style.css index a275402f..3a64bf25 100644 --- a/code/style.css +++ b/code/style.css @@ -11,7 +11,7 @@ body { h1 { font-weight: bold; - font-size: 28px; + font-size: 30px; line-height: 34px; color: #fff; text-align: center; @@ -26,6 +26,14 @@ h2 { margin-bottom: 36px; } +h4 { + font-weight: bold; + font-size: 24px; + line-height: 34px; + color: #fff; + text-align: center; +} + p { font-size: 18px; font-weight: 600; @@ -51,6 +59,7 @@ input { main { margin: 0 auto; width: 100%; + /* row 55 and 56 makes it responsive */ max-width: 700px; height: 600px; border-radius: 30px; @@ -147,4 +156,38 @@ button { button:hover { opacity: 0.9; transition: all 0.2s ease; +} + +/* Media Queries */ +/* Mobile */ +@media only screen and (min-width: 320px) and (max-width: 767px) { + h1 { + font-size: 20px; + } + + h4 { + font-size: 14px; + } +} + +/* Tablet */ +@media only screen and (min-width: 768px) and (max-width: 1023px) { + h1 { + font-size: 24px; + } + + h4 { + font-size: 18px; + } +} + +/* Desktop */ +@media only screen and (min-width: 1024px) and (max-width: 1600px) { + h1 { + font-size: 30px; + } + + h4 { + font-size: 24px; + } } \ No newline at end of file