Initialize project with a few classes to get started

This commit is contained in:
hippoz 2020-12-28 17:51:34 +02:00
parent 49b5c1f2a0
commit d3af43bc7c
Signed by: hippoz
GPG key ID: 7C52899193467641
15 changed files with 16823 additions and 0 deletions

23
bfrontend/.gitignore vendored Normal file
View file

@ -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*

70
bfrontend/README.md Normal file
View file

@ -0,0 +1,70 @@
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## 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 cant go back!**
If you arent 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 youre on your own.
You dont have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldnt feel obligated to use this feature. However we understand that this tool wouldnt be useful if you couldnt 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](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](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
### Making a Progressive Web App
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
### Advanced Configuration
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### Deployment
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
### `npm run build` fails to minify
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

16500
bfrontend/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

39
bfrontend/package.json Normal file
View file

@ -0,0 +1,39 @@
{
"name": "bfrontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

View file

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Brainlet</title>
</head>
<body>
<noscript>Sorry, but JavaScript is required to render and view this page.</noscript>
<div id="root"></div>
</body>
</html>

View file

@ -0,0 +1,8 @@
{
"short_name": "Brainlet",
"name": "Brainlet",
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

View file

@ -0,0 +1,2 @@
User-agent: *
Disallow: /

View file

@ -0,0 +1,59 @@
import config from './Config';
async function APIRequest(endpoint, options) {
let res;
let json;
let isOK = false;
let err;
try {
res = await fetch(`${config.apiUrl}${endpoint}`, options);
json = await res.json();
isOK = true;
} catch(e) {
err = e;
isOK = false;
}
if (isOK && res.ok && !json.error) {
isOK = true;
} else {
isOK = false;
}
return { res, json, isOK, err };
}
APIRequest.authenticated = async function(endpoint, options) {
let res;
let json;
let isOK = false;
let err;
if (!options) options = {};
if (!options.headers) options.headers = {};
options.headers = {
credentials: 'include',
...options.headers
};
try {
res = await fetch(`${config.apiUrl}${endpoint}`, options);
json = await res.json();
isOK = true;
} catch(e) {
err = e;
isOK = false;
}
if (isOK && res.ok && !json.error) {
isOK = true;
} else {
isOK = false;
}
return { res, json, isOK, err };
};
export default APIRequest;

View file

@ -0,0 +1,30 @@
import APIRequest from './APIRequest';
class Authenticator {
constructor(method='cookie') {
this.method = method;
this.__token = undefined;
this.user = undefined;
}
}
Authenticator.prototype.fetchLoggedInUserInfo = async function(token) {
const { json, isOK } = await APIRequest.authenticated();
if (!isOK) return false;
if (!json.user) return false;
this.user = json.user;
this.__token = json.user.token;
return true;
}
Authenticator.prototype.setToken = function(token) {
this.__token = token;
};
Authenticator.prototype.getToken = function(token) {
return this.__token;
};
const authenticator = new Authenticator('cookie');
export default authenticator;

View file

@ -0,0 +1,23 @@
import { useEffect, useState } from 'react';
import authenticator from './../Authenticator';
export default function App() {
const [isLoggedIn, setIsLoggedIn] = useState([]);
useEffect(() => {
authenticator.fetchLoggedInUserInfo()
.then((res) => {
setIsLoggedIn(res);
});
}, [authenticator.user]);
return (
<Router>
<Switch>
<Route path="/login">
</Route>
</Switch>
</Router>
);
}

View file

@ -0,0 +1,3 @@
export default function Login() {
}

5
bfrontend/src/Config.js Normal file
View file

@ -0,0 +1,5 @@
const config = {
apiUrl: 'http://localhost:3002'
};
export default config;

37
bfrontend/src/Logger.js Normal file
View file

@ -0,0 +1,37 @@
function loggerOfType(components, type='log') {
return (...args) => {
let str = '';
for (const v of components) {
str += `[${v}] `;
}
switch (type) {
case 'log': {
console.log(str, ...args);
}
case 'error': {
console.error(str, ...args);
}
case 'warn': {
console.warn(str, ...args);
}
case 'genmsg': {
return str;
}
}
return str;
}
};
export default function Logger(components, types=['warn', 'error', 'log']) {
const loggerObj = {};
for (type of types) {
loggerObj[type] = loggerOfType(components, type);
}
return loggerObj;
}

0
bfrontend/src/index.css Normal file
View file

11
bfrontend/src/index.js Normal file
View file

@ -0,0 +1,11 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './Components/App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);