Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
firestore
  • Loading branch information
bpd01001 committed Feb 17, 2020
1 parent 050d650 commit 9ee673b
Showing 1 changed file with 22 additions and 53 deletions.
75 changes: 22 additions & 53 deletions week-4/firestore.html
Expand Up @@ -28,7 +28,10 @@
<body>
<h1>My Shopping List: <span id="listName"></span></h1>

<input type="text" name="item" id="item"> <button id="add">Add</button>
<form action="" id="shoppingForm">
<input type="text" name="item" id="item"> <button id="add">Add</button>
</form>


<hr>

Expand Down Expand Up @@ -71,61 +74,22 @@



const addBtn = document.getElementById('add');
const shoppingForm = document.getElementById('shoppingForm');
const inputBox = document.getElementById('item');
const list = document.getElementById('shopping-list');
const listName = document.querySelector("#listName");



addBtn.addEventListener('click', function (){
if (typeof arg === 'string') {
item = arg;
} else {
item = inputBox.value;
}

// docRef.set({
// list: [item]
// }, { merge: true });

// Create list item
let li = document.createElement('li');
li.className = "list-item";

// Create an input element
let input = document.createElement('input');
input.type = "checkbox";
input.name = "items";

// Create a span element
let span = document.createElement('span');
span.innerText = item;

// Create an anchor
let a = document.createElement('a');
a.innerText = "x";
a.href = "";
a.addEventListener('click', function (event) {
this.parentNode.remove();

// Need to delete item from Firebase

event.preventDefault();
});

// Append each element to our list item
li.appendChild(input);
li.appendChild(span);
li.appendChild(a);

// Append our list item to the <ul>
list.appendChild(li);


inputBox.value = "";
inputBox.focus();

shoppingForm.addEventListener('submit', function (event){
let item = inputBox.value;
docRef.update({
list: firebase.firestore.FieldValue.arrayUnion(item)
}).then(function(){
addItem(item);
})

event.preventDefault();
});

function addItem(arg){
Expand Down Expand Up @@ -154,9 +118,14 @@
a.innerText = "x";
a.href = "";
a.addEventListener('click', function (event) {
this.parentNode.remove();

// Need to delete item from Firebase
let item = this.previousSibling.innerText;
let a = this;
docRef.update({
list: firebase.firestore.FieldValue.arrayRemove(item)
}).then( function(){
a.parentNode.remove();
// this.parentNode.remove();
})

event.preventDefault();
});
Expand Down

0 comments on commit 9ee673b

Please sign in to comment.