Skip to content
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
67 changes: 67 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,71 @@
html, body {
margin: 0;
padding: 0;
font-family: 'Rubik', sans-serif;
background-color: #EEF0F4;
color: #432000;
user-select: none;
}

.container {
display: flex;
flex-direction: column;
max-width: 320px;
margin: 30px auto;
}

img {
width: 150px;
margin: 0 auto;
}

input {
color: #432000;
background-color: #DCE1EB;
border: 0;
padding: 15px;
border-radius: 8px;
font-size: 20px;
text-align: center;
font-family: 'Rubik', sans-serif;
margin: 10px 0;
}

button {
color: #FDFDFD;
background-color: #AC485A;
border: 0;
padding: 15px;
border-radius: 8px;
font-size: 20px;
text-align: center;
font-family: 'Rubik', sans-serif;
}

button:hover {
background-color: #432000;
cursor: pointer;
}

ul {
list-style-type: none;
padding: 0;
display: flex;
flex-wrap: wrap;
gap: 10px;
}

ul li {
font-size: 20px;
background-color: #FFFDF8;
padding: 15px;
border-radius: 8px;
flex-grow: 1;
text-align: center;
box-shadow: 0px 1px 4px rgba(0, 0, 0, 0.2)
}

ul li:hover {
background-color: #FFECC7;
cursor: pointer;
}
19 changes: 16 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@
<html>
<head>
<title>Add to Cart</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Rubik:wght@300&display=swap" rel="stylesheet">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="stylesheet" href="index.css">
</head>
<body>
<input type="text" id="input-field" placeholder="Bread">
<button id="add-button">Add to cart</button>
<script src="index.js"></script>
<div class="container">
<img src="assets/cat.png">
<input type="text" id="input-field" placeholder="Bread">
<button id="add-button">Add to cart</button>
<ul id="shopping-list">
</ul>
</div>
<script src="index.js" type="module"></script>
</body>
</html>
60 changes: 58 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,68 @@
Challenge:
Make it so that when you click the 'Add to cart' button, whatever is written in the input field should be console logged.
*/
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js"
import { getDatabase, ref, push } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js"

const appSettings = {
databaseURL: "https://realtime-database-4c27d-default-rtdb.europe-west1.firebasedatabase.app/"
}

const app = initializeApp(appSettings)
const database = getDatabase(app)
const shoppingListInDB = ref(database, "shoppingList")

const inputFieldEl = document.getElementById("input-field")
const addButtonEl = document.getElementById("add-button")
const shoppingListEl = document.getElementById("shopping-list")

addButtonEl.addEventListener("click", function() {
let inputValue = inputFieldEl.value

push(shoppingListInDB, inputValue)

clearInputFieldEl()
})

onValue(shoppingListInDB, function(snapshot) {
if (snapshot.exists()) {
let itemsArray = Object.entries(snapshot.val())

clearShoppingListEl()

for (let i = 0; i < itemsArray.length; i++) {
let currentItem = itemsArray[i]
let currentItemID = currentItem[0]
let currentItemValue = currentItem[1]

appendItemToShoppingListEl(currentItem)
}
} else {
shoppingListEl.innerHTML = "No items here... yet"
}
})

function clearShoppingListEl() {
shoppingListEl.innerHTML = ""
}

function clearInputFieldEl() {
inputFieldEl.value = ""
}

function appendItemToShoppingListEl(item) {
let itemID = item[0]
let itemValue = item[1]

let newEl = document.createElement("li")

newEl.textContent = itemValue

newEl.addEventListener("click", function() {
let exactLocationOfItemInDB = ref(database, `shoppingList/${itemID}`)

remove(exactLocationOfItemInDB)
})

console.log(inputValue)
})
shoppingListEl.append(newEl)
}