diff --git a/README.md b/README.md
index 897dc836..be76334b 100644
--- a/README.md
+++ b/README.md
@@ -1,44 +1,8 @@
-This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
+# Drawing in Space
-## Available Scripts
+## Usage
-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/).
+| Command | Use |
+| ---------------- | -------------------------------------------------------------- |
+| `npm start` | Launches the client app development server on `localhost:3000` |
+| `npm run server` | Launches the space station API server on `localhost:1337` |
diff --git a/package-lock.json b/package-lock.json
index 176e9685..06b949a9 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -2389,8 +2389,8 @@
},
"body-parser": {
"version": "1.19.0",
- "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
- "integrity": "sha512-dhEPs72UPbDnAQJ9ZKMNTP6ptJaionhP5cBb541nXPlW60Jepo9RV/a4fX4XWW9CuFNK22krhrj1+rgzifNCsw==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/body-parser/-/body-parser-1.19.0.tgz",
+ "integrity": "sha1-lrJwnlfJxOCab9Zqj9l5hE9p8Io=",
"requires": {
"bytes": "3.1.0",
"content-type": "~1.0.4",
@@ -2406,13 +2406,13 @@
"dependencies": {
"bytes": {
"version": "3.1.0",
- "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
- "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/bytes/-/bytes-3.1.0.tgz",
+ "integrity": "sha1-9s95M6Ng4FiPqf3oVlHNx/gF0fY="
},
"debug": {
"version": "2.6.9",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
- "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/debug/-/debug-2.6.9.tgz",
+ "integrity": "sha1-XRKFFd8TT/Mn6QpMk/Tgd6U2NB8=",
"requires": {
"ms": "2.0.0"
}
@@ -2424,8 +2424,8 @@
},
"qs": {
"version": "6.7.0",
- "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
- "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/qs/-/qs-6.7.0.tgz",
+ "integrity": "sha1-QdwaAV49WB8WIXdr4xr7KHapsbw="
}
}
},
@@ -3624,16 +3624,16 @@
},
"content-disposition": {
"version": "0.5.3",
- "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz",
- "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/content-disposition/-/content-disposition-0.5.3.tgz",
+ "integrity": "sha1-4TDK9+cnkIfFYWwgB9BIVpiYT70=",
"requires": {
"safe-buffer": "5.1.2"
}
},
"content-type": {
"version": "1.0.4",
- "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz",
- "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/content-type/-/content-type-1.0.4.tgz",
+ "integrity": "sha1-4TjMdeBAxyexlm/l5fjJruJW/js="
},
"convert-source-map": {
"version": "1.6.0",
@@ -3645,8 +3645,8 @@
},
"cookie": {
"version": "0.4.0",
- "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.0.tgz",
- "integrity": "sha512-+Hp8fLp57wnUSt0tY0tHEXh4voZRDnoIrZPqlo3DPiI4y9lwg/jqx+1Om94/W6ZaPDOUbnjOt/99w66zk+l1Xg=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/cookie/-/cookie-0.4.0.tgz",
+ "integrity": "sha1-vrQ35wIrO21JAZ0IhmUwPr6cFLo="
},
"cookie-signature": {
"version": "1.0.6",
@@ -3697,6 +3697,15 @@
"resolved": "https://plangrid.jfrog.io/plangrid/api/npm/npm/core-util-is/-/core-util-is-1.0.2.tgz",
"integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
},
+ "cors": {
+ "version": "2.8.5",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/cors/-/cors-2.8.5.tgz",
+ "integrity": "sha1-6sEdpRWS3Ya58G9uesKTs9+HXSk=",
+ "requires": {
+ "object-assign": "^4",
+ "vary": "^1"
+ }
+ },
"cosmiconfig": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-5.2.1.tgz",
@@ -5200,8 +5209,8 @@
},
"express": {
"version": "4.17.1",
- "resolved": "https://registry.npmjs.org/express/-/express-4.17.1.tgz",
- "integrity": "sha512-mHJ9O79RqluphRrcw2X/GTh3k9tVv8YcoyY4Kkh4WDMUYKRZUq0h1o0w2rrrxBqM7VoeUVqgb27xlEMXTnYt4g==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/express/-/express-4.17.1.tgz",
+ "integrity": "sha1-RJH8OGBc9R+GKdOcK10Cb5ikwTQ=",
"requires": {
"accepts": "~1.3.7",
"array-flatten": "1.1.1",
@@ -5242,8 +5251,8 @@
},
"debug": {
"version": "2.6.9",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
- "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/debug/-/debug-2.6.9.tgz",
+ "integrity": "sha1-XRKFFd8TT/Mn6QpMk/Tgd6U2NB8=",
"requires": {
"ms": "2.0.0"
}
@@ -5255,8 +5264,8 @@
},
"qs": {
"version": "6.7.0",
- "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz",
- "integrity": "sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/qs/-/qs-6.7.0.tgz",
+ "integrity": "sha1-QdwaAV49WB8WIXdr4xr7KHapsbw="
}
}
},
@@ -5720,8 +5729,8 @@
},
"finalhandler": {
"version": "1.1.2",
- "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.2.tgz",
- "integrity": "sha512-aAWcW57uxVNrQZqFXjITpW3sIUQmHGG3qSb9mUah9MgMC4NeWhNOlNjXEYq3HjRAvL6arUviZGGJsBg6z0zsWA==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/finalhandler/-/finalhandler-1.1.2.tgz",
+ "integrity": "sha1-t+fQAP/RGTjQ/bBTUG9uur6fWH0=",
"requires": {
"debug": "2.6.9",
"encodeurl": "~1.0.2",
@@ -5734,8 +5743,8 @@
"dependencies": {
"debug": {
"version": "2.6.9",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
- "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/debug/-/debug-2.6.9.tgz",
+ "integrity": "sha1-XRKFFd8TT/Mn6QpMk/Tgd6U2NB8=",
"requires": {
"ms": "2.0.0"
}
@@ -7132,8 +7141,8 @@
},
"http-errors": {
"version": "1.7.2",
- "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz",
- "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/http-errors/-/http-errors-1.7.2.tgz",
+ "integrity": "sha1-T1ApzxMjnzEDblsuVSkrz7zIXI8=",
"requires": {
"depd": "~1.1.2",
"inherits": "2.0.3",
@@ -7144,7 +7153,7 @@
"dependencies": {
"inherits": {
"version": "2.0.3",
- "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/inherits/-/inherits-2.0.3.tgz",
"integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4="
}
}
@@ -13384,12 +13393,19 @@
}
},
"proxy-addr": {
- "version": "2.0.5",
- "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.5.tgz",
- "integrity": "sha512-t/7RxHXPH6cJtP0pRG6smSr9QJidhB+3kXu0KgXnbGYMgzEnUxRQ4/LDdfOwZEMyIh3/xHb8PX3t+lfL9z+YVQ==",
+ "version": "2.0.6",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/proxy-addr/-/proxy-addr-2.0.6.tgz",
+ "integrity": "sha1-/cIzZQVEfT8vLGOO0nLK9hS7sr8=",
"requires": {
"forwarded": "~0.1.2",
- "ipaddr.js": "1.9.0"
+ "ipaddr.js": "1.9.1"
+ },
+ "dependencies": {
+ "ipaddr.js": {
+ "version": "1.9.1",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
+ "integrity": "sha1-v/OFQ+64mEglB5/zoqjmy9RngbM="
+ }
}
},
"prr": {
@@ -13534,8 +13550,8 @@
},
"raw-body": {
"version": "2.4.0",
- "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.4.0.tgz",
- "integrity": "sha512-4Oz8DUIwdvoa5qMJelxipzi/iJIi40O5cGV1wNYp5hvZP8ZN0T+jiNkL0QepXs+EsQ9XJ8ipEDoiH70ySUJP3Q==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/raw-body/-/raw-body-2.4.0.tgz",
+ "integrity": "sha1-oc5vucm8NWylLoklarWQWeE9AzI=",
"requires": {
"bytes": "3.1.0",
"http-errors": "1.7.2",
@@ -13545,8 +13561,8 @@
"dependencies": {
"bytes": {
"version": "3.1.0",
- "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
- "integrity": "sha512-zauLjrfCG+xvoyaqLoV8bLVXXNGC4JqlxFCutSDWA6fJrTo2ZuvLYTqZ7aHBLZSMOopbzwv8f+wZcVzfVTI2Dg=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/bytes/-/bytes-3.1.0.tgz",
+ "integrity": "sha1-9s95M6Ng4FiPqf3oVlHNx/gF0fY="
}
}
},
@@ -14910,8 +14926,8 @@
},
"send": {
"version": "0.17.1",
- "resolved": "https://registry.npmjs.org/send/-/send-0.17.1.tgz",
- "integrity": "sha512-BsVKsiGcQMFwT8UxypobUKyv7irCNRHk1T0G680vk88yf6LBByGcZJOTJCrTP2xVN6yI+XjPJcNuE3V4fT9sAg==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/send/-/send-0.17.1.tgz",
+ "integrity": "sha1-wdiwWfeQD3Rm3Uk4vcROEd2zdsg=",
"requires": {
"debug": "2.6.9",
"depd": "~1.1.2",
@@ -14930,28 +14946,28 @@
"dependencies": {
"debug": {
"version": "2.6.9",
- "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
- "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/debug/-/debug-2.6.9.tgz",
+ "integrity": "sha1-XRKFFd8TT/Mn6QpMk/Tgd6U2NB8=",
"requires": {
"ms": "2.0.0"
},
"dependencies": {
"ms": {
"version": "2.0.0",
- "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/ms/-/ms-2.0.0.tgz",
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
}
}
},
"mime": {
"version": "1.6.0",
- "resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
- "integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/mime/-/mime-1.6.0.tgz",
+ "integrity": "sha1-Ms2eXGRVO9WNGaVor0Uqz/BJgbE="
},
"ms": {
"version": "2.1.1",
- "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz",
- "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/ms/-/ms-2.1.1.tgz",
+ "integrity": "sha1-MKWGTrPrsKZvLr5tcnrwagnYbgo="
}
}
},
@@ -15012,8 +15028,8 @@
},
"serve-static": {
"version": "1.14.1",
- "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz",
- "integrity": "sha512-JMrvUwE54emCYWlTI+hGrGv5I8dEwmco/00EvkzIIsR7MqrHonbD9pO2MOfFnpFntl7ecpZs+3mW+XbQZu9QCg==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/serve-static/-/serve-static-1.14.1.tgz",
+ "integrity": "sha1-Zm5jbcTwEPfvKZcKiKZ0MgiYsvk=",
"requires": {
"encodeurl": "~1.0.2",
"escape-html": "~1.0.3",
@@ -15054,8 +15070,8 @@
},
"setprototypeof": {
"version": "1.1.1",
- "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
- "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/setprototypeof/-/setprototypeof-1.1.1.tgz",
+ "integrity": "sha1-fpWsskqpL1iF4KvvW6ExMw1K5oM="
},
"sha.js": {
"version": "2.4.11",
@@ -16032,8 +16048,8 @@
},
"toidentifier": {
"version": "1.0.0",
- "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz",
- "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw=="
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/toidentifier/-/toidentifier-1.0.0.tgz",
+ "integrity": "sha1-fhvjRw8ed5SLxD2Uo8j013UrpVM="
},
"topo": {
"version": "2.0.2",
@@ -16104,8 +16120,8 @@
},
"type-is": {
"version": "1.6.18",
- "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
- "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
+ "resolved": "https://art-bobcat.autodesk.com:443/artifactory/api/npm/autodesk-npm-virtual/type-is/-/type-is-1.6.18.tgz",
+ "integrity": "sha1-TlUs0F3wlGfcvE73Od6J8s83wTE=",
"requires": {
"media-typer": "0.3.0",
"mime-types": "~2.1.24"
diff --git a/package.json b/package.json
index 181ace35..5b3aa6ba 100644
--- a/package.json
+++ b/package.json
@@ -3,6 +3,8 @@
"version": "0.1.0",
"private": true,
"dependencies": {
+ "cors": "^2.8.5",
+ "express": "^4.17.1",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-scripts": "2.1.1"
@@ -11,7 +13,8 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
- "eject": "react-scripts eject"
+ "eject": "react-scripts eject",
+ "server": "node ./src/server.js"
},
"eslintConfig": {
"extends": "react-app"
diff --git a/src/DrawingApp.js b/src/DrawingApp.js
index 40bb3a70..d7aae0a4 100644
--- a/src/DrawingApp.js
+++ b/src/DrawingApp.js
@@ -1,49 +1,46 @@
import React from "react";
-import DrawingServer from "./DrawingServer";
+import ServerInterface from "./ServerInterface";
import generatePathSegments from "./generate-paths";
import "./App.css";
+const refreshInterval = 1000;
+
export default class DrawingApp extends React.Component {
constructor(props) {
super(props);
+ this.intervalId = null;
+
this.state = {
dragging: false,
points: [],
confirmedPoints: [],
justStarted: false,
- server: new DrawingServer(),
+ serverInterface: new ServerInterface(),
refreshInterval: null,
};
-
- this.onMouseDown = this.onMouseDown.bind(this);
- this.onMouseUp = this.onMouseUp.bind(this);
- this.onMouseMove = this.onMouseMove.bind(this);
- this.reset = this.reset.bind(this);
- this.refresh = this.refresh.bind(this);
}
componentDidMount() {
- this.setState({
- refreshInterval: window.setInterval(this.refresh, 33),
- });
+ this.intervalId = window.setInterval(this.refresh, refreshInterval);
}
+
componentWillUnmount() {
- if (this.state.refreshInterval) {
- window.clearInterval(this.state.refreshInterval);
+ if (this.intervalId) {
+ window.clearInterval(this.intervalId);
}
}
- onMouseDown(ev) {
+ onMouseDown = (ev) => {
this.setState({ dragging: true, justStarted: true });
- }
+ };
- onMouseUp(ev) {
+ onMouseUp = (ev) => {
this.setState({ dragging: false });
- }
+ };
- onMouseMove(ev) {
+ onMouseMove = (ev) => {
if (this.state.dragging) {
// We will build an array of exactly one or two points to add to our overall line data
const newPoints = [];
@@ -75,33 +72,33 @@ export default class DrawingApp extends React.Component {
justStarted: false,
});
- // And send to the "remote" server
- this.state.server
+ // And send to the space station
+ this.state.serverInterface
.addPoints(newPoints)
- .then(response => {
+ .then((response) => {
// Success
})
- .catch(error => {
+ .catch((error) => {
// Failure
});
}
- }
+ };
- reset() {
+ reset = () => {
this.setState({ points: [] });
this.state.server.reset();
- }
+ };
- refresh() {
- this.state.server
+ refresh = () => {
+ this.state.serverInterface
.getPoints()
- .then(response => {
+ .then((response) => {
this.setState({ confirmedPoints: response });
})
- .catch(error => {
+ .catch((error) => {
console.warn(error);
});
- }
+ };
render() {
return (
diff --git a/src/DrawingServer.js b/src/DrawingServer.js
deleted file mode 100644
index ae6aff90..00000000
--- a/src/DrawingServer.js
+++ /dev/null
@@ -1,57 +0,0 @@
-// --------------------------------------------------------------------------
-// Simualted remote server - Do not edit!
-// --------------------------------------------------------------------------
-
-export default class DrawingServer {
- constructor() {
- this._points = [];
- this.ERROR_RATE = 0.0;
- this.MIN_LATENCY = 100;
- this.MAX_LATENCY = 100;
- }
-
- addPoints(points) {
- return this.withLatency(() => {
- // Clone points
- const newPoints = [];
- points.forEach(p => {
- newPoints.push(Object.assign({}, p));
- });
-
- // And append to internal points array
- this._points = this._points.concat(newPoints);
-
- // Respond with a copy of the newly added object
- return this.withLatency(() => newPoints.map(p => Object.assign({}, p)));
- });
- }
-
- getPoints(point) {
- return this.withLatency(() => {
- const snapshot = this._points.map(p => Object.assign({}, p));
- return this.withLatency(() => snapshot);
- });
- }
-
- reset() {
- return new Promise(resolve => {
- this._points = [];
- resolve();
- });
- }
-
- withLatency(cb) {
- const success = Math.random() < 1 - this.ERROR_RATE;
- const latency =
- Math.random() * (this.MAX_LATENCY - this.MIN_LATENCY) + this.MIN_LATENCY;
- return new Promise((resolve, reject) =>
- window.setTimeout(() => {
- if (success) {
- resolve(cb());
- } else {
- reject("Network error.");
- }
- }, latency)
- );
- }
-}
diff --git a/src/ServerInterface.js b/src/ServerInterface.js
new file mode 100644
index 00000000..e1f7658f
--- /dev/null
+++ b/src/ServerInterface.js
@@ -0,0 +1,21 @@
+export default class ServerInterface {
+ constructor() {}
+
+ addPoints(points) {
+ return fetch("http://localhost:1337/points", {
+ method: "POST",
+ headers: [["Content-Type", "application/json"]],
+ body: JSON.stringify(points),
+ }).then((res) => res.json());
+ }
+
+ getPoints() {
+ return fetch("http://localhost:1337/points").then((res) => res.json());
+ }
+
+ reset() {
+ return fetch("http://localhost:1337/points", { method: "DELETE" }).then(
+ (res) => res.json()
+ );
+ }
+}
diff --git a/src/generate-paths.js b/src/generate-paths.js
index 67a347b3..b0e3d4cd 100644
--- a/src/generate-paths.js
+++ b/src/generate-paths.js
@@ -4,10 +4,27 @@ import React from "react";
// Path drawing code
// --------------------------------------------------------------------------
+/**
+ *
+ * @param points A flat array of points, representing multiple segments
+ * For example:
+ * ```
+ * [
+ * { x: 1, y: 2, isEndOfSegment: false }
+ * { x: 1, y: 3, isEndOfSegment: false }
+ * { x: null, y: null, isEndOfSegment: true }
+ * { x: 2, y: 2, isEndOfSegment: false }
+ * { x: 2, y: 3, isEndOfSegment: false }
+ * { x: null, y: null, isEndOfSegment: true }
+ * ]
+ * ```
+ * @param key Unique key used as the React key prop
+ * @returns Array of React nodes
+ */
export default function generatePathSegments(points, key = "segment") {
const segments = [];
let currentSegment = [];
- points.forEach(p => {
+ points.forEach((p) => {
currentSegment.push(p);
if (p.isEndOfSegment) {
segments.push(
diff --git a/src/index.js b/src/index.js
index 3bb71c4a..12e0115e 100644
--- a/src/index.js
+++ b/src/index.js
@@ -2,11 +2,5 @@ import React from "react";
import ReactDOM from "react-dom";
import "./index.css";
import DrawingApp from "./DrawingApp";
-import * as serviceWorker from "./serviceWorker";
ReactDOM.render(, document.getElementById("root"));
-
-// If you want your app to work offline and load faster, you can change
-// unregister() to register() below. Note this comes with some pitfalls.
-// Learn more about service workers: http://bit.ly/CRA-PWA
-serviceWorker.unregister();
diff --git a/src/server.js b/src/server.js
new file mode 100644
index 00000000..60bca1eb
--- /dev/null
+++ b/src/server.js
@@ -0,0 +1,81 @@
+// This is the server code that lives on the space ship. You can inspect it,
+// but don't modify it (not even to include console.log statements).
+
+const express = require("express");
+const cors = require("cors");
+const app = express();
+const port = 1337;
+
+app.use(express.json());
+app.use(cors());
+app.options("*", cors());
+
+async function delay(milliseconds) {
+ return new Promise((resolve) => setTimeout(resolve, milliseconds));
+}
+
+function randomLatency() {
+ const minLatency = 50;
+ const maxLatency = 500;
+ return Math.round(Math.random() * (maxLatency - minLatency) + minLatency);
+}
+
+let points = [];
+
+async function addPoints(newPoints) {
+ await delay(randomLatency());
+ if (!Array.isArray(newPoints)) {
+ throw new Error("new points must be an array");
+ }
+ points = points.concat(newPoints);
+ await delay(randomLatency());
+ return newPoints;
+}
+
+async function getPoints() {
+ const snapshot = [...points];
+ await delay(randomLatency());
+ return snapshot;
+}
+
+async function deletePoints() {
+ await delay(randomLatency());
+ points = [];
+}
+
+app.post("/points", (req, res) => {
+ addPoints(req.body)
+ .then((result) => {
+ res.send(result);
+ })
+ .catch((err) => {
+ res.status(400);
+ res.send({ message: err.message });
+ });
+});
+
+app.get("/points", (_req, res) => {
+ getPoints()
+ .then((result) => {
+ res.send(result);
+ })
+ .catch((err) => {
+ res.status(400);
+ res.send({ message: err.message });
+ });
+});
+
+app.delete("/points", (_req, res) => {
+ deletePoints()
+ .then(() => {
+ res.send([]);
+ })
+ .catch((err) => {
+ res.status(500);
+ res.send({ message: err.message });
+ });
+});
+
+app.listen(port, () => {
+ console.log(`Space station listening at http://localhost:${port}`);
+});
diff --git a/src/serviceWorker.js b/src/serviceWorker.js
deleted file mode 100644
index 2283ff9c..00000000
--- a/src/serviceWorker.js
+++ /dev/null
@@ -1,135 +0,0 @@
-// This optional code is used to register a service worker.
-// register() is not called by default.
-
-// This lets the app load faster on subsequent visits in production, and gives
-// it offline capabilities. However, it also means that developers (and users)
-// will only see deployed updates on subsequent visits to a page, after all the
-// existing tabs open on the page have been closed, since previously cached
-// resources are updated in the background.
-
-// To learn more about the benefits of this model and instructions on how to
-// opt-in, read http://bit.ly/CRA-PWA
-
-const isLocalhost = Boolean(
- window.location.hostname === 'localhost' ||
- // [::1] is the IPv6 localhost address.
- window.location.hostname === '[::1]' ||
- // 127.0.0.1/8 is considered localhost for IPv4.
- window.location.hostname.match(
- /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
- )
-);
-
-export function register(config) {
- if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
- // The URL constructor is available in all browsers that support SW.
- const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
- if (publicUrl.origin !== window.location.origin) {
- // Our service worker won't work if PUBLIC_URL is on a different origin
- // from what our page is served on. This might happen if a CDN is used to
- // serve assets; see https://github.com/facebook/create-react-app/issues/2374
- return;
- }
-
- window.addEventListener('load', () => {
- const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
-
- if (isLocalhost) {
- // This is running on localhost. Let's check if a service worker still exists or not.
- checkValidServiceWorker(swUrl, config);
-
- // Add some additional logging to localhost, pointing developers to the
- // service worker/PWA documentation.
- navigator.serviceWorker.ready.then(() => {
- console.log(
- 'This web app is being served cache-first by a service ' +
- 'worker. To learn more, visit http://bit.ly/CRA-PWA'
- );
- });
- } else {
- // Is not localhost. Just register service worker
- registerValidSW(swUrl, config);
- }
- });
- }
-}
-
-function registerValidSW(swUrl, config) {
- navigator.serviceWorker
- .register(swUrl)
- .then(registration => {
- registration.onupdatefound = () => {
- const installingWorker = registration.installing;
- if (installingWorker == null) {
- return;
- }
- installingWorker.onstatechange = () => {
- if (installingWorker.state === 'installed') {
- if (navigator.serviceWorker.controller) {
- // At this point, the updated precached content has been fetched,
- // but the previous service worker will still serve the older
- // content until all client tabs are closed.
- console.log(
- 'New content is available and will be used when all ' +
- 'tabs for this page are closed. See http://bit.ly/CRA-PWA.'
- );
-
- // Execute callback
- if (config && config.onUpdate) {
- config.onUpdate(registration);
- }
- } else {
- // At this point, everything has been precached.
- // It's the perfect time to display a
- // "Content is cached for offline use." message.
- console.log('Content is cached for offline use.');
-
- // Execute callback
- if (config && config.onSuccess) {
- config.onSuccess(registration);
- }
- }
- }
- };
- };
- })
- .catch(error => {
- console.error('Error during service worker registration:', error);
- });
-}
-
-function checkValidServiceWorker(swUrl, config) {
- // Check if the service worker can be found. If it can't reload the page.
- fetch(swUrl)
- .then(response => {
- // Ensure service worker exists, and that we really are getting a JS file.
- const contentType = response.headers.get('content-type');
- if (
- response.status === 404 ||
- (contentType != null && contentType.indexOf('javascript') === -1)
- ) {
- // No service worker found. Probably a different app. Reload the page.
- navigator.serviceWorker.ready.then(registration => {
- registration.unregister().then(() => {
- window.location.reload();
- });
- });
- } else {
- // Service worker found. Proceed as normal.
- registerValidSW(swUrl, config);
- }
- })
- .catch(() => {
- console.log(
- 'No internet connection found. App is running in offline mode.'
- );
- });
-}
-
-export function unregister() {
- if ('serviceWorker' in navigator) {
- navigator.serviceWorker.ready.then(registration => {
- registration.unregister();
- });
- }
-}