From 32c8fc265b1fd820ffda887664bbe003bf527e34 Mon Sep 17 00:00:00 2001 From: willhockey20 Date: Mon, 11 Sep 2023 01:34:48 -0400 Subject: [PATCH 1/6] basic setup done, have working creation and deletion, want to setup individual deletion of list items though --- package.json | 2 +- public/css/main.css | 34 +++++++- public/index.html | 31 +++++++- public/js/main.js | 189 +++++++++++++++++++++++++++++++++++++++++--- server.improved.js | 74 ----------------- 5 files changed, 238 insertions(+), 92 deletions(-) delete mode 100644 server.improved.js diff --git a/package.json b/package.json index 988f135f..2fbe673f 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,6 @@ "start": "node server.improved.js" }, "dependencies": { - "mime": "^2.4.4" + "mime": "^2.6.0" } } diff --git a/public/css/main.css b/public/css/main.css index 7cf6207b..9f8412e0 100644 --- a/public/css/main.css +++ b/public/css/main.css @@ -1,4 +1,36 @@ body { - background:black; + background: gray; color:white; + font-family:"Oswald", sans-serif; + font-size: 14px; +} +h1{ + font-family:"Oswald", serif; + font-size: 48px; + color:aquamarine; +} +.lists{ + display: flex; + justify-content: flex-start; + padding-left:7rem +} + +#groceryList{ + display: flex; + flex-direction:column; + list-style: none; +} +#cartList{ + display: flex; + flex-direction:column; + list-style: none; +} +.errorMsg{ + visibility: hidden; + display: none; + color:red; + font-size: 18px; +} +.priceCounts{ + } \ No newline at end of file diff --git a/public/index.html b/public/index.html index 59d90d3b..75be0190 100644 --- a/public/index.html +++ b/public/index.html @@ -1,18 +1,41 @@ - CS4241 Assignment 2 + Grocery List - + + +

Your Grocery List

- - + + + + +
+ +
+ + + +
+
+ + + +
+ diff --git a/public/js/main.js b/public/js/main.js index a569258f..38234997 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -1,27 +1,192 @@ -// FRONT-END (CLIENT) JAVASCRIPT HERE - +let total = 0.00 const submit = async function( event ) { - // stop form submission from trying to load - // a new .html page for displaying results... - // this was the original browser behavior and still - // remains to this day event.preventDefault() - const input = document.querySelector( '#yourname' ), - json = { yourname: input.value }, + let err = document.getElementsByClassName("errorMsg")[0]; + const itemin = document.querySelector( "#item" ), + pricein = document.querySelector("#price"), + json = { item: {itemName: itemin.value, price: pricein.value} }, body = JSON.stringify( json ) + if(isNaN(parseFloat(json.item.price))) + { + err.style.display = "flex" + err.style.visibility = "visible" + return false; + } + else{ + err.style.display = "none" + err.style.visibility = "hidden" + } const response = await fetch( '/submit', { method:'POST', body }) - const text = await response.text() + const data = await response.json() + let list; + let tmp; + if(document.getElementById("groceryList") === null) + { + list = document.createElement('ul') + list.setAttribute("id", "groceryList") + } + else{ + list = document.getElementById("groceryList") + tmp = [].slice.call(list.children); + } + console.log(tmp) + if(tmp[1].innerText == "Item Name: Price($)") + { + console.log(tmp[1]) + list.removeChild(tmp[1]) + const li = document.createElement('li') + const myIn = document.createElement('input') + const checkLabel = document.createElement("label") + checkLabel.appendChild(document.createTextNode(data.groceryList[0].itemName[0].toUpperCase() + data.groceryList[0].itemName.slice(1) + `: $${data.groceryList[0].price}`)) + myIn.setAttribute("type", "checkbox") + myIn.className = "giBox" + li.className = "groceryItem" + li.appendChild(myIn); + li.appendChild(checkLabel); + list.appendChild( li ) + } + for(let i = 0; i < data.groceryList.length; i++) + { + console.log(i, tmp.length) + if(i >= (tmp.length - 1)) + { + const li = document.createElement('li') + const myIn = document.createElement('input') + const checkLabel = document.createElement("label") + checkLabel.appendChild(document.createTextNode(data.groceryList[i].itemName[0].toUpperCase() + data.groceryList[i].itemName.slice(1) + `: $${data.groceryList[i].price}`)) + myIn.setAttribute("type", "checkbox") + myIn.className = "giBox" + li.className = "groceryItem" + li.appendChild(myIn); + li.appendChild(checkLabel); + list.appendChild( li ) + } + } + let tp = document.getElementById("tpNum"); + tp.innerText = `$${data.totalPrice.totalPrice.toFixed(2)}` + total = parseFloat(data.totalPrice.totalPrice.toFixed(2)) + +} + +const reset = async function( event ) +{ + event.preventDefault(); + workingTotal = 0.0 + let body = JSON.stringify({id: "reset"}) + + const resetResponse = await fetch( "/reset", { + method:'DELETE', + body + }) + + let list; + if(document.getElementById("groceryList") === null) + { + list = document.createElement('ul') + list.setAttribute("id", "groceryList") + list.appendChild(listLabel) + list.appendChild(defaultListItem); + document.getElementById("lists-container").appendChild(list) + } + else{ + list = document.getElementById("groceryList") + list.innerHTML = "" + } + list.appendChild(listLabel) + list.appendChild(defaultListItem); - console.log( 'text:', text ) + let tp = document.getElementById("tpNum"); + tp.innerText = `$0.00` + tp = document.getElementById("icNum") + tp.innerText = `$0.00` + tp = document.getElementById("difNum") + tp.innerText = `$0.00` + + document.getElementById("cartList").innerHTML = "" + document.getElementById("cartList").appendChild(cartLabel) } +const defaultListItem = document.createElement("li"); +const defaultIn = document.createElement('input') +const defaultLabel = document.createElement("label") +const cartLabel = document.createElement("label") +const listLabel = document.createElement("label") + +let workingTotal = 0.00; +let glist; window.onload = function() { - const button = document.querySelector("button"); + + defaultLabel.appendChild(document.createTextNode("Item Name: Price($)")) + defaultIn.setAttribute("type", "checkbox") + defaultListItem.className = "groceryItem" + defaultListItem.appendChild(defaultIn); + defaultListItem.appendChild(defaultLabel); + listLabel.appendChild(document.createTextNode("Check items In Cart")) + listLabel.id = "glist-lab" + cartLabel.appendChild(document.createTextNode("In Your Cart")) + cartLabel.id = "cart-lab" + + const button = document.getElementById("submit"); + const resetBut = document.getElementById("reset"); button.onclick = submit; -} \ No newline at end of file + resetBut.onclick = reset; + + gList = document.getElementById("groceryList"); + + gList.addEventListener('change', function(e){ + + if(e.target.classList.contains("giBox")){ + let myLi = e.target.parentElement; + let cartList = document.getElementById("cartList"); + if(e.target.checked){ + let temp = myLi.cloneNode(); + let txt = myLi.innerText; + let num = txt.slice(txt.indexOf("$") + 1) + if(isNaN(parseFloat(num))) + { + //workingTotal+= parseFloat(num.slice(num.indexOf("$"))) + console.log(parseFloat(num.slice(num.indexOf(" ")))) + console.log(num.slice(num.indexOf("$") + 1), "NaN") + console.log(workingTotal) + } + else{ + workingTotal+= parseFloat(num); + console.log(parseFloat(num), "Num") + console.log(workingTotal) + } + document.getElementById("icNum").innerText= `$${workingTotal.toFixed(2)}`; + document.getElementById("difNum").innerText= `$${(total - workingTotal).toFixed(2)}`; + temp.innerHTML = "" + temp.innerText = txt + cartList.appendChild(temp) + } + else{ + let arr = [].slice.call(cartList.children) + arr.every(i => { + if(i.innerText === myLi.innerText) + { + let tnum = i.innerText.slice(i.innerText.indexOf("$") + 1) + if(isNaN(parseFloat(tnum))) + { + workingTotal-= parseFloat(tnum.slice(tnum.indexOf(" ") + 1)) + } + else{ + workingTotal-= parseFloat(tnum); + } + document.getElementById("icNum").innerText= `$${workingTotal.toFixed(2)}`; + document.getElementById("difNum").innerText= `$${Math.abs((total - workingTotal).toFixed(2))}`; + i.remove() + return false; + } + return true; + }) + } + } +}) +} diff --git a/server.improved.js b/server.improved.js deleted file mode 100644 index 9ac27fb8..00000000 --- a/server.improved.js +++ /dev/null @@ -1,74 +0,0 @@ -const http = require( 'http' ), - fs = require( 'fs' ), - // IMPORTANT: you must run `npm install` in the directory for this assignment - // to install the mime library if you're testing this on your local machine. - // However, Glitch will install it automatically by looking in your package.json - // file. - mime = require( 'mime' ), - dir = 'public/', - port = 3000 - -const appdata = [ - { 'model': 'toyota', 'year': 1999, 'mpg': 23 }, - { 'model': 'honda', 'year': 2004, 'mpg': 30 }, - { 'model': 'ford', 'year': 1987, 'mpg': 14} -] - -const server = http.createServer( function( request,response ) { - if( request.method === 'GET' ) { - handleGet( request, response ) - }else if( request.method === 'POST' ){ - handlePost( request, response ) - } -}) - -const handleGet = function( request, response ) { - const filename = dir + request.url.slice( 1 ) - - if( request.url === '/' ) { - sendFile( response, 'public/index.html' ) - }else{ - sendFile( response, filename ) - } -} - -const handlePost = function( request, response ) { - let dataString = '' - - request.on( 'data', function( data ) { - dataString += data - }) - - request.on( 'end', function() { - console.log( JSON.parse( dataString ) ) - - // ... do something with the data here!!! - - response.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) - response.end('test') - }) -} - -const sendFile = function( response, filename ) { - const type = mime.getType( filename ) - - fs.readFile( filename, function( err, content ) { - - // if the error = null, then we've loaded the file successfully - if( err === null ) { - - // status code: https://httpstatuses.com - response.writeHeader( 200, { 'Content-Type': type }) - response.end( content ) - - }else{ - - // file not found, error code 404 - response.writeHeader( 404 ) - response.end( '404 Error: File Not Found' ) - - } - }) -} - -server.listen( process.env.PORT || port ) From b3f41d4e27a4f6d58e059ae19cf09ce8cef37b32 Mon Sep 17 00:00:00 2001 From: willhockey20 Date: Mon, 11 Sep 2023 01:35:52 -0400 Subject: [PATCH 2/6] actually commited the gitignore and the server code now --- .gitignore | 2 + server.js | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 .gitignore create mode 100644 server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f51735d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +package-lock.json +/node_modules \ No newline at end of file diff --git a/server.js b/server.js new file mode 100644 index 00000000..a8860603 --- /dev/null +++ b/server.js @@ -0,0 +1,121 @@ +const http = require("http"), + fs = require("fs"), + // IMPORTANT: you must run `npm install` in the directory for this assignment + // to install the mime library if you're testing this on your local machine. + // However, Glitch will install it automatically by looking in your package.json + // file. + mime = require("mime"), + dir = "public/", + port = 3000; + +const appdata = [ + { model: "toyota", year: 1999, mpg: 23 }, + { model: "honda", year: 2004, mpg: 30 }, + { model: "ford", year: 1987, mpg: 14 }, +]; + +const totalPrice = { totalPrice: 0.0 }; +let retObject; + +const groceryList = []; + +const server = http.createServer(function (request, response) { + + /*switch (request.method){ + case "GET": + handleGet(request, response); + break; + case "POST": + handlePost(request, response); + break; + case "DELETE": + handleDelete(request, response); + break; + + }*/ + if (request.method === "GET") { + handleGet(request, response); + } else if (request.method === "POST") { + handlePost(request, response); + }else if (request.method === "DELETE") { + handleDelete(request, response); + } + +}); + +const handleGet = function (request, response) { + const filename = dir + request.url.slice(1); + + if (request.url === "/") { + sendFile(response, "public/index.html"); + } else { + sendFile(response, filename); + } +}; + +const calcTotalPrice = function () { + totalPrice.totalPrice = 0.0; + if(groceryList.length !== 0){groceryList.forEach((item) => { + if (!isNaN(parseFloat(item.price))) { + totalPrice.totalPrice += parseFloat(item.price); + } else { + totalPrice.totalPrice += 0.0; + } + })}; +}; +const handlePost = function (request, response) { + let dataString = ""; + + request.on("data", function (data) { + dataString += data; + }); + + request.on("end", function () { + const newItem = JSON.parse(dataString).item; + groceryList.push(newItem); + calcTotalPrice(); + //console.log(totalPrice); + retObject = { groceryList, totalPrice }; + console.log(retObject) + response.writeHead(200, "OK", { "Content-Type": "text/json" }); + response.end(JSON.stringify(retObject)); + }); + +}; + +const handleDelete = function (request, response) { + + let dataString = ""; + + request.on("data", function (data) { + dataString += data; + }); + + request.on("end", function () { + groceryList.splice(0, groceryList.length); + console.log(groceryList) + calcTotalPrice(); + response.writeHead(200, "OK", { "Content-Type": "text/json" }); + response.end(JSON.stringify(groceryList)); + }); + +}; + +const sendFile = function (response, filename) { + const type = mime.getType(filename); + + fs.readFile(filename, function (err, content) { + // if the error = null, then we've loaded the file successfully + if (err === null) { + // status code: https://httpstatuses.com + response.writeHeader(200, { "Content-Type": type }); + response.end(content); + } else { + // file not found, error code 404 + response.writeHeader(404); + response.end("404 Error: File Not Found"); + } + }); +}; + +server.listen(process.env.PORT || port); From dee60f98f7ffd18cbccbd79cc778420532cd4107 Mon Sep 17 00:00:00 2001 From: willhockey20 Date: Mon, 11 Sep 2023 16:28:46 -0400 Subject: [PATCH 3/6] Modifying item prices now works --- public/index.html | 3 +- public/js/main.js | 175 ++++++++++++++++++++++++++++++++++++++-------- server.js | 41 ++++++++--- 3 files changed, 179 insertions(+), 40 deletions(-) diff --git a/public/index.html b/public/index.html index 75be0190..142c1f02 100644 --- a/public/index.html +++ b/public/index.html @@ -18,6 +18,7 @@

Your Grocery List

+ @@ -30,7 +31,7 @@
    -
  • Item Name: Price($)
  • +
  • Item Name: Price($)
    diff --git a/public/js/main.js b/public/js/main.js index 38234997..4d8e8a72 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -24,6 +24,90 @@ const submit = async function( event ) { }) const data = await response.json() + + addList(data); + +} + +const modify = async function (event){ + event.preventDefault() + let idxs = []; + let idx = 0; + let list = [].slice.call(document.getElementById("groceryList").children) + list = list.splice(1) + list.forEach(element => { + if(element.getElementsByClassName("modbox").length === 1) + { + if(element.getElementsByClassName("modbox")[0].checked){ + idxs.push(idx); + } + } + idx+= 1; + }); + + let err = document.getElementsByClassName("errorMsg")[0]; + const pricein = document.querySelector("#price"), + json = { items: idxs, price: pricein.value}, + body = JSON.stringify( json ) + if(isNaN(parseFloat(json.price))) + { + err.style.display = "flex" + err.style.visibility = "visible" + return false; + } + else{ + err.style.display = "none" + err.style.visibility = "hidden" + } + + const response = await fetch( '/modify', { + method:'POST', + body + }) + + const data = await response.json() + + updateList(data); +} + +const reset = async function( event ) +{ + event.preventDefault(); + workingTotal = 0.0 + let body = JSON.stringify({id: "reset"}) + + const resetResponse = await fetch( "/reset", { + method:'DELETE', + body + }) + + let list; + if(document.getElementById("groceryList") === null) + { + list = document.createElement('ul') + list.setAttribute("id", "groceryList") + list.appendChild(listLabel) + list.appendChild(defaultListItem); + document.getElementById("lists-container").appendChild(list) + } + else{ + list = document.getElementById("groceryList") + list.innerHTML = "" + } + list.appendChild(listLabel) + list.appendChild(defaultListItem); + + let tp = document.getElementById("tpNum"); + tp.innerText = `$0.00` + tp = document.getElementById("icNum") + tp.innerText = `$0.00` + tp = document.getElementById("difNum") + tp.innerText = `$0.00` + + document.getElementById("cartList").innerHTML = "" + document.getElementById("cartList").appendChild(cartLabel) +} +const addList = function(data){ let list; let tmp; if(document.getElementById("groceryList") === null) @@ -35,20 +119,25 @@ const submit = async function( event ) { list = document.getElementById("groceryList") tmp = [].slice.call(list.children); } - console.log(tmp) + //console.log(tmp) if(tmp[1].innerText == "Item Name: Price($)") { console.log(tmp[1]) list.removeChild(tmp[1]) const li = document.createElement('li') const myIn = document.createElement('input') + const inTwo = document.createElement('input') const checkLabel = document.createElement("label") checkLabel.appendChild(document.createTextNode(data.groceryList[0].itemName[0].toUpperCase() + data.groceryList[0].itemName.slice(1) + `: $${data.groceryList[0].price}`)) myIn.setAttribute("type", "checkbox") myIn.className = "giBox" + inTwo.setAttribute("type", "checkbox") + inTwo.className = "modbox" li.className = "groceryItem" li.appendChild(myIn); li.appendChild(checkLabel); + li.appendChild(inTwo) + li.id = "item-0" list.appendChild( li ) } for(let i = 0; i < data.groceryList.length; i++) @@ -58,58 +147,84 @@ const submit = async function( event ) { { const li = document.createElement('li') const myIn = document.createElement('input') + const inTwo = document.createElement('input') const checkLabel = document.createElement("label") checkLabel.appendChild(document.createTextNode(data.groceryList[i].itemName[0].toUpperCase() + data.groceryList[i].itemName.slice(1) + `: $${data.groceryList[i].price}`)) myIn.setAttribute("type", "checkbox") myIn.className = "giBox" + inTwo.setAttribute("type", "checkbox") + inTwo.className = "modbox" li.className = "groceryItem" + li.id = `item-${i}` li.appendChild(myIn); li.appendChild(checkLabel); + li.appendChild(inTwo) list.appendChild( li ) } } let tp = document.getElementById("tpNum"); tp.innerText = `$${data.totalPrice.totalPrice.toFixed(2)}` total = parseFloat(data.totalPrice.totalPrice.toFixed(2)) - } -const reset = async function( event ) -{ - event.preventDefault(); - workingTotal = 0.0 - let body = JSON.stringify({id: "reset"}) - - const resetResponse = await fetch( "/reset", { - method:'DELETE', - body - }) - +const updateList = function(data){ let list; + let tmp; if(document.getElementById("groceryList") === null) { list = document.createElement('ul') list.setAttribute("id", "groceryList") - list.appendChild(listLabel) - list.appendChild(defaultListItem); - document.getElementById("lists-container").appendChild(list) } else{ list = document.getElementById("groceryList") - list.innerHTML = "" + tmp = [].slice.call(list.children); } - list.appendChild(listLabel) - list.appendChild(defaultListItem); - - let tp = document.getElementById("tpNum"); - tp.innerText = `$0.00` - tp = document.getElementById("icNum") - tp.innerText = `$0.00` - tp = document.getElementById("difNum") - tp.innerText = `$0.00` - - document.getElementById("cartList").innerHTML = "" - document.getElementById("cartList").appendChild(cartLabel) + //console.log(tmp) + if(tmp[1].innerText == "Item Name: Price($) ") + { + console.log(tmp[1]) + list.removeChild(tmp[1]) + const li = document.createElement('li') + const myIn = document.createElement('input') + const inTwo = document.createElement('input') + const checkLabel = document.createElement("label") + checkLabel.appendChild(document.createTextNode(data.groceryList[0].itemName[0].toUpperCase() + data.groceryList[0].itemName.slice(1) + `: $${data.groceryList[0].price}`)) + myIn.setAttribute("type", "checkbox") + myIn.className = "giBox" + inTwo.setAttribute("type", "checkbox") + inTwo.className = "modbox" + li.className = "groceryItem" + li.appendChild(myIn); + li.appendChild(checkLabel); + li.appendChild(inTwo) + li.id = "item-0" + list.appendChild( li ) + } + for(let i = 0; i < data.groceryList.length; i++) + { + const li = document.createElement('li') + const myIn = document.createElement('input') + const inTwo = document.createElement('input') + const checkLabel = document.createElement("label") + checkLabel.appendChild(document.createTextNode(data.groceryList[i].itemName[0].toUpperCase() + data.groceryList[i].itemName.slice(1) + `: $${data.groceryList[i].price}`)) + myIn.setAttribute("type", "checkbox") + myIn.className = "giBox" + console.log(tmp[i + 1].getElementsByClassName("giBox")[0].checked) + myIn.checked = tmp[i + 1].getElementsByClassName("giBox")[0].checked + inTwo.setAttribute("type", "checkbox") + inTwo.className = "modbox" + li.className = "groceryItem" + li.id = `item-${i}` + li.appendChild(myIn); + li.appendChild(checkLabel); + li.appendChild(inTwo) + list.removeChild(tmp[i+1]) + list.appendChild( li ) + + } + let tp = document.getElementById("tpNum"); + tp.innerText = `$${data.totalPrice.totalPrice.toFixed(2)}` + total = parseFloat(data.totalPrice.totalPrice.toFixed(2)) } const defaultListItem = document.createElement("li"); @@ -134,6 +249,8 @@ window.onload = function() { const button = document.getElementById("submit"); const resetBut = document.getElementById("reset"); + const modBut = document.getElementById("modify"); + modBut.onclick = modify; button.onclick = submit; resetBut.onclick = reset; diff --git a/server.js b/server.js index a8860603..b6c7690e 100644 --- a/server.js +++ b/server.js @@ -63,6 +63,13 @@ const calcTotalPrice = function () { } })}; }; + +const modifyPrice = function (data){ + data.items.forEach(idx => { + console.log(groceryList[idx]) + groceryList[idx].price = data.price; + }) +} const handlePost = function (request, response) { let dataString = ""; @@ -70,16 +77,30 @@ const handlePost = function (request, response) { dataString += data; }); - request.on("end", function () { - const newItem = JSON.parse(dataString).item; - groceryList.push(newItem); - calcTotalPrice(); - //console.log(totalPrice); - retObject = { groceryList, totalPrice }; - console.log(retObject) - response.writeHead(200, "OK", { "Content-Type": "text/json" }); - response.end(JSON.stringify(retObject)); - }); + if(request.url ==="/submit") + { + request.on("end", function () { + const newItem = JSON.parse(dataString).item; + groceryList.push(newItem); + calcTotalPrice(); + //console.log(totalPrice); + retObject = { groceryList, totalPrice }; + console.log(retObject) + response.writeHead(200, "OK", { "Content-Type": "text/json" }); + response.end(JSON.stringify(retObject)); + }); + } + else{ + request.on("end", function () { + modifyPrice(JSON.parse(dataString)) + calcTotalPrice(); + retObject = { groceryList, totalPrice }; + console.log(retObject) + response.writeHead(200, "OK", { "Content-Type": "text/json" }); + response.end(JSON.stringify(retObject)); + }); + } + }; From ee0393a0f1aa6bcf2c34e16c8ddc84f9b6333f56 Mon Sep 17 00:00:00 2001 From: willhockey20 Date: Tue, 12 Sep 2023 11:50:02 -0400 Subject: [PATCH 4/6] commiting for glitch --- README.md | 15 +++++++++------ public/css/main.css | 10 +++++++++- public/index.html | 4 +++- public/js/main.js | 31 +++++++++++++++++++++++++++++++ server.js | 37 ++++++++++++++++++++++++++++++++++++- 5 files changed, 88 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 493995c1..cba13520 100644 --- a/README.md +++ b/README.md @@ -67,9 +67,7 @@ Acheivements Below are suggested technical and design achievements. You can use these to help boost your grade up to an A and customize the assignment to your personal interests. These are recommended acheivements, but feel free to create/implement your own... just make sure you thoroughly describe what you did in your README and why it was challenging. ALL ACHIEVEMENTS MUST BE DESCRIBED IN YOUR README IN ORDER TO GET CREDIT FOR THEM. Remember, the highest grade you can get on any individual assignment is a 100%. *Technical* -- (5 points) Create a single-page app that both provides a form for users to submit data and always shows the current state of the server-side data. To put it another way, when the user submits data, the server should respond sending back the updated data (including the derived field calculated on the server) and the client should then update its data display. -- (5 points) In addition to a form enabling adding and deleting data on the server, also add the ability to modify existing data. *Design/UX* - (5 points per person, with a max of 10 points) Test your user interface with other students in the class. Define a specific task for them to complete (ideally something short that takes <10 minutes), and then use the [think-aloud protocol](https://en.wikipedia.org/wiki/Think_aloud_protocol) to obtain feedback on your design (talk-aloud is also fine). Important considerations when designing your study: @@ -87,14 +85,19 @@ You'll need to use sometype of collaborative software that will enable you both *You do not need to actually make changes based on their feedback*. This acheivement is designed to help gain experience testing user interfaces. If you run two user studies, you should answer two sets of questions. -Sample Readme (delete the above when you're ready to submit, and modify the below so with your links and descriptions) ---- - ## Your Web Application Title Include a very brief summary of your project here. Be sure to include the CSS positioning technique you used, and any required instructions to use your application. +This is a basic grocery list maker. It keeps track of pricing per item and total price. You can reset the whole list and modify items in the list. It uses the CSS Flex/Flexbox for positioning certain elements on the page. To modify an item's price, enter a price in the price field, check the checkbox to the right of the item (or items, can modify multiple items at the same time), then click the Modify Item Price button. All items checked off will have their price in the list updated. I should note that its only the list itself that'll get the price updated. If you update the price of an item in the cart it won't reflect because it was added to the cart before the price change. + ## Technical Achievements -- **Tech Achievement 1**: Using a combination of... +- **Tech Achievement 1**: Created a single-page app that provides a form for users to submit items for their grocery list along with their estimated price. The server calculates what the total cost (before any tax for taxable items) of the list would be and sends that back along side the actual list itself for the client to display the list and total price. Users can reset the entire list + +- **Tech Achievement Two**: Users can modify the price of any item added to their grocery list ### Design/Evaluation Achievements - **Design Achievement 1**: +1. Heineman +2. He thought it was hard to tell when the list was resetting because of my sample item being similarly styled from a font perspective. He also said that it was unclear what the middle checkbox was and that I should add a label for it. +3. I was surprised with his raving about the total price calculation. That felt like such a basic feature. He liked the simplicity of my design which kinda surprised me also because I thought it was a little too simple in its current state. I was going for simplicity but I thought the current design is a little too simple. Most of ym styling was positioning, not actual design styling from the colors and other things +4. I added a label to identify what the middle checkbox does due to his feedback. I also would like to add more distinction between different elements based on his feedback. They all look too similar and some design distinctions could be beneficial. diff --git a/public/css/main.css b/public/css/main.css index 9f8412e0..cbe92885 100644 --- a/public/css/main.css +++ b/public/css/main.css @@ -14,12 +14,20 @@ h1{ justify-content: flex-start; padding-left:7rem } - +.groceryItem{ + font-weight: 300; +} +label{ + font-family: "Oswald", serif; +} #groceryList{ display: flex; flex-direction:column; list-style: none; } +#groceryList #modbox-label{ + padding-left: 1.5rem; +} #cartList{ display: flex; flex-direction:column; diff --git a/public/index.html b/public/index.html index 142c1f02..f6743779 100644 --- a/public/index.html +++ b/public/index.html @@ -19,6 +19,7 @@

    Your Grocery List

    + @@ -30,7 +31,8 @@
      - + +
    • Item Name: Price($)
      diff --git a/public/js/main.js b/public/js/main.js index 4d8e8a72..3d2dac9b 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -167,6 +167,35 @@ const addList = function(data){ total = parseFloat(data.totalPrice.totalPrice.toFixed(2)) } +const delItems = async function ( event ){ + event.preventDefault() + + let idxs = []; + let idx = 0; + let list = [].slice.call(document.getElementById("groceryList").children) + list = list.splice(1) + list.forEach(element => { + if(element.getElementsByClassName("modbox").length === 1) + { + if(element.getElementsByClassName("modbox")[0].checked){ + idxs.push(idx); + } + } + idx+= 1; + }); + + const json = {items: idxs}, + body = JSON.stringify( json ) + const response = await fetch( '/delete', { + method:'DELETE', + body + }) + + const data = await response.json() + + updateList(data); +} + const updateList = function(data){ let list; let tmp; @@ -250,9 +279,11 @@ window.onload = function() { const button = document.getElementById("submit"); const resetBut = document.getElementById("reset"); const modBut = document.getElementById("modify"); + const delBut = document.getElementById("delete") modBut.onclick = modify; button.onclick = submit; resetBut.onclick = reset; + delBut.onclick = delItems; gList = document.getElementById("groceryList"); diff --git a/server.js b/server.js index b6c7690e..95e7d0c3 100644 --- a/server.js +++ b/server.js @@ -103,7 +103,31 @@ const handlePost = function (request, response) { }; +const deleteItems = function(data){ + let temp = [] + outerLoop: data.items.forEach(idx =>{ + if(idx < groceryList.length){ + innerLoop: for(let i = 0; i < groceryList.length; i++) + { + if(i !== idx){ + temp.push(groceryList[i]) + } + else{ + groceryList.splice(idx, 1) + } + }} + else{ + groceryList.splice(idx, 1) + } + }); + + console.log(temp) + groceryList.splice(0, groceryList.length); + temp.forEach(item =>{ + groceryList.push(item) + }); +} const handleDelete = function (request, response) { let dataString = ""; @@ -112,13 +136,24 @@ const handleDelete = function (request, response) { dataString += data; }); + if(request.url === "/reset"){ request.on("end", function () { groceryList.splice(0, groceryList.length); console.log(groceryList) calcTotalPrice(); response.writeHead(200, "OK", { "Content-Type": "text/json" }); response.end(JSON.stringify(groceryList)); - }); + });} + else{ + request.on("end", function () { + deleteItems(JSON.parse(dataString)) + calcTotalPrice(); + retObject = { groceryList, totalPrice }; + console.log(retObject) + response.writeHead(200, "OK", { "Content-Type": "text/json" }); + response.end(JSON.stringify(retObject)); + }); + } }; From 93bbac7ef00f41794459e5efb1f2e611815e5f95 Mon Sep 17 00:00:00 2001 From: willhockey20 Date: Tue, 12 Sep 2023 11:51:29 -0400 Subject: [PATCH 5/6] second glitch commit, forgot a readme thing --- README.md | 87 ------------------------------------------------------- 1 file changed, 87 deletions(-) diff --git a/README.md b/README.md index cba13520..7a344503 100644 --- a/README.md +++ b/README.md @@ -1,90 +1,3 @@ -Assignment 2 - Short Stack: Basic Two-tier Web Application using HTML/CSS/JS and Node.js -=== - -Due: September 11th, by 11:59 AM. - -This assignment aims to introduce you to creating a prototype two-tiered web application. -Your application will include the use of HTML, CSS, JavaScript, and Node.js functionality, with active communication between the client and the server over the life of a user session. - -Baseline Requirements ---- - -There is a large range of application areas and possibilities that meet these baseline requirements. -Try to make your application do something useful! A todo list, storing / retrieving high scores for a very simple game... have a little fun with it. - -Your application is required to implement the following functionalities: - -- a `Server` which not only serves files, but also maintains a tabular dataset with 3 or more fields related to your application -- a `Results` functionality which shows the entire dataset residing in the server's memory -- a `Form/Entry` functionality which allows a user to add or delete data items residing in the server's memory -- a `Server Logic` which, upon receiving new or modified "incoming" data, includes and uses a function that adds at least one additional derived field to this incoming data before integrating it with the existing dataset -- the `Derived field` for a new row of data must be computed based on fields already existing in the row. -For example, a `todo` dataset with `task`, `priority`, and `creation_date` may generate a new field `deadline` by looking at `creation_date` and `priority` - -Your application is required to demonstrate the use of the following concepts: - -HTML: -- One or more [HTML Forms](https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms), with any combination of form tags appropriate for the user input portion of the application -- A results page displaying all data currently available on the server. You will most likely use a `` tag for this, but `
        ` or `
          ` could also work and might be simpler to work with. Alternatively, you can create a single-page app (see Technical Acheivements) but this is not a requirement. -- All pages should [validate](https://validator.w3.org) -- If your app contains multple pages, they should all be accessible from the homepage (index.html) - -CSS: -- CSS styling of the primary visual elements in the application -- Various CSS Selector functionality must be demonstrated: - - Element selectors - - ID selectors - - Class selectors -- CSS positioning and styling of the primary visual elements in the application: - - Use of either a CSS grid or flexbox for layout - - Rules defining fonts for all text used; no default fonts! Be sure to use a web safe font or a font from a web service like [Google Fonts](http://fonts.google.com/) - -- CSS defined in a maintainable, readable form, in external stylesheets - -JavaScript: -- At minimum, a small amount of front-end JavaScript to get / fetch data from the server; a sample is provided in this repository. - -Node.js: -- An HTTP Server that delivers all necessary files and data for the application, and also creates the required `Derived Fields` in your data. -A starting point is provided in this repository. - -Deliverables ---- - -Do the following to complete this assignment and acheive a base grade of 85%: - -1. Fork the starting project code (make sure to fork the 2023 repo!). This repo contains some starter code that may be used or discarded as needed. -2. Implement your project with the above requirements. -3. Test your project to make sure that when someone goes to your main page, it displays correctly. -4. Deploy your project to Glitch, and fill in the appropriate fields in your package.json file. -5. Ensure that your project has the proper naming scheme `a2-yourGithubUsername` so we can find it. -6. Modify the README to the specifications below, and delete all of the instructions originally found in this README. -7. Create and submit a Pull Request to the original repo. Label the pull request as follows: a2-gitusername-firstname-lastname - -Acheivements ---- - -Below are suggested technical and design achievements. You can use these to help boost your grade up to an A and customize the assignment to your personal interests. These are recommended acheivements, but feel free to create/implement your own... just make sure you thoroughly describe what you did in your README and why it was challenging. ALL ACHIEVEMENTS MUST BE DESCRIBED IN YOUR README IN ORDER TO GET CREDIT FOR THEM. Remember, the highest grade you can get on any individual assignment is a 100%. - -*Technical* - - -*Design/UX* -- (5 points per person, with a max of 10 points) Test your user interface with other students in the class. Define a specific task for them to complete (ideally something short that takes <10 minutes), and then use the [think-aloud protocol](https://en.wikipedia.org/wiki/Think_aloud_protocol) to obtain feedback on your design (talk-aloud is also fine). Important considerations when designing your study: - -1. Make sure you start the study by clearly stating the task that you expect your user to accomplish. -2. You shouldn't provide any verbal instructions on how to use your interface / accomplish the task you give them. Make sure that your interface is clear enough that users can figure it out without any instruction, or provide text instructions from within the interface itself. -3. If users get stuck to the point where they give up, you can then provde instruction so that the study can continue, but make sure to discuss this in your README. You won't lose any points for this... all feedback is good feedback! - -You'll need to use sometype of collaborative software that will enable you both to see the test subject's screen and listen to their voice as they describe their thoughts, or conduct the studies in person. After completing each study, briefly (one to two sentences for each question) address the following in your README: - -1. Provide the last name of each student you conduct the evaluation with. -2. What problems did the user have with your design? -3. What comments did they make that surprised you? -4. What would you change about the interface based on their feedback? - -*You do not need to actually make changes based on their feedback*. This acheivement is designed to help gain experience testing user interfaces. If you run two user studies, you should answer two sets of questions. - ## Your Web Application Title Include a very brief summary of your project here. Be sure to include the CSS positioning technique you used, and any required instructions to use your application. From ee842d8aa8510b8058ff5fd282f11a07e3c184f0 Mon Sep 17 00:00:00 2001 From: willhockey20 Date: Tue, 12 Sep 2023 11:55:45 -0400 Subject: [PATCH 6/6] Final for pull --- README.md | 4 +++- package.json | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7a344503..982e30f1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -## Your Web Application Title +## a2-wfryan (Grocery List Maker) + +https://a2-wfryan.glitch.me/ Include a very brief summary of your project here. Be sure to include the CSS positioning technique you used, and any required instructions to use your application. This is a basic grocery list maker. It keeps track of pricing per item and total price. You can reset the whole list and modify items in the list. It uses the CSS Flex/Flexbox for positioning certain elements on the page. To modify an item's price, enter a price in the price field, check the checkbox to the right of the item (or items, can modify multiple items at the same time), then click the Modify Item Price button. All items checked off will have their price in the list updated. I should note that its only the list itself that'll get the price updated. If you update the price of an item in the cart it won't reflect because it was added to the cart before the price change. diff --git a/package.json b/package.json index 2fbe673f..3b4a6d56 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "", "author": "", "scripts": { - "start": "node server.improved.js" + "start": "node server.js" }, "dependencies": { "mime": "^2.6.0"