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
34 changes: 5 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
24 changes: 24 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
53 changes: 53 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -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 )
5 changes: 5 additions & 0 deletions snowpack.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"mount": {
"src": "/dist"
}
}
100 changes: 100 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<form onSubmit={(e) => this.handleSubmit(e)}>
<label>
Input a Number:
<input name = "add" type="text" value={this.state.add} onChange={this.handleChange}/>
</label>
<input type="submit" value="Submit"/>
</form>

<form onSubmit={(e) => this.handleRemove(e)}>
<label>
Remove an Index:
<input name = "remove" type="text" value={this.state.remove} onChange={this.handleChange} />
</label>
<input type="submit" value="Remove"/>
</form>
</div>

);
}
}

export default App;
19 changes: 19 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<title>Empty project</title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<h1 class = "center">
Welcome to Counter
</h1>
<p class = "center">
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.
</p>

<div id="app"></div>
<script type="module" src="index.js"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";


var mountNode = document.getElementById("app");
ReactDOM.render(<App name ="test"/>, mountNode);
28 changes: 28 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

@import url('https://fonts.googleapis.com/css?family=Aboreto&display=swap');
body {
background-color: #9FC2D7;
display:flex;
flex-flow: column;
}
h1{
font-family: "Aboreto", serif;
}
p {
color:#717069;
font-family: "Aboreto", serif;
}
button {
background-color: #D1AF66;
font-family: "Aboreto", serif;
}
input{
background-color: #9FC2D7;
font-family: "Aboreto", serif;
}
.center{
text-align: center;
}
#removeButton{
background-color: #F04132;
}