diff --git a/README.md b/README.md index 54ef094..4e2b58e 100644 --- a/README.md +++ b/README.md @@ -1,68 +1,51 @@ -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +# LAB - Class 24 -## Available Scripts +## RESTy -In the project directory, you can run: +### Randee Orion -### `npm start` +### Links and Resources +- [Submission PR]() +- [CodePen Code]() +- [CodePen Live]() -Runs the app in the development mode.
-Open [http://localhost:3000](http://localhost:3000) to view it in the browser. +### How to initialize / run +Click on the CodePen Live link for a hosted version of the lab. +Click on the CodePen Code link for access to the code. -The page will reload if you make edits.
-You will also see any lint errors in the console. - -### `npm test` - -Launches the test runner in the interactive watch mode.
-See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. +### UML +![UML](lab23UML.png) -### `npm run build` -Builds the app for production to the `build` folder.
-It correctly bundles React in production mode and optimizes the build for the best performance. -The build is minified and the filenames include the hashes.
-Your app is ready to be deployed! -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. -### `npm run eject` -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. -## Learn More -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). -To learn React, check out the [React documentation](https://reactjs.org/). -### Code Splitting -This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting -### Analyzing the Bundle Size - -This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). -### Making a Progressive Web App +## Available Scripts -This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app +In the project directory, you can run: -### Advanced Configuration +### `npm start` -This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration +Runs the app in the development mode.
+Open [http://localhost:3000](http://localhost:3000) to view it in the browser. -### Deployment +The page will reload if you make edits.
+You will also see any lint errors in the console. -This section has moved here: https://facebook.github.io/create-react-app/docs/deployment +### `npm test` -### `npm run build` fails to minify +Launches the test runner in the interactive watch mode.
+See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. -This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify diff --git a/lab23UML.png b/lab23UML.png new file mode 100644 index 0000000..ea43587 Binary files /dev/null and b/lab23UML.png differ diff --git a/package-lock.json b/package-lock.json index 032d0ac..43763e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6617,6 +6617,19 @@ "resolved": "https://registry.npmjs.org/hex-color-regex/-/hex-color-regex-1.1.0.tgz", "integrity": "sha512-l9sfDFsuqtOqKDsQdqrMRk0U85RZc0RtOR9yPI7mRVOa4FsR/BVnZ0shmQRM96Ji99kYZP/7hn1cedc1+ApsTQ==" }, + "history": { + "version": "4.10.1", + "resolved": "https://registry.npmjs.org/history/-/history-4.10.1.tgz", + "integrity": "sha512-36nwAD620w12kuzPAsyINPWJqlNbij+hpK1k9XRloDtym8mxzGYl2c17LnV6IAGB2Dmg4tEa7G7DlawS0+qjew==", + "requires": { + "@babel/runtime": "^7.1.2", + "loose-envify": "^1.2.0", + "resolve-pathname": "^3.0.0", + "tiny-invariant": "^1.0.2", + "tiny-warning": "^1.0.0", + "value-equal": "^1.0.1" + } + }, "hmac-drbg": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/hmac-drbg/-/hmac-drbg-1.0.1.tgz", @@ -6627,6 +6640,14 @@ "minimalistic-crypto-utils": "^1.0.1" } }, + "hoist-non-react-statics": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/hoist-non-react-statics/-/hoist-non-react-statics-3.3.2.tgz", + "integrity": "sha512-/gGivxi8JPKWNm/W0jSmzcMPpfpPLc3dY/6GxhX2hQ9iGj3aDfklV4ET7NjKpSinLpJ5vafa9iiGIEZg10SfBw==", + "requires": { + "react-is": "^16.7.0" + } + }, "hosted-git-info": { "version": "2.8.8", "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz", @@ -8724,6 +8745,15 @@ "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", "integrity": "sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==" }, + "mini-create-react-context": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/mini-create-react-context/-/mini-create-react-context-0.4.0.tgz", + "integrity": "sha512-b0TytUgFSbgFJGzJqXPKCFCBWigAjpjo+Fl7Vf7ZbKRDptszpppKxXH6DRXEABZ/gcEQczeb0iZ7JvL8e8jjCA==", + "requires": { + "@babel/runtime": "^7.5.5", + "tiny-warning": "^1.0.3" + } + }, "mini-css-extract-plugin": { "version": "0.9.0", "resolved": "https://registry.npmjs.org/mini-css-extract-plugin/-/mini-css-extract-plugin-0.9.0.tgz", @@ -11414,6 +11444,52 @@ "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" }, + "react-router": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-5.2.0.tgz", + "integrity": "sha512-smz1DUuFHRKdcJC0jobGo8cVbhO3x50tCL4icacOlcwDOEQPq4TMqwx3sY1TP+DvtTgz4nm3thuo7A+BK2U0Dw==", + "requires": { + "@babel/runtime": "^7.1.2", + "history": "^4.9.0", + "hoist-non-react-statics": "^3.1.0", + "loose-envify": "^1.3.1", + "mini-create-react-context": "^0.4.0", + "path-to-regexp": "^1.7.0", + "prop-types": "^15.6.2", + "react-is": "^16.6.0", + "tiny-invariant": "^1.0.2", + "tiny-warning": "^1.0.0" + }, + "dependencies": { + "isarray": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=" + }, + "path-to-regexp": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.8.0.tgz", + "integrity": "sha512-n43JRhlUKUAlibEJhPeir1ncUID16QnEjNpwzNdO3Lm4ywrBpBZ5oLD0I6br9evr1Y9JTqwRtAh7JLoOzAQdVA==", + "requires": { + "isarray": "0.0.1" + } + } + } + }, + "react-router-dom": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-5.2.0.tgz", + "integrity": "sha512-gxAmfylo2QUjcwxI63RhQ5G85Qqt4voZpUXSEqCwykV0baaOTQDR1f0PmY8AELqIyVc0NEZUj0Gov5lNGcXgsA==", + "requires": { + "@babel/runtime": "^7.1.2", + "history": "^4.9.0", + "loose-envify": "^1.3.1", + "prop-types": "^15.6.2", + "react-router": "5.2.0", + "tiny-invariant": "^1.0.2", + "tiny-warning": "^1.0.0" + } + }, "react-scripts": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-3.4.1.tgz", @@ -11841,6 +11917,11 @@ "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-3.0.0.tgz", "integrity": "sha1-six699nWiBvItuZTM17rywoYh0g=" }, + "resolve-pathname": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/resolve-pathname/-/resolve-pathname-3.0.0.tgz", + "integrity": "sha512-C7rARubxI8bXFNB/hqcp/4iUeIXJhJZvFPFPiSPRnhU5UPxzMFIl+2E6yY6c4k9giDJAhtV+enfA+G89N6Csng==" + }, "resolve-url": { "version": "0.2.1", "resolved": "https://registry.npmjs.org/resolve-url/-/resolve-url-0.2.1.tgz", @@ -13379,6 +13460,16 @@ "resolved": "https://registry.npmjs.org/timsort/-/timsort-0.3.0.tgz", "integrity": "sha1-QFQRqOfmM5/mTbmiNN4R3DHgK9Q=" }, + "tiny-invariant": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/tiny-invariant/-/tiny-invariant-1.1.0.tgz", + "integrity": "sha512-ytxQvrb1cPc9WBEI/HSeYYoGD0kWnGEOR8RY6KomWLBVhqz0RgTwVO9dLrGz7dC+nN9llyI7OKAgRq8Vq4ZBSw==" + }, + "tiny-warning": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/tiny-warning/-/tiny-warning-1.0.3.tgz", + "integrity": "sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==" + }, "tmp": { "version": "0.0.33", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", @@ -13763,6 +13854,11 @@ "spdx-expression-parse": "^3.0.0" } }, + "value-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/value-equal/-/value-equal-1.0.1.tgz", + "integrity": "sha512-NOJ6JZCAWr0zlxZt+xqCHNTEKOsrks2HQd4MqhP1qy4z1SkbEP467eNx6TgDKXMvUOb+OENfJCZwM+16n7fRfw==" + }, "vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index 3382afe..d20943e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "node-sass": "^4.14.1", "react": "^16.13.1", "react-dom": "^16.13.1", + "react-router-dom": "^5.2.0", "react-scripts": "3.4.1" }, "scripts": { diff --git a/src/App.js b/src/App.js index 3d3ba27..b4be466 100644 --- a/src/App.js +++ b/src/App.js @@ -1,15 +1,158 @@ import React from 'react'; +import { BrowserRouter, Route } from 'react-router-dom'; import './styles/styles.sass' -import RESTy from './components/RESTy' +import If from './components/If'; +import Form from './components/Form'; +import Results from './components/Results'; +import History from './components/History'; +import Header from './components/Header'; -function App() { - return ( -
- -
- ); +class App extends React.Component { + constructor(props) { + super(props); + this.state={ + url: '', + method: 'GET', + body: '', + headers: '', + responseBody: '', + responseHeaders: '', + history: [], + loading: false, + }; + } + + onURLChange(e) { + this.setState({ url: e.target.value }); + } + + onMethodChange(e) { + this.setState({ method: e.target.value }); + } + + onBodyChange(e) { + this.setState({ body: e.target.value }); + } + + onHeadersChange(e) { + this.setState({ headers: e.target.value }); + } + + async onRerun(request) { + await this.setState({ + url: request.url, + body: request.body, + method: request.method, + headers: request.headers, + }); + + await this.onSubmit({ rerun: true }); + + } + async onSubmit(e) { + await this.setState({ loading: true}); + let request = { + url: this.state.url, + method: this.state.method, + headers: this.state.headers, + body: this.state.body + } + + let resBody = {}; + let resHeaders = {}; + + let res = await fetch(this.state.url, { + method: this.state.method, + body: this.state.method ==='GET' + ? null + : this.state.body + ? JSON.parse(this.state.body) + : null, + headers: this.state.headers + ? { + ...JSON.parse(this.state.headers), + Accept: 'application/json', + } + : {Accept: 'application/json'}, + }); + + if(res.status === 200 || res.status === 201) { + if (!e.rerun) + await this.setState({ + history: [...this.state.history, request], + }); + + resBody = await res.json(); + + for (const entry of res.headers.entries()) { + resHeaders[entry[0]] = entry[1]; + } + } else resBody = res.status + res.statusText; + + this.setState({ + loading: false, + responseBody: resBody, + resHeaders: resHeaders, + }); + + } + + render() { + return ( +
+ +
+ +
+ + + + + + + + + +

Loading...

+
+ + + + + +
+ ); + } } + + + export default App; diff --git a/src/components/Form.js b/src/components/Form.js index 5429f33..60141d6 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -1,20 +1,43 @@ import React from 'react'; function FormInput(props){ - function handleChange(e){ - props.onChange(e.target.value, props.stateKey); - } return ( -
- - +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
- ); +); } export default FormInput; \ No newline at end of file diff --git a/src/components/Header.js b/src/components/Header.js new file mode 100644 index 0000000..b7bfd6e --- /dev/null +++ b/src/components/Header.js @@ -0,0 +1,20 @@ +import React from 'react'; +import { Link } from 'react-router-dom'; + +function Header(props){ + return ( +
+ +

RESTy Application

+
+ ); +} + +export default Header; \ No newline at end of file diff --git a/src/components/History.js b/src/components/History.js new file mode 100644 index 0000000..c8c3e0a --- /dev/null +++ b/src/components/History.js @@ -0,0 +1,68 @@ +import React from 'react'; +import If from './If'; +import { Redirect } from 'react-router-dom'; + +class History extends React.Component { + constructor(props) { + super(props); + this.state = { + redirect: false, + }; + } + + onRerun(request) { + this.props.onRerun(request); + this.setState({ redirect: true }); + } + + render() { + let historyElements = []; + for(let i=0; i < this.props.history.length; i++) { + if(this.props.size === 'light') { + historyElements.push( +
+ {this.props.history[i].url} + {this.props.history[i].method} + +
+ ); + } else { + historyElements.push( +
+ {this.props.history[i].url} + {this.props.history[i].method} + +
+ {this.props.history[i].body} + {this.props.history[i].headers} +
+
+ + ); + } + } + + return ( + <> + + + + +
{historyElements}
+
+ + ) + } +} + +export default History; \ No newline at end of file diff --git a/src/components/If.js b/src/components/If.js new file mode 100644 index 0000000..51c47b6 --- /dev/null +++ b/src/components/If.js @@ -0,0 +1,8 @@ +import React from 'react'; + +function If(props) { + if(props.condition) return <>{props.children} + else return null; +} + +export default If; \ No newline at end of file diff --git a/src/components/RESTy.js b/src/components/RESTy.js index 5df8b8d..7cd0642 100644 --- a/src/components/RESTy.js +++ b/src/components/RESTy.js @@ -7,48 +7,70 @@ class RESTy extends React.Component { super(props); this.state = { - url: 'https://gitschooledalexaapp.herokuapp.com/potions', + url: '', method: 'GET', - results: [], + headers: {}, + body: {}, } }; - async updateAPIParams(val, key){ - await this.setState({...this.state, [key]: val}) + onURLChange(e){ + console.log('inside URL CHANGE handler') + console.log('*****************'); + console.log('THIS DOT STATE'); + console.log('*****************'); + console.log(this.state) + console.log('e.target.val', e.target.value); + this.setState({...this.state, url: e.target.value}) } - async fetchThatAPI () { - let url = this.state.url; + onMethodChange(e){ + this.setState({...this.state, method: e.target.value}); + } + + async onSubmit () { + // console.log('*****************'); + // console.log('THIS DOT STATE'); + // console.log('*****************'); + console.log(this.state) + console.log(' attempting to make a ', this.state.method, 'request to ', this.state.url); + + let body; + let headers = {}; - let res = await fetch(url, { - method: this.state.method, + let res = await fetch(this.state.url, { + method: this.state.method, headers: { Accept: 'application/json', } }); - let data = await res.json(); - data = data.results; - - let objects = []; + if (res.status === 200) { + body = await res.json(); - for(let i = 0; i < data.length; i ++) { - objects.push({ src: data[i].urls.thumb, desc: data[i].description }) + for (const entry of res.headers.entries()) { + headers[entry[0]] = entry[1]; + } } - this.setState({ ...this.state, results: objects }) + this.setState({ headers, body }); + } render() { return ( <> -

GET all your RESTfull info here!

+

RESTy

+ ) diff --git a/src/components/Results.js b/src/components/Results.js index e69de29..6f8f33a 100644 --- a/src/components/Results.js +++ b/src/components/Results.js @@ -0,0 +1,16 @@ +import React from 'react'; + +function Results(props){ + return( +
+
+
{JSON.stringify(props.headers, null, props.tabWidth)}
+
+
+
{JSON.stringify(props.body, null, props.tabWidth)}
+
+
+ ) +} + +export default Results; \ No newline at end of file