Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DMD4470/week-4/firestore.html
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
201 lines (148 sloc)
5.39 KB
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<link rel="stylesheet" href="css/firestore.css"> | |
<link href="https://fonts.googleapis.com/css?family=Amatic+SC:400,700&display=swap" rel="stylesheet"> | |
<title>My Shopping List App</title> | |
<style> | |
#item { | |
border: 1px solid #ccc; | |
} | |
ul, li{ | |
margin: 0; | |
padding: 0; | |
list-style-type: none; | |
} | |
li{ | |
padding: .3em 0; | |
} | |
input:checked + span{ | |
text-decoration: line-through; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>My Shopping List App: <span id="listName"></span></h1> | |
<form action="" id="shoppingForm"> | |
<input type="text" name="item" id="item"> <button id="add">Add</button> | |
</form> | |
<hr> | |
<ul id="shopping-list"> | |
</ul> | |
<script src="https://www.gstatic.com/firebasejs/7.8.0/firebase-app.js"></script> | |
<script src="https://www.gstatic.com/firebasejs/7.8.0/firebase-firestore.js"></script> | |
<script> | |
// Initialize Cloud Firestore through Firebase | |
firebase.initializeApp({ | |
apiKey: "AIzaSyBKiU2VOeD9Vwx4Fxt4_G1mbP-_pD-wHrk", | |
authDomain: "web3-3b002.firebaseapp.com", | |
databaseURL: "https://web3-3b002.firebaseio.com", | |
projectId: "web3-3b002", | |
storageBucket: "web3-3b002.appspot.com", | |
messagingSenderId: "45494778793", | |
appId: "1:45494778793:web:7cf73d66fd9ae2bfb1e2b8", | |
measurementId: "G-E35RWMSN0D" | |
}); | |
var db = firebase.firestore(); | |
var docRef = db.collection("shopping-lists").doc("OSjUoao3PR9BrMgQ8gjZ"); | |
//get the document and THEN run it | |
//waiting for document to be fetched | |
//.data returns object with all documents data | |
docRef.get().then(function(doc) { | |
let name = doc.data().name; | |
let items = doc.data().list; | |
listName.innerText = name; | |
items.forEach(function(item){ | |
addItem(item); | |
}) | |
}).catch(function(error) { | |
console.log("Error getting document:", error); | |
}); | |
const shoppingForm = document.getElementById('shoppingForm'); | |
const inputBox = document.getElementById('item'); | |
const list = document.getElementById('shopping-list'); | |
const listName = document.querySelector("#listName") | |
shoppingForm.addEventListener('submit', function (event){ | |
let item =inputBox.value; | |
docRef.update({ | |
list: firebase.firestore.FieldValue.arrayUnion(item) | |
}).then(function(){ | |
addItem(item); | |
}); | |
event.preventDefault(); | |
}); | |
//docRef.ser({ | |
//list: [item] | |
//}, {merge: true} | |
// var docRef = db.collection("shopping-lists").doc("list"); | |
//docRef.update({ | |
//regions: firebase.firestore.FieldValue.arrayUnion(item) | |
//}); | |
//try to add an item without erasing the entire list | |
//array.push | |
function addItem(arg){ | |
if(typeof arg == 'string'){ | |
item = arg; | |
}else{ | |
item =inputBox.value; | |
} | |
// Create list item | |
let li = document.createElement('li'); | |
li.className ="list-item"; | |
//Create input element | |
let input = document.createElement('input') | |
input.type = "checkbox"; | |
input.name = "items"; | |
//Create Span tag | |
let span = document.createElement('span'); | |
span.innerText = item; | |
span.className ="spanyo"; | |
//Create an anchor | |
let a = document.createElement('a'); | |
a.innerText ="x"; | |
a.href = ""; | |
a.addEventListener('click',function(event){ | |
let item = this.previousElementSibling.innerText; | |
let a = this; | |
docRef.update({ | |
list: firebase.firestore.FieldValue.arrayRemove(item) | |
}).then( function() { | |
a.parentNode.remove(); | |
}) | |
event.preventDefault(); | |
}); | |
//this.parentNode.remove(); | |
//console.log(this.previousElementSibling.innerText); | |
//Save all Items | |
// Need to delete item from firebase | |
// Append each element to our list item | |
li.appendChild(input); | |
li.appendChild(span); | |
li.appendChild(a); | |
//console.log(li); | |
list.appendChild(li); | |
inputBox.value = ""; | |
inputBox.focus(); | |
//const tags = document.querySelectorAll(".spanyo"); | |
//console.log(tags); | |
//allItems.forEach(span => console.log()); | |
//console.log(span.innerText); | |
//Queryselecterall - log text | |
//span.innertext | |
// document.queryselectorall | |
//give span a class name | |
}; | |
function getAllItems(){ | |
let items = document.querySelectorAll('.spanyo'); | |
let itemsArray = []; | |
items.forEach(item => { | |
itemsArray.push(item.innerText); | |
}) | |
return itemsArray; | |
} | |
</script> | |
</body> | |
</html> |