├── .github
└── workflows
│ ├── config.yml
│ └── deploy.sh
├── .gitignore
├── .gitignore_cicd
├── client
├── README.md
├── build
│ ├── asset-manifest.json
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ ├── precache-manifest.0699d5c9698c486c05b4904bd5856d75.js
│ ├── robots.txt
│ ├── service-worker.js
│ └── static
│ │ ├── css
│ │ ├── main.cf6385ff.chunk.css
│ │ └── main.cf6385ff.chunk.css.map
│ │ └── js
│ │ ├── 2.3c07f4e1.chunk.js
│ │ ├── 2.3c07f4e1.chunk.js.LICENSE.txt
│ │ ├── 2.3c07f4e1.chunk.js.map
│ │ ├── main.bb3a1453.chunk.js
│ │ ├── main.bb3a1453.chunk.js.map
│ │ ├── runtime-main.09b85ec0.js
│ │ └── runtime-main.09b85ec0.js.map
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
├── src
│ ├── components
│ │ ├── Chat
│ │ │ └── index.tsx
│ │ ├── Home
│ │ │ ├── index.tsx
│ │ │ └── style.css
│ │ ├── Login
│ │ │ ├── index.tsx
│ │ │ └── style.css
│ │ ├── Register
│ │ │ ├── index.tsx
│ │ │ └── style.css
│ │ └── index.tsx
│ ├── index.css
│ ├── index.tsx
│ ├── react-app-env.d.ts
│ └── utility.ts
├── tsconfig.json
└── yarn.lock
└── server
├── build
├── models
│ ├── messages.js
│ └── user.js
├── server.js
├── utilities.js
├── websocket.js
└── wsfunc.js
├── models
├── messages.ts
└── user.ts
├── package.json
├── server.ts
├── tsconfig.json
├── utilities.ts
├── websocket.ts
├── wsfunc.ts
└── yarn.lock
/.github/workflows/config.yml:
--------------------------------------------------------------------------------
1 | name: CI deploy
2 | on:
3 | push:
4 | branches: [master]
5 | env:
6 | AWS_HOST: 52.90.10.161
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 | steps:
11 | - uses: actions/checkout@v2
12 | - uses: actions/setup-node@v1
13 | with:
14 | node-version: '14.8.0'
15 | - name: Build Server
16 | env:
17 | NODE_ENV: development
18 | run: |
19 | cd server
20 | yarn install --frozen-lockfile
21 | tsc
22 | - name: Build Client
23 | env:
24 | NODE_ENV: development
25 | run: |
26 | cd client
27 | yarn install --frozen-lockfile
28 | yarn build
29 | - name: Deploy on server
30 | env:
31 | DEPLOYMENT_KEY_B64: ${{ secrets.DEPLOYMENT_KEY_B64 }}
32 | run: |
33 | mkdir ~/.ssh;
34 | touch ~/.ssh/known_hosts;
35 | ssh-keyscan $AWS_HOST >> ~/.ssh/known_hosts;
36 | echo $DEPLOYMENT_KEY_B64 | base64 -d > ~/.ssh/id_rsa;
37 | chmod 644 ~/.ssh/known_hosts;
38 | chmod 600 ~/.ssh/id_rsa;
39 | chmod 0755 ./.github/workflows/deploy.sh;
40 | ./.github/workflows/deploy.sh;
41 | rm -rf ~/.ssh;
42 |
--------------------------------------------------------------------------------
/.github/workflows/deploy.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | set -e
3 | user="ubuntu"
4 |
5 | rm -rf .git
6 | rm -rf .gitignore
7 | git config --global user.email "mehulmpt@gmail.com"
8 | git config --global user.name "Mehul Mohan"
9 | mv .gitignore_cicd .gitignore
10 | git init .
11 | git add .
12 | git commit -m "Deploying"
13 | git remote add production ssh://$user@$AWS_HOST/~/webapp
14 | git push --force production master
15 |
16 | ssh $user@$AWS_HOST "cd ~/webapp && \
17 | pm2 kill
18 | NODE_ENV=production pm2 start /home/ubuntu/webapp/server/build/server.js
19 | exit"
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
--------------------------------------------------------------------------------
/.gitignore_cicd:
--------------------------------------------------------------------------------
1 | node_modules/
--------------------------------------------------------------------------------
/client/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `yarn start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `yarn test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `yarn build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `yarn eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | 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.
35 |
36 | 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.
37 |
38 | 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.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
--------------------------------------------------------------------------------
/client/build/asset-manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "files": {
3 | "main.css": "/static/css/main.cf6385ff.chunk.css",
4 | "main.js": "/static/js/main.bb3a1453.chunk.js",
5 | "main.js.map": "/static/js/main.bb3a1453.chunk.js.map",
6 | "runtime-main.js": "/static/js/runtime-main.09b85ec0.js",
7 | "runtime-main.js.map": "/static/js/runtime-main.09b85ec0.js.map",
8 | "static/js/2.3c07f4e1.chunk.js": "/static/js/2.3c07f4e1.chunk.js",
9 | "static/js/2.3c07f4e1.chunk.js.map": "/static/js/2.3c07f4e1.chunk.js.map",
10 | "index.html": "/index.html",
11 | "precache-manifest.0699d5c9698c486c05b4904bd5856d75.js": "/precache-manifest.0699d5c9698c486c05b4904bd5856d75.js",
12 | "service-worker.js": "/service-worker.js",
13 | "static/css/main.cf6385ff.chunk.css.map": "/static/css/main.cf6385ff.chunk.css.map",
14 | "static/js/2.3c07f4e1.chunk.js.LICENSE.txt": "/static/js/2.3c07f4e1.chunk.js.LICENSE.txt"
15 | },
16 | "entrypoints": [
17 | "static/js/runtime-main.09b85ec0.js",
18 | "static/js/2.3c07f4e1.chunk.js",
19 | "static/css/main.cf6385ff.chunk.css",
20 | "static/js/main.bb3a1453.chunk.js"
21 | ]
22 | }
--------------------------------------------------------------------------------
/client/build/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mehulmpt/codedamn-live-project/d4aae7e430d76882c9b01faee1acb90396b47155/client/build/favicon.ico
--------------------------------------------------------------------------------
/client/build/index.html:
--------------------------------------------------------------------------------
1 |
React App
--------------------------------------------------------------------------------
/client/build/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mehulmpt/codedamn-live-project/d4aae7e430d76882c9b01faee1acb90396b47155/client/build/logo192.png
--------------------------------------------------------------------------------
/client/build/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mehulmpt/codedamn-live-project/d4aae7e430d76882c9b01faee1acb90396b47155/client/build/logo512.png
--------------------------------------------------------------------------------
/client/build/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/client/build/precache-manifest.0699d5c9698c486c05b4904bd5856d75.js:
--------------------------------------------------------------------------------
1 | self.__precacheManifest = (self.__precacheManifest || []).concat([
2 | {
3 | "revision": "b5484e7ecb41e88e5b6a93f2d6e2dfbb",
4 | "url": "/index.html"
5 | },
6 | {
7 | "revision": "3663d118ecb55f962aae",
8 | "url": "/static/css/main.cf6385ff.chunk.css"
9 | },
10 | {
11 | "revision": "318428ceeb62820617d2",
12 | "url": "/static/js/2.3c07f4e1.chunk.js"
13 | },
14 | {
15 | "revision": "0749163b59fbee32225059cb60c18af6",
16 | "url": "/static/js/2.3c07f4e1.chunk.js.LICENSE.txt"
17 | },
18 | {
19 | "revision": "3663d118ecb55f962aae",
20 | "url": "/static/js/main.bb3a1453.chunk.js"
21 | },
22 | {
23 | "revision": "9b4e396c183e42c1fa5c",
24 | "url": "/static/js/runtime-main.09b85ec0.js"
25 | }
26 | ]);
--------------------------------------------------------------------------------
/client/build/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/client/build/service-worker.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Welcome to your Workbox-powered service worker!
3 | *
4 | * You'll need to register this file in your web app and you should
5 | * disable HTTP caching for this file too.
6 | * See https://goo.gl/nhQhGp
7 | *
8 | * The rest of the code is auto-generated. Please don't update this file
9 | * directly; instead, make changes to your Workbox build configuration
10 | * and re-run your build process.
11 | * See https://goo.gl/2aRDsh
12 | */
13 |
14 | importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js");
15 |
16 | importScripts(
17 | "/precache-manifest.0699d5c9698c486c05b4904bd5856d75.js"
18 | );
19 |
20 | self.addEventListener('message', (event) => {
21 | if (event.data && event.data.type === 'SKIP_WAITING') {
22 | self.skipWaiting();
23 | }
24 | });
25 |
26 | workbox.core.clientsClaim();
27 |
28 | /**
29 | * The workboxSW.precacheAndRoute() method efficiently caches and responds to
30 | * requests for URLs in the manifest.
31 | * See https://goo.gl/S9QRab
32 | */
33 | self.__precacheManifest = [].concat(self.__precacheManifest || []);
34 | workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
35 |
36 | workbox.routing.registerNavigationRoute(workbox.precaching.getCacheKeyForURL("/index.html"), {
37 |
38 | blacklist: [/^\/_/,/\/[^\/?]+\.[^\/]+$/],
39 | });
40 |
--------------------------------------------------------------------------------
/client/build/static/css/main.cf6385ff.chunk.css:
--------------------------------------------------------------------------------
1 | body{margin:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Roboto","Oxygen","Ubuntu","Cantarell","Fira Sans","Droid Sans","Helvetica Neue",sans-serif;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}code{font-family:source-code-pro,Menlo,Monaco,Consolas,"Courier New",monospace}.App{display:flex;height:100vh;width:100vw;justify-content:center;align-items:center;text-align:center}.buttons{width:480px;display:flex;justify-content:space-between}.form{height:100vh;width:100vw;justify-content:center;align-items:center;text-align:center}.form,.register-fields{display:flex;flex-direction:column}.register-fields{width:480px}.register-fields>div{margin-bottom:20px}
2 | /*# sourceMappingURL=main.cf6385ff.chunk.css.map */
--------------------------------------------------------------------------------
/client/build/static/css/main.cf6385ff.chunk.css.map:
--------------------------------------------------------------------------------
1 | {"version":3,"sources":["index.css","style.css"],"names":[],"mappings":"AAAA,KACE,QAAS,CACT,mJAEY,CACZ,kCAAmC,CACnC,iCACF,CAEA,KACE,yEAEF,CCZA,KACC,YAAa,CACb,YAAa,CACb,WAAY,CACZ,sBAAuB,CACvB,kBAAmB,CACnB,iBACD,CAEA,SACC,WAAY,CACZ,YAAa,CACb,6BACD,CAbA,MAEC,YAAa,CACb,WAAY,CACZ,sBAAuB,CACvB,kBAAmB,CACnB,iBAED,CAEA,uBATC,YAAa,CAMb,qBAOD,CAJA,iBAEC,WAED,CAEA,qBACC,kBACD","file":"main.cf6385ff.chunk.css","sourcesContent":["body {\n margin: 0;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',\n 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',\n sans-serif;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n}\n\ncode {\n font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',\n monospace;\n}\n",".form {\n\tdisplay: flex;\n\theight: 100vh;\n\twidth: 100vw;\n\tjustify-content: center;\n\talign-items: center;\n\ttext-align: center;\n\tflex-direction: column;\n}\n\n.register-fields {\n\tdisplay: flex;\n\twidth: 480px;\n\tflex-direction: column;\n}\n\n.register-fields > div {\n\tmargin-bottom: 20px;\n}\n"]}
--------------------------------------------------------------------------------
/client/build/static/js/2.3c07f4e1.chunk.js.LICENSE.txt:
--------------------------------------------------------------------------------
1 | /*
2 | object-assign
3 | (c) Sindre Sorhus
4 | @license MIT
5 | */
6 |
7 | /**
8 | * A better abstraction over CSS.
9 | *
10 | * @copyright Oleg Isonen (Slobodskoi) / Isonen 2014-present
11 | * @website https://github.com/cssinjs/jss
12 | * @license MIT
13 | */
14 |
15 | /** @license React v0.19.1
16 | * scheduler.production.min.js
17 | *
18 | * Copyright (c) Facebook, Inc. and its affiliates.
19 | *
20 | * This source code is licensed under the MIT license found in the
21 | * LICENSE file in the root directory of this source tree.
22 | */
23 |
24 | /** @license React v16.13.1
25 | * react-dom.production.min.js
26 | *
27 | * Copyright (c) Facebook, Inc. and its affiliates.
28 | *
29 | * This source code is licensed under the MIT license found in the
30 | * LICENSE file in the root directory of this source tree.
31 | */
32 |
33 | /** @license React v16.13.1
34 | * react-is.production.min.js
35 | *
36 | * Copyright (c) Facebook, Inc. and its affiliates.
37 | *
38 | * This source code is licensed under the MIT license found in the
39 | * LICENSE file in the root directory of this source tree.
40 | */
41 |
42 | /** @license React v16.13.1
43 | * react.production.min.js
44 | *
45 | * Copyright (c) Facebook, Inc. and its affiliates.
46 | *
47 | * This source code is licensed under the MIT license found in the
48 | * LICENSE file in the root directory of this source tree.
49 | */
50 |
--------------------------------------------------------------------------------
/client/build/static/js/main.bb3a1453.chunk.js:
--------------------------------------------------------------------------------
1 | (this.webpackJsonpclient=this.webpackJsonpclient||[]).push([[0],{60:function(e,t,a){e.exports=a(75)},65:function(e,t,a){},66:function(e,t,a){},73:function(e,t,a){},74:function(e,t,a){},75:function(e,t,a){"use strict";a.r(t);var n=a(0),r=a.n(n),c=a(7),o=a.n(c),l=(a(65),a(24)),i=a(10),s=a(102);a(66);function u(){return r.a.createElement("div",{className:"App"},r.a.createElement("header",{className:"App-header"},r.a.createElement("h1",null,"Simple Chat Room App"),r.a.createElement("div",{className:"buttons"},r.a.createElement(s.a,{color:"secondary",variant:"contained",component:l.b,to:"/login"},"Login"),r.a.createElement(s.a,{color:"primary",variant:"contained",component:l.b,to:"/register"},"Register"))))}var m=a(20),p=a.n(m),d=a(26),f=a(18),h=a(109),g=(a(73),!("localhost"===window.location.hostname)?"":"http://localhost:1337");function v(e,t){return E.apply(this,arguments)}function E(){return(E=Object(d.a)(p.a.mark((function e(t,a){var n;return p.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fetch("".concat(g).concat(t),{method:"POST",headers:{"Content-Type":"application/json","x-access-token":localStorage.getItem("token")||""},body:JSON.stringify(a)}).then((function(e){return e.json()}));case 2:return n=e.sent,e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function b(){var e=Object(n.useState)(""),t=Object(f.a)(e,2),a=t[0],c=t[1],o=Object(n.useState)(""),l=Object(f.a)(o,2),u=l[0],m=l[1],g=Object(i.f)();function E(){return(E=Object(d.a)(p.a.mark((function e(){var t;return p.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,v("/api/login",{email:a,password:u});case 2:"ok"===(t=e.sent).status?(localStorage.setItem("token",t.data),alert("You are logged in"),g.push("/chat")):alert(t.error);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}return r.a.createElement("div",{className:"form"},r.a.createElement("h1",null,"Login"),r.a.createElement("form",{className:"register-fields"},r.a.createElement(h.a,{fullWidth:!0,placeholder:"you@awesome.com",label:"Your Email",value:a,onChange:function(e){return c(e.target.value)},variant:"outlined"}),r.a.createElement(h.a,{fullWidth:!0,value:u,onChange:function(e){return m(e.target.value)},placeholder:"p@$$w0rd",label:"Password",variant:"outlined"}),r.a.createElement(s.a,{color:"primary",variant:"contained",onClick:function(){return E.apply(this,arguments)}},"Login")))}a(74);function y(){var e=Object(n.useState)(""),t=Object(f.a)(e,2),a=t[0],c=t[1],o=Object(n.useState)(""),l=Object(f.a)(o,2),i=l[0],u=l[1];function m(){return(m=Object(d.a)(p.a.mark((function e(){var t;return p.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,v("/api/register",{email:a,password:i});case 2:t=e.sent,console.log(t);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}return r.a.createElement("div",{className:"form"},r.a.createElement("h1",null,"Register"),r.a.createElement("form",{className:"register-fields"},r.a.createElement(h.a,{fullWidth:!0,placeholder:"you@awesome.com",label:"Your Email",value:a,onChange:function(e){return c(e.target.value)},variant:"outlined"}),r.a.createElement(h.a,{fullWidth:!0,value:i,onChange:function(e){return u(e.target.value)},placeholder:"p@$$w0rd",label:"Password",variant:"outlined"}),r.a.createElement(s.a,{color:"primary",variant:"contained",onClick:function(){return m.apply(this,arguments)}},"Register")))}var O=a(52),j=a(106),S=a(107),w=a(110),k=a(108),x=a(41);function N(){var e=Object(n.useState)(""),t=Object(f.a)(e,2),a=t[0],c=t[1],o=Object(n.useState)([]),l=Object(f.a)(o,2),u=l[0],m=l[1],p=Object(n.useState)(null),d=Object(f.a)(p,2),g=d[0],v=d[1],E=Object(i.f)();return Object(n.useEffect)((function(){var e=new WebSocket("ws://localhost:1338/"+localStorage.getItem("token"));return e.addEventListener("open",(function(){e.send(JSON.stringify({intent:"old-messages",count:10}))}),{once:!0}),e.addEventListener("error",(function(){alert("Please login first"),E.replace("/login")})),e.addEventListener("message",(function(e){var t=function(e){try{return JSON.parse(e)}catch(t){return null}}(e.data);t&&("chat"===t.intent?m((function(e){return[].concat(Object(O.a)(e),[t])})):"old-messages"===t.intent&&(console.log(t.data,"are the older messages"),m(t.data.map((function(e){return{user:e.email,message:e.message}})).reverse())))})),v(e),function(){e.close()}}),[]),r.a.createElement("div",null,r.a.createElement("h1",null,"Chat Page"),r.a.createElement("div",{className:"chat-box"},u.map((function(e,t){return r.a.createElement(j.a,{alignItems:"flex-start",key:t},r.a.createElement(S.a,null,r.a.createElement(w.a,{alt:"Remy Sharp",src:"/static/images/avatar/1.jpg"})),r.a.createElement(k.a,{primary:e.user,secondary:r.a.createElement(r.a.Fragment,null,r.a.createElement(x.a,{component:"span",variant:"body2",color:"textPrimary"},e.message))}))}))),r.a.createElement(h.a,{onChange:function(e){return c(e.target.value)},value:a,multiline:!0,rows:2,variant:"outlined",color:"primary"}),r.a.createElement(s.a,{variant:"outlined",color:"primary",onClick:function(){(null===g||void 0===g?void 0:g.readyState)===WebSocket.OPEN&&(g.send(JSON.stringify({message:a,intent:"chat"})),c(""))}},"Send Message"))}var C=function(){return r.a.createElement(l.a,null,r.a.createElement(i.c,null,r.a.createElement(i.a,{path:"/",component:u,exact:!0}),r.a.createElement(i.a,{path:"/login",component:b,exact:!0}),r.a.createElement(i.a,{path:"/register",component:y,exact:!0}),r.a.createElement(i.a,{path:"/chat",component:N,exact:!0})))};o.a.render(r.a.createElement(r.a.StrictMode,null,r.a.createElement(C,null)),document.getElementById("root"))}},[[60,1,2]]]);
2 | //# sourceMappingURL=main.bb3a1453.chunk.js.map
--------------------------------------------------------------------------------
/client/build/static/js/main.bb3a1453.chunk.js.map:
--------------------------------------------------------------------------------
1 | {"version":3,"sources":["components/Home/index.tsx","utility.ts","components/Login/index.tsx","components/Register/index.tsx","components/Chat/index.tsx","components/index.tsx","index.tsx"],"names":["Home","className","Button","color","variant","component","Link","to","API_URL","window","location","hostname","apiCall","path","payload","a","fetch","method","headers","localStorage","getItem","body","JSON","stringify","then","t","json","res","Register","useState","email","setEmail","password","setPassword","history","useHistory","status","setItem","data","alert","push","error","TextField","fullWidth","placeholder","label","value","onChange","e","target","onClick","console","log","Chat","chatMessage","setChatMessage","chatMessages","setChatMessages","wsRef","setWSRef","useEffect","ws","WebSocket","addEventListener","send","intent","count","once","replace","event","message","parse","processMessage","oldMessages","map","item","user","reverse","close","index","ListItem","alignItems","key","ListItemAvatar","Avatar","alt","src","ListItemText","primary","secondary","Fragment","Typography","multiline","rows","readyState","OPEN","App","exact","Login","ReactDOM","render","StrictMode","document","getElementById"],"mappings":"2SAKe,SAASA,IACvB,OACC,yBAAKC,UAAU,OACd,4BAAQA,UAAU,cACjB,oDACA,yBAAKA,UAAU,WACd,kBAACC,EAAA,EAAD,CAAQC,MAAM,YAAYC,QAAQ,YAAYC,UAAWC,IAAMC,GAAG,UAAlE,SAGA,kBAACL,EAAA,EAAD,CAAQC,MAAM,UAAUC,QAAQ,YAAYC,UAAWC,IAAMC,GAAG,aAAhE,e,8CCXCC,G,QAHqD,cAA7BC,OAAOC,SAASC,UAGd,GAAK,yBAE9B,SAAeC,EAAtB,oC,4CAAO,WAAuBC,EAAcC,GAArC,eAAAC,EAAA,sEACYC,MAAM,GAAD,OAAIR,GAAJ,OAAcK,GAAQ,CAC5CI,OAAQ,OACRC,QAAS,CACR,eAAgB,mBAChB,iBAAkBC,aAAaC,QAAQ,UAAY,IAEpDC,KAAMC,KAAKC,UAAUT,KACnBU,MAAK,SAACC,GAAD,OAAOA,EAAEC,UARX,cACAC,EADA,yBAUCA,GAVD,4C,sBCCQ,SAASC,IAAY,IAAD,EACRC,mBAAS,IADD,mBAC3BC,EAD2B,KACpBC,EADoB,OAEFF,mBAAS,IAFP,mBAE3BG,EAF2B,KAEjBC,EAFiB,KAI5BC,EAAUC,cAJkB,4CAMlC,4BAAApB,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,kBAAC,IAAMC,SAAP,KACC,kBAACC,EAAA,EAAD,CACCtF,UAAU,OACVD,QAAQ,QACRD,MAAM,eAELmE,EAAQA,iBAejB,kBAAC5B,EAAA,EAAD,CACCK,SAAU,SAACC,GAAD,OAAOO,EAAeP,EAAEC,OAAOH,QACzCA,MAAOQ,EACPsC,WAAS,EACTC,KAAM,EACNzF,QAAQ,WACRD,MAAM,YAGP,kBAACD,EAAA,EAAD,CAAQE,QAAQ,WAAWD,MAAM,UAAU+C,QAjH7C,YACU,OAALQ,QAAK,IAALA,OAAA,EAAAA,EAAOoC,cAAehC,UAAUiC,OAKpCrC,EAAMM,KAAK1C,KAAKC,UAAU,CAAE+C,QAAShB,EAAaW,OAAQ,UAC1DV,EAAe,OA0Gd,iBC9HYyC,MAbf,WACC,OACC,kBAAC,IAAD,KACC,kBAAC,IAAD,KACC,kBAAC,IAAD,CAAOnF,KAAK,IAAIR,UAAWL,EAAMiG,OAAK,IACtC,kBAAC,IAAD,CAAOpF,KAAK,SAASR,UAAW6F,EAAOD,OAAK,IAC5C,kBAAC,IAAD,CAAOpF,KAAK,YAAYR,UAAWuB,EAAUqE,OAAK,IAClD,kBAAC,IAAD,CAAOpF,KAAK,QAAQR,UAAWgD,EAAM4C,OAAK,OCT9CE,IAASC,OACR,kBAAC,IAAMC,WAAP,KACC,kBAAC,EAAD,OAEDC,SAASC,eAAe,W","file":"static/js/main.bb3a1453.chunk.js","sourcesContent":["import React from 'react'\nimport { Button } from '@material-ui/core'\nimport { Link } from 'react-router-dom'\nimport './style.css'\n\nexport default function Home() {\n\treturn (\n\t\t
\n\t\t\t\n\t\t\t\t
Simple Chat Room App
\n\t\t\t\t
\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t
\n\t\t\t\n\t\t
\n\t)\n}\n","export const IS_DEVELOPMENT = window.location.hostname === 'localhost'\nexport const IS_PRODUCTION = !IS_DEVELOPMENT\n\nconst API_URL = IS_PRODUCTION ? '' : 'http://localhost:1337'\n\nexport async function apiCall(path: string, payload: { [key: string]: any }) {\n\tconst res = await fetch(`${API_URL}${path}`, {\n\t\tmethod: 'POST',\n\t\theaders: {\n\t\t\t'Content-Type': 'application/json',\n\t\t\t'x-access-token': localStorage.getItem('token') || ''\n\t\t},\n\t\tbody: JSON.stringify(payload)\n\t}).then((t) => t.json())\n\n\treturn res\n}\n","import React, { useState } from 'react'\nimport { TextField, Button } from '@material-ui/core'\nimport './style.css'\nimport { apiCall } from '../../utility'\nimport { useHistory } from 'react-router-dom'\n\nexport default function Register() {\n\tconst [email, setEmail] = useState('')\n\tconst [password, setPassword] = useState('')\n\n\tconst history = useHistory()\n\n\tasync function loginUser() {\n\t\tconst res = await apiCall('/api/login', { email, password })\n\n\t\tif (res.status === 'ok') {\n\t\t\t// TODO: Bad practice -> refresh tokens\n\t\t\tlocalStorage.setItem('token', res.data)\n\t\t\talert('You are logged in')\n\t\t\thistory.push('/chat')\n\t\t} else {\n\t\t\talert(res.error)\n\t\t}\n\t}\n\n\treturn (\n\t\t
\n\t\t\t
Login
\n\t\t\t\n\t\t
\n\t)\n}\n","import React, { useState } from 'react'\nimport { TextField, Button } from '@material-ui/core'\nimport './style.css'\nimport { apiCall } from '../../utility'\n\nexport default function Register() {\n\tconst [email, setEmail] = useState('')\n\tconst [password, setPassword] = useState('')\n\n\tasync function registerUser() {\n\t\tconst res = await apiCall('/api/register', { email, password })\n\t\tconsole.log(res)\n\t}\n\n\treturn (\n\t\t
\n\t\t\t
Register
\n\t\t\t\n\t\t
\n\t)\n}\n","import React, { useEffect, useState } from 'react'\nimport {\n\tTextField,\n\tButton,\n\tListItem,\n\tListItemAvatar,\n\tAvatar,\n\tListItemText,\n\tTypography\n} from '@material-ui/core'\nimport { useHistory } from 'react-router-dom'\n\ntype Message = {\n\tuser: string\n\tmessage: string\n\tintent: 'chat'\n}\n\nfunction processMessage(payload: string) {\n\ttry {\n\t\treturn JSON.parse(payload)\n\t} catch (error) {\n\t\treturn null\n\t}\n}\n\nexport default function Chat() {\n\tconst [chatMessage, setChatMessage] = useState('')\n\tconst [chatMessages, setChatMessages] = useState([])\n\tconst [wsRef, setWSRef] = useState(null)\n\n\tconst history = useHistory()\n\n\tfunction sendMessage() {\n\t\tif (wsRef?.readyState !== WebSocket.OPEN) {\n\t\t\t// websocket not connected\n\t\t\treturn\n\t\t}\n\n\t\twsRef.send(JSON.stringify({ message: chatMessage, intent: 'chat' }))\n\t\tsetChatMessage('')\n\t}\n\n\tuseEffect(() => {\n\t\tconst ws = new WebSocket('ws://localhost:1338/' + localStorage.getItem('token'))\n\n\t\tws.addEventListener(\n\t\t\t'open',\n\t\t\t() => {\n\t\t\t\tws.send(\n\t\t\t\t\tJSON.stringify({\n\t\t\t\t\t\tintent: 'old-messages',\n\t\t\t\t\t\tcount: 10\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t},\n\t\t\t{ once: true }\n\t\t)\n\n\t\tws.addEventListener('error', () => {\n\t\t\talert('Please login first')\n\t\t\thistory.replace('/login')\n\t\t})\n\n\t\tws.addEventListener('message', (event) => {\n\t\t\tconst data = event.data\n\n\t\t\tconst message: any = processMessage(data)\n\n\t\t\tif (!message) return\n\n\t\t\tif (message.intent === 'chat') {\n\t\t\t\tsetChatMessages((oldMessages) => {\n\t\t\t\t\treturn [...oldMessages, message as Message]\n\t\t\t\t})\n\t\t\t} else if (message.intent === 'old-messages') {\n\t\t\t\tconsole.log(message.data, 'are the older messages')\n\t\t\t\tsetChatMessages(\n\t\t\t\t\tmessage.data\n\t\t\t\t\t\t.map((item: any) => {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tuser: item.email,\n\t\t\t\t\t\t\t\tmessage: item.message\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})\n\t\t\t\t\t\t.reverse()\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\n\t\tsetWSRef(ws)\n\n\t\treturn () => {\n\t\t\tws.close()\n\t\t}\n\t}, [])\n\n\t// TODO: Add another action which loads more messages\n\n\t// 1. another click handler for that button\n\t// 2. Should extract the smallest date of current messages\n\t// 3. Should send a new websocket request with old-messages but with date as the property // date as well as count\n\n\treturn (\n\t\t