diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..f51735d3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +package-lock.json +/node_modules \ No newline at end of file diff --git a/README.md b/README.md index 027584c5e..9b3d6a2c1 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,21 @@ -Assignment 4 - Components -=== +## Morgan's text OwO-ifier +Link: https://a4-patrick-lee22.herokuapp.com/ -Due: October 4th, by 11:59 AM. +This application is designed to take user input and then convert it into "OwO speak." The specific regex rules implemented on the user messages have these effects: -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. +1. All 'L' or 'R' characters are replaced with 'W' characters, case sensitive. +2. All 'N' characters followed by a vowell are replaced with 'Ny', also case sensitive. +3. All '!' characters are replaced with a text face randomly selected from a predetermined list of faces. -[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) +The resulting data from this transformation can be viewed in a table or in a sentence form, at the user's preference. The options for which view is currently active are at the top of the page. -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. +The layout for this page was set up using flexboxes. -Deliverables ---- +## Changes for Assignment 4 +For this assignment, all elements displaying user data and the button used for submission were retooled using React components. It should have the exact same functionality as it did in assignment 2 (as listed above), the primary difference in using this new development framework was the headache I developed while refactoring the code. -Do the following to complete this assignment: +I did not enjoy using React. My problems with React as a framework mostly boil down to my personal preferences when coding and trying my best to preserve the code base as it already existed, which was a challenge. I found out, to my dismay, that I could not update the state of a React component from another function. The only way to set the state of a React component is to do so via an internal method, and the only way these internal methods can be called is via a browser event, originating from a react component. So, this is why the submit button is now part of a React component, along with all of the code to fetch the appdata. -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`. +I originally wished to use React to generate similar blocks of HTML on the fly whenever I needed to insert a new element into the page. I ended up completely changing the structure of my code, and what resulted was a more complicated, somewhat less readable application. I understand the power this framework has. I will probably use this framework again at some point in the future. That fact brings me great pain. -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. +PS: To anyone looking at my commit history, I'm fine, I swear. I'm just a bit overdramatic and I like documenting my pain in the commit messages. \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 000000000..0dd35f1e6 --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "a4-components", + "version": "1.0.0", + "description": "This application is designed to take user input and then convert it into \"OwO speak.\" The specific regex rules implemented on the user messages have these effects:", + "main": "server.improved.js", + "dependencies": { + "babel-cli": "^6.26.0", + "babel-preset-react-app": "^3.1.2", + "mime": "^2.5.2" + }, + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.improved.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/patrick-lee22/a4-components.git" + }, + "keywords": [], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/patrick-lee22/a4-components/issues" + }, + "homepage": "https://github.com/patrick-lee22/a4-components#readme" +} diff --git a/public/css/style.css b/public/css/style.css new file mode 100644 index 000000000..7e6162438 --- /dev/null +++ b/public/css/style.css @@ -0,0 +1,72 @@ +/*Style your own assignment! This is fun! */ +th,td,p,b,input,button { + font-family: 'Courier Prime', monospace; + font-size: large; +} + +button { + cursor:pointer; +} + +a { + font-family: 'Courier Prime', monospace; + font-size: x-large; +} + +.root { + display: flex; + flex-direction: row; + align-content: center; +} + +#sentence-root{ + display: none; +} + +th, td { + border:#333; + border-width: 1px; + border-style: solid; + padding: 1em; +} + +p { + border: #333; + border-width: 1px; + border-style: solid; + margin-right: 1em; + padding-left: 5px; + padding-right:5px; + padding-top: 2px; + padding-bottom: 2px; +} + +b { + border: #333; + border-width: 1px; + border-bottom: solid; + border-top:hidden; + border-left:hidden; + border-right:hidden; + display: flex; + justify-content: center; + padding-left: 5px; + padding-right:5px; + padding-top: 2px; + padding-bottom: 2px; +} + +table { + margin-top:.5em; +} + +form { + display:flex; + flex-direction: column; + margin-top: 1em; + margin-bottom: 1em; +} + +input { + margin-bottom: 1em; +} \ No newline at end of file diff --git a/public/index.html b/public/index.html new file mode 100644 index 000000000..05fdefd0e --- /dev/null +++ b/public/index.html @@ -0,0 +1,28 @@ + + + + + CS4241 Assignment 2 + + + + + + + + + + + +
+ + +
+
+ + + \ No newline at end of file diff --git a/public/js/new_elements.js b/public/js/new_elements.js new file mode 100644 index 000000000..750bd03f5 --- /dev/null +++ b/public/js/new_elements.js @@ -0,0 +1,330 @@ +'use strict'; + +var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } + +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + +function NormalSentence(props) { + return React.createElement( + 'p', + null, + props.name, + ' said: "', + props.message, + '"' + ); +} + +function OwOSentence(props) { + return React.createElement( + 'p', + null, + props.nameowo, + ' said: "', + props.messageowo, + '"' + ); +} + +function NormalRow(props) { + return React.createElement( + 'tr', + null, + React.createElement( + 'td', + null, + props.name + ), + React.createElement( + 'td', + null, + props.message + ) + ); +} + +function OwORow(props) { + return React.createElement( + 'tr', + null, + React.createElement( + 'td', + null, + props.nameowo + ), + React.createElement( + 'td', + null, + props.messageowo + ) + ); +} + +var OwOTable = function (_React$Component) { + _inherits(OwOTable, _React$Component); + + function OwOTable(props) { + _classCallCheck(this, OwOTable); + + return _possibleConstructorReturn(this, (OwOTable.__proto__ || Object.getPrototypeOf(OwOTable)).call(this, props)); + } + + _createClass(OwOTable, [{ + key: 'render', + value: function render() { + return React.createElement( + 'table', + null, + React.createElement( + 'thead', + null, + React.createElement( + 'tr', + null, + React.createElement( + 'th', + null, + 'Name' + ), + React.createElement( + 'th', + null, + 'Message' + ) + ), + this.props.appdata.map(function (elt) { + return OwORow(elt); + }) + ) + ); + } + }]); + + return OwOTable; +}(React.Component); + +var NormalTable = function (_React$Component2) { + _inherits(NormalTable, _React$Component2); + + function NormalTable(props) { + _classCallCheck(this, NormalTable); + + return _possibleConstructorReturn(this, (NormalTable.__proto__ || Object.getPrototypeOf(NormalTable)).call(this, props)); + } + + _createClass(NormalTable, [{ + key: 'render', + value: function render() { + return React.createElement( + 'table', + null, + React.createElement( + 'thead', + null, + React.createElement( + 'tr', + null, + React.createElement( + 'th', + null, + 'Name' + ), + React.createElement( + 'th', + null, + 'Message' + ) + ), + this.props.appdata.map(function (elt) { + return NormalRow(elt); + }) + ) + ); + } + }]); + + return NormalTable; +}(React.Component); + +var OwOParagraphs = function (_React$Component3) { + _inherits(OwOParagraphs, _React$Component3); + + function OwOParagraphs(props) { + _classCallCheck(this, OwOParagraphs); + + return _possibleConstructorReturn(this, (OwOParagraphs.__proto__ || Object.getPrototypeOf(OwOParagraphs)).call(this, props)); + } + + _createClass(OwOParagraphs, [{ + key: 'componentDidMount', + value: function componentDidMount() {} + }, { + key: 'render', + value: function render() { + return React.createElement( + 'div', + null, + this.props.appdata.map(function (elt) { + return OwOSentence(elt); + }) + ); + } + }]); + + return OwOParagraphs; +}(React.Component); + +var NormalParagraphs = function (_React$Component4) { + _inherits(NormalParagraphs, _React$Component4); + + function NormalParagraphs(props) { + _classCallCheck(this, NormalParagraphs); + + var _this4 = _possibleConstructorReturn(this, (NormalParagraphs.__proto__ || Object.getPrototypeOf(NormalParagraphs)).call(this, props)); + + _this4.state = { children: [] }; + return _this4; + } + + _createClass(NormalParagraphs, [{ + key: 'render', + value: function render() { + return React.createElement( + 'div', + null, + this.props.appdata.map(function (elt) { + return NormalSentence(elt); + }) + ); + } + }]); + + return NormalParagraphs; +}(React.Component); + +var ParentElement = function (_React$Component5) { + _inherits(ParentElement, _React$Component5); + + function ParentElement(props) { + _classCallCheck(this, ParentElement); + + var _this5 = _possibleConstructorReturn(this, (ParentElement.__proto__ || Object.getPrototypeOf(ParentElement)).call(this, props)); + + _this5.state = { + appdata: [] + }; + + _this5.componentDidMount = function () { + var that = _this5; + fetch('/getAppdata', { + method: 'GET' + }).then(function (response) { + response.text().then(function (jsonData) { + console.log(jsonData); + var appdata = JSON.parse(jsonData); + that.setState({ appdata: appdata }); + }); + }); + }; + + return _this5; + } + + _createClass(ParentElement, [{ + key: 'submit', + value: function submit() { + // prevent default form action from being carried out + var nameText = document.getElementById('name'); + var targetText = document.getElementById('target-text'); + var json = { + name: nameText.value, + message: targetText.value + }; + var body = JSON.stringify(json); + var that = this; + + fetch('/submit', { + method: 'POST', + body: body + }).then(function (response) { + // do something with the reponse + response.text().then(function (str) { + that.setState(function (prevstate, props) { + appdata: prevstate.appdata.push(JSON.parse(str)); + }); + }); + }); + } + }, { + key: 'render', + value: function render() { + var _this6 = this; + + return React.createElement( + 'div', + null, + React.createElement( + 'button', + { onClick: function onClick() { + return _this6.submit(); + } }, + 'submit' + ), + React.createElement( + 'div', + { id: 'table-root', 'class': 'root' }, + React.createElement( + 'div', + null, + React.createElement( + 'b', + null, + 'Normal Table' + ), + React.createElement(NormalTable, { appdata: this.state.appdata }) + ), + React.createElement( + 'div', + null, + React.createElement( + 'b', + null, + 'OwO-ified Table' + ), + React.createElement(OwOTable, { appdata: this.state.appdata }) + ) + ), + React.createElement( + 'div', + { id: 'sentence-root', 'class': 'root' }, + React.createElement( + 'div', + { id: 'normal-sentence-root' }, + React.createElement( + 'b', + null, + 'Normal Sentences' + ), + React.createElement(NormalParagraphs, { appdata: this.state.appdata }) + ), + React.createElement( + 'div', + { id: 'owo-sentence-root' }, + React.createElement( + 'b', + null, + 'OwO-ified Sentences' + ), + React.createElement(OwOParagraphs, { appdata: this.state.appdata }) + ) + ) + ); + } + }]); + + return ParentElement; +}(React.Component); \ No newline at end of file diff --git a/public/js/scripts.js b/public/js/scripts.js new file mode 100644 index 000000000..774fb167b --- /dev/null +++ b/public/js/scripts.js @@ -0,0 +1,18 @@ +const switchView = function (e) { + if (e.target.id === 'table-nav') { + document.getElementById('table-root').style.display = 'flex'; + document.getElementById('sentence-root').style.display = 'none'; + } else { + document.getElementById('table-root').style.display = 'none'; + document.getElementById('sentence-root').style.display = 'flex'; + } +} + +window.onload = function () { + const tableLink = document.getElementById('table-nav') + const sentenceLink = document.getElementById('sentence-nav') + tableLink.onclick = switchView; + sentenceLink.onclick = switchView; + + ReactDOM.render(React.createElement(ParentElement, null), document.getElementById('main-body-container')); +} \ No newline at end of file diff --git a/server.improved.js b/server.improved.js new file mode 100644 index 000000000..5a42efab7 --- /dev/null +++ b/server.improved.js @@ -0,0 +1,102 @@ +const http = require( 'http' ), + fs = require( 'fs' ), + // IMPORTANT: you must run `npm install` in the directory for this assignment + // to install the mime library used in the following line of code + mime = require( 'mime' ), + dir = 'public/', + port = 3000 + +const appdata = [{ + name: "God", + message: "Welcome to Heaven, kid.", + nameowo: "God", + messageowo: "Wewcome to Heaven, kid." +}]; +const faces = ["(・`ω´・)",";;w;;","owo","UwU",">w<","^w^","(・.◤)","^̮^","(>人<)","( ゚ヮ゚)","(▰˘◡˘▰)"] + +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 ) + console.log(filename); + + if( request.url === '/' ) { + sendFile( response, 'public/index.html' ) + }else if(request.url === '/getAppdata') { + response.writeHead(200, "OK", { 'Content-Type': 'text/plain' }); + console.log(JSON.stringify(appdata)); + response.end(JSON.stringify(appdata)); + }else{ + sendFile( response, filename ) + } +} + +const handlePost = function( request, response ) { + let dataString = '' + + request.on( 'data', function( data ) { + dataString += data + }) + + request.on( 'end', function() { + let jsonData = JSON.parse(dataString); + //console.log(jsonData) + let obj = { + name: jsonData.name, + message: jsonData.message, + nameowo: owoify(jsonData.name), + messageowo: owoify(jsonData.message) + } + appdata.push(obj); + + + response.writeHead( 200, "OK", { 'Content-Type': 'text/plain' }) + response.end(JSON.stringify(obj)); + }) +} + +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' ) + + } + }) +} + +function owoify(text) { + //console.log(text) + let v = text.replace(/[lr]/g, 'w').replace(/[LR]/g, 'W').replace(/n[aeiou]/g, 'ny').replace(/N[aeiou]/g, 'Ny').replace(/N[AEIOU]/g, 'NY'); + let numExclaimations = (v.match(/!/g)||[]).length; + for(let i = 0; i < numExclaimations; i++) { + v = v.replace('!'," " + faces[getRandomInt(0, faces.length)] + " "); + } + return v; +} + +function getRandomInt(min, max) { + min = Math.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min) + min); +} + +server.listen( process.env.PORT || port ) diff --git a/src/new_elements.js b/src/new_elements.js new file mode 100644 index 000000000..a8dd52d02 --- /dev/null +++ b/src/new_elements.js @@ -0,0 +1,158 @@ +'use strict'; + +function NormalSentence(props) { + return

{props.name} said: "{props.message}"

; +} + +function OwOSentence(props) { + return

{props.nameowo} said: "{props.messageowo}"

; +} + +function NormalRow(props) { + return {props.name}{props.message} +} + +function OwORow(props) { + return {props.nameowo}{props.messageowo} +} + +class OwOTable extends React.Component { + constructor(props) { + super(props); + } + + render() { + return + + + + + + {this.props.appdata.map((elt) => + OwORow(elt))} + +
NameMessage
; + } +} + +class NormalTable extends React.Component { + constructor(props) { + super(props); + } + + render() { + return + + + + + + {this.props.appdata.map((elt) => + NormalRow(elt))} + +
NameMessage
; + } +} + +class OwOParagraphs extends React.Component { + constructor(props) { + super(props); + } + + componentDidMount() { + } + + render() { + return
+ {this.props.appdata.map((elt) => + OwOSentence(elt))} +
; + } +} + +class NormalParagraphs extends React.Component { + constructor(props) { + super(props); + this.state = { children: [] }; + } + + render() { + return
+ {this.props.appdata.map((elt) => + NormalSentence(elt))} +
; + } +} + +class ParentElement extends React.Component { + constructor(props) { + super(props); + } + state = { + appdata: [] + } + + componentDidMount = () => { + let that = this; + fetch('/getAppdata', { + method: 'GET', + }).then(function (response) { + response.text().then(function (jsonData) { + console.log(jsonData) + let appdata = JSON.parse(jsonData); + that.setState({ appdata: appdata }) + }) + }) + } + + submit() { + // prevent default form action from being carried out + const nameText = document.getElementById('name'); + const targetText = document.getElementById('target-text'); + let json = { + name: nameText.value, + message: targetText.value + }; + let body = JSON.stringify(json); + let that = this; + + fetch('/submit', { + method: 'POST', + body + }) + .then(function (response) { + // do something with the reponse + response.text().then(function (str) { + that.setState((prevstate, props) => { + appdata: prevstate.appdata.push(JSON.parse(str)); + }) + }) + }) + } + + render() { + return
+ +
+
+ Normal Table + +
+
+ OwO-ified Table + +
+
+
+
+ Normal Sentences + +
+
+ OwO-ified Sentences + +
+
+
+ } +}