diff --git a/.env b/.env new file mode 100644 index 000000000..00606b308 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +REACT_APP_PUSHER_APP_ID=669186 +REACT_APP_PUSHER_CLUSTER=us2 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..4d29575de --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* diff --git a/README.md b/README.md index e5734085b..897dc8366 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,44 @@ -# LambdaMUD-Client -Front-end for the [LambdaMUD-Project](https://github.com/LambdaSchool/LambdaMUD-Project). +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). -Fork this repo and put your front-end client code in your fork. +## Available Scripts + +In the project directory, you can run: + +### `npm start` + +Runs the app in the development mode.
+Open [http://localhost:3000](http://localhost:3000) to view it in the browser. + +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. + +### `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/). diff --git a/package.json b/package.json new file mode 100644 index 000000000..050602c7a --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "mud-client", + "version": "0.1.0", + "private": true, + "dependencies": { + "axios": "^0.18.0", + "pusher": "^2.2.0", + "pusher-js": "^4.3.1", + "react": "^16.6.3", + "react-dom": "^16.6.3", + "react-router": "^4.3.1", + "react-router-dom": "^4.3.1", + "react-scripts": "2.1.1" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "eslintConfig": { + "extends": "react-app" + }, + "browserslist": [ + ">0.2%", + "not dead", + "not ie <= 11", + "not op_mini all" + ], + "devDependencies": { + "dotenv": "^6.2.0" + } +} diff --git a/public/_redirects b/public/_redirects new file mode 100644 index 000000000..50a463356 --- /dev/null +++ b/public/_redirects @@ -0,0 +1 @@ +/* /index.html 200 \ No newline at end of file diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 000000000..a11777cc4 Binary files /dev/null and b/public/favicon.ico differ diff --git a/public/index.html b/public/index.html new file mode 100644 index 000000000..90f2d12d3 --- /dev/null +++ b/public/index.html @@ -0,0 +1,41 @@ + + + + + + + + + + + React App + + + +
+ + + diff --git a/public/manifest.json b/public/manifest.json new file mode 100644 index 000000000..1f2f141fa --- /dev/null +++ b/public/manifest.json @@ -0,0 +1,15 @@ +{ + "short_name": "React App", + "name": "Create React App Sample", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/src/components/App/App.css b/src/components/App/App.css new file mode 100644 index 000000000..92f956e80 --- /dev/null +++ b/src/components/App/App.css @@ -0,0 +1,32 @@ +.App { + text-align: center; +} + +.App-logo { + animation: App-logo-spin infinite 20s linear; + height: 40vmin; +} + +.App-header { + background-color: #282c34; + min-height: 100vh; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + font-size: calc(10px + 2vmin); + color: white; +} + +.App-link { + color: #61dafb; +} + +@keyframes App-logo-spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } +} diff --git a/src/components/App/App.js b/src/components/App/App.js new file mode 100644 index 000000000..8033c73a0 --- /dev/null +++ b/src/components/App/App.js @@ -0,0 +1,22 @@ +import React, { Component } from "react"; +import { NavLink, Route } from "react-router-dom"; +import { withRouter } from "react-router"; +import Login from "../Login/Login"; +import Signup from "../Signup/Signup"; +import Mud from "../Mud/Mud"; +import "./App.css"; +import { Link } from "react-router-dom"; + +class App extends Component { + render() { + return ( +
+ + + +
+ ); + } +} + +export default withRouter(App); diff --git a/src/components/App/App.test.js b/src/components/App/App.test.js new file mode 100644 index 000000000..a754b201b --- /dev/null +++ b/src/components/App/App.test.js @@ -0,0 +1,9 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import App from './App'; + +it('renders without crashing', () => { + const div = document.createElement('div'); + ReactDOM.render(, div); + ReactDOM.unmountComponentAtNode(div); +}); diff --git a/src/components/Login/Login.css b/src/components/Login/Login.css new file mode 100644 index 000000000..e69de29bb diff --git a/src/components/Login/Login.js b/src/components/Login/Login.js new file mode 100644 index 000000000..42732c42e --- /dev/null +++ b/src/components/Login/Login.js @@ -0,0 +1,94 @@ +import React, { Component } from "react"; +import { Link } from "react-router-dom"; +import { Redirect, Switch } from "react-router"; +import axios from "axios"; + +class Login extends Component { + state = { + user: { + username: "", + password: "" + } + }; + + handleChange = event => { + this.setState({ + user: { + ...this.state.user, + [event.target.name]: event.target.value + } + }); + }; + + loginUser() { + axios + .post( + "https://backend-mud-lambda.herokuapp.com/api/login/", + this.state.user + ) + .then(response => { + localStorage.setItem("accessToken", response.data.key); + }) + .catch(err => { + alert(err.response.request.responseText); + }); + } + + handleSubmit = () => { + this.loginUser(); + setTimeout(() => { + if (localStorage.getItem("accessToken")) { + this.props.history.push("/mud"); + } + }, 700); + }; + + render() { + return ( +
+
+
+

Login

+
+
+
+
+
+ + +
+
+
+
{ + this.handleSubmit(); + }} + > + Submit +
+
+
+ + Click here to Signup + +
+ ); + } +} + +export default Login; diff --git a/src/components/Mud/Mud.css b/src/components/Mud/Mud.css new file mode 100644 index 000000000..11a035b9d --- /dev/null +++ b/src/components/Mud/Mud.css @@ -0,0 +1,28 @@ +.mud-container { + margin-top: 20px; +} + +.textarea { + background-color: #fff; +} + +.link-mud { + margin-top: 10px; + text-decoration: none; + color: black; + background-color: #3abeff; + border-radius: 4px; + padding: 5px 10px; +} + +.log { + text-decoration: none; + color: black; + background-color: #3abeff; + border-radius: 4px; + padding: 5px 10px; + width: 100px; + margin: 0 auto; + margin-top: 10px; + cursor: pointer; +} diff --git a/src/components/Mud/Mud.js b/src/components/Mud/Mud.js new file mode 100644 index 000000000..901b56108 --- /dev/null +++ b/src/components/Mud/Mud.js @@ -0,0 +1,199 @@ +import React, { Component } from "react"; +import axios from "axios"; +import Pusher from "pusher-js"; +import { Link } from "react-router-dom"; +import "./Mud.css"; +require("dotenv").config(); + +class Mud extends Component { + state = { + input: "", + uuid: "" + }; + + pusherSetup() { + // Enable pusher logging - don't include this in production + Pusher.logToConsole = true; + var pusher = new Pusher("858967c7631c17228127", { + cluster: "us2", + forceTLS: true + }); + var channel = pusher.subscribe(`p-channel-${this.state.uuid}`); + channel.bind("broadcast", function(data) { + document.getElementById("textArea").value += `\n${data.message}`; + }); + } + + // initializes mud + initMud() { + if (localStorage.getItem("accessToken")) { + axios + .get("https://backend-mud-lambda.herokuapp.com/api/adv/init/", { + headers: { + Authorization: `Token ${localStorage.getItem("accessToken")}` + } + }) + .then(response => { + // sets uuid and initializes room with description + this.state.uuid = response.data.uuid; + document.getElementById("textArea").value += `${ + response.data.title + }\n${response.data.description}\n`; + // sets if any players are in the area + response.data.players.map(p => { + document.getElementById( + "textArea" + ).value += `\n${p} is standing there.`; + }); + document.getElementById( + "textArea" + ).value += `\n\nInput n,s,e,w for north,south,east, and west to move accordingly\nType anything else to speak in chat.\n=================================================`; + this.pusherSetup(); + }) + .catch(error => {}); + } else { + document.getElementById( + "textArea" + ).value += `You are not logged in. Please go back to the Login Page`; + } + } + + // checks what the input is and does proper request + command() { + if ( + (this.state.input === "n") | + (this.state.input === "e") | + (this.state.input === "w") | + (this.state.input === "s") + ) { + axios + .post( + "https://backend-mud-lambda.herokuapp.com/api/adv/move/", + { direction: `${this.state.input}` }, + { + headers: { + Authorization: `Token ${localStorage.getItem("accessToken")}` + } + } + ) + .then(response => { + document.getElementById("textArea").value += `\n\n${ + response.data.title + }\n${response.data.description}\n`; + response.data.players.map(p => { + document.getElementById( + "textArea" + ).value += `\n${p} is standing there.`; + }); + document.getElementById( + "textArea" + ).value += `\n=================================================`; + document.getElementById( + "textArea" + ).scrollTop = document.getElementById("textArea").scrollHeight; + }) + .catch(error => {}); + } else { + // If not a direction then produces say + axios + .post( + "https://backend-mud-lambda.herokuapp.com/api/adv/say/", + { message: `${this.state.input}` }, + { + headers: { + Authorization: `Token ${localStorage.getItem("accessToken")}` + } + } + ) + .then(response => { + document.getElementById("textArea").value += `\n\n${ + response.data.message + }`; + document.getElementById( + "textArea" + ).scrollTop = document.getElementById("textArea").scrollHeight; + }) + .catch(error => {}); + } + } + + handleChange = event => { + this.setState({ + input: event.target.value + }); + }; + + handleSubmit = event => { + this.command(); + document.getElementById("input").value = ""; + }; + + componentDidMount() { + this.initMud(); + } + + render() { + return ( +
+
+ {localStorage.getItem("accessToken") ? ( +
{ + localStorage.removeItem("accessToken"); + this.props.history.push("/"); + }} + > + Log Out +
+ ) : ( +
{ + this.props.history.push("/"); + }} + > + Log In +
+ )} +
+
+