diff --git a/README.md b/README.md index 027584c5e..5ed9edd1a 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,9 @@ -Assignment 4 - Components -=== +## Counter -Due: October 4th, by 11:59 AM. +https://butternut-elfin-van.glitch.me/ -For this assignment you will re-implement the client side portion of *either* A2 or A3 using either React or Svelte components. If you choose A3 you only need to use components for the data display / updating; you can leave your login UI as is. +Include a very brief summary of your project here and what you changed / added to assignment #2. Briefly (3–4 sentences) answer the following question: did the new technology improve or hinder the development experience? +- I used react to change the way the website sent and recieved information from the server. The input fields are programmed reactively, along with the information display. With Express and react, the code for the client and server were both much more consise +and streamlined. However, it was much more difficult to set up due to the requirement of snowpack to continually build the project. All in all, I think that for smaller deployments such as this, it is less beneficial to use React, due to size and complexity overheads -[Svelte Tutorial](https://github.com/cs4241-21a/cs4241-21a.github.io/blob/main/using_svelte.md) -[React Tutorial](https://github.com/cs4241-21a/cs4241-21a.github.io/blob/main/using_react.md) -This project can be implemented on any hosting service (Glitch, DigitalOcean, Heroku etc.), however, you must include all files in your GitHub repo so that the course staff can view them. - -Deliverables ---- - -Do the following to complete this assignment: - -1. Implement your project with the above requirements. -3. Test your project to make sure that when someone goes to your main page on Glitch/Heroku/etc., it displays correctly. -4. Ensure that your project has the proper naming scheme `a4-firstname-lastname` so we can find it. -5. Fork this repository and modify the README to the specifications below. Be sure to add *all* project files. -6. Create and submit a Pull Request to the original repo. Name the pull request using the following template: `a4-firstname-lastname`. - -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 - -your hosting link e.g. http://a4-charlieroberts.glitch.me - -Include a very brief summary of your project here and what you changed / added to assignment #3. Briefly (3–4 sentences) answer the following question: did the new technology improve or hinder the development experience? - -Unlike previous assignments, this assignment will be solely graded on whether or not you successfully complete it. Partial credit will be generously given. diff --git a/package.json b/package.json new file mode 100644 index 000000000..7f979c736 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "a4-jake-matthews", + "version": "1.0.0", + "description": "", + "main": "index.js", + "keywords": [], + "author": "", + "license": "ISC", + "scripts": { + "clean": "rm dist/bundle.js", + "dev": "snowpack dev", + "build": "snowpack build", + "start": "node server.js" + }, + "dependencies": { + "express": "^4.18.1", + "mime": "^3.0.0", + "react": "^18.2.0", + "react-dom": "^18.2.0" + }, + "devDependencies": { + "snowpack": "^3.8.8" + } +} diff --git a/server.js b/server.js new file mode 100644 index 000000000..50eb4e931 --- /dev/null +++ b/server.js @@ -0,0 +1,53 @@ +const express = require( 'express' ), + app = express() + +let total = 0, numbers = [], allData = [total,numbers] + +app.use( express.json() ) + +// this will most likely be 'build' or 'public' +app.use( express.static( 'build' )) + +app.get( '/', ( req, res ) => { + let url = "." + req.url + if( req.url === '/' ) { + res.redirect("dist/index.html") + }else{ + res.redirect(url) + } +}) + +app.post('/remove', (req,res) => { + let indexToRemove = Number(req.body.indexToRemove) + console.log(req.body) + console.log(indexToRemove) + let numToRemove = numbers[indexToRemove] + total -= numToRemove + numbers.splice(indexToRemove,1) + allData = [total,numbers] + res.json(allData) +}) + +app.post( '/add', ( req,res ) => { + let json = req.body //json = {numToAdd: '5'} + //console.log(json.numToAdd) + + if(!isNaN(json.numToAdd)){ + total += Number(json.numToAdd) + //let now = new Date() + //times.push(now.toLocaleTimeString('en-US')) + //console.log(now.toLocaleTimeString('en-US')) + allData = ["Total: " + total, numbers] + numbers.push(Number(json.numToAdd)) + }else{ + console.log("Incorrect Format: Number not submitted") + } + + //console.log(numbers) + //console.log(total) + + res.writeHead( 200, "OK", {'Content-Type': 'text/plain' }) + res.end(JSON.stringify(allData)) +}) + +app.listen( process.env.PORT || 8080 ) diff --git a/snowpack.config.json b/snowpack.config.json new file mode 100644 index 000000000..20fdfd888 --- /dev/null +++ b/snowpack.config.json @@ -0,0 +1,5 @@ +{ + "mount": { + "src": "/dist" + } +} diff --git a/src/App.jsx b/src/App.jsx new file mode 100644 index 000000000..51a98540b --- /dev/null +++ b/src/App.jsx @@ -0,0 +1,100 @@ +import React from 'react'; +import './style.css'; + +//Main Component +class App extends React.Component { + constructor( props ) { + super( props ) + // initialize our state + this.state = {add:0,remove:0} + + this.handleChange = this.handleChange.bind(this); + this.handleSubmit = this.handleSubmit.bind(this); + this.handleRemove = this.handleRemove.bind(this); + } + + handleChange(event) { + const value = event.target.value; + const name = event.target.name; + this.setState({ + [name]: value + }); + } + + handleSubmit(event) { + //alert('A name was submitted: ' + this.state.add); + event.preventDefault(); + fetch( '/add', { + method:'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ numToAdd: this.state.add}) + }) + .then( response => response.json() ) + .then( json => { + let testData = !!document.getElementById("p"); + if(testData){ + document.getElementById("p").remove() + document.getElementById("p").remove() + //document.getElementById("p").remove() + } + json.forEach( item => { + const p = document.createElement('p') + p.id = "p" + p.innerText = JSON.stringify( item ) + document.body.appendChild( p ) + }) + }) + } + + handleRemove(event){ + //console.log("removing"); + //alert('A number was removed: ' + this.state.remove); + event.preventDefault(); + + fetch( '/remove', { + method:'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ indexToRemove: this.state.remove}) + }) + .then( response => response.json() ) + .then( json => { + let testData = !!document.getElementById("p"); + if(testData){ + document.getElementById("p").remove() + document.getElementById("p").remove() + //document.getElementById("p").remove() + } + json.forEach( item => { + const p = document.createElement('p') + p.id = "p" + p.innerText = JSON.stringify( item ) + document.body.appendChild( p ) + }) + }) + } + + render() { + return ( +
+ This is a simple webpage for keeping track of numbers. Each submission is added to a running total, along with a timestamp. Add or subtract any numbers you wish. +
+ + + + + diff --git a/src/index.jsx b/src/index.jsx new file mode 100644 index 000000000..ca1b6febc --- /dev/null +++ b/src/index.jsx @@ -0,0 +1,7 @@ +import React from "react"; +import ReactDOM from "react-dom"; +import App from "./App"; + + +var mountNode = document.getElementById("app"); +ReactDOM.render(