Skip to content

Commit

Permalink
Merge pull request #25 from dpakach/typescript
Browse files Browse the repository at this point in the history
Move to typescript
  • Loading branch information
dpakach authored Jan 10, 2021
2 parents bf038a3 + d5fcb50 commit 9041193
Show file tree
Hide file tree
Showing 32 changed files with 2,886 additions and 1,668 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ jobs:
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
cd frontend
yarn
- name: fmt
- name: Go-Fmt
run: diff -u <(echo -n) <(gofmt -d ./)

- name: Lint-js
run: |
cd frontend
yarn lint
- name: Build-Auth
run: |
cd auth
Expand Down
25 changes: 25 additions & 0 deletions frontend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: "@typescript-eslint/parser", // Specifies the ESLint parser
parserOptions: {
ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features
sourceType: "module", // Allows for the use of imports
ecmaFeatures: {
jsx: true // Allows for the parsing of JSX
}
},
settings: {
react: {
version: "detect" // Tells eslint-plugin-react to automatically detect the version of React to use
}
},
extends: [
"plugin:react/recommended", // Uses the recommended rules from @eslint-plugin-react
"plugin:@typescript-eslint/recommended", // Uses the recommended rules from @typescript-eslint/eslint-plugin
"prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
"plugin:prettier/recommended" // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
],
rules: {
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
},
};
7 changes: 7 additions & 0 deletions frontend/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
semi: true,
trailingComma: "all",
singleQuote: true,
printWidth: 120,
tabWidth: 2
};
40 changes: 28 additions & 12 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,49 @@
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --open",
"build": "webpack"
"build": "webpack",
"lint": "eslint '*/**/*.{js,ts,tsx}' --quiet",
"lint-fix": "eslint '*/**/*.{js,ts,tsx}' --quiet --fix"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"webpack-cli": "^3.3.12"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@babel/preset-react": "^7.10.4",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.20",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/react-router-dom": "^5.1.7",
"@typescript-eslint/eslint-plugin": "^4.12.0",
"@typescript-eslint/parser": "^4.12.0",
"awesome-typescript-loader": "^5.2.1",
"babel-loader": "^8.1.0",
"core-js": "^3.6.5",
"css-loader": "^4.3.0",
"html-webpack-plugin": "^4.5.0",
"mobx": "^5.0.1",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.22.0",
"html-webpack-plugin": "^4.5.1",
"mobx": "^6.0.4",
"node-sass": "^4.14.1",
"prettier": "^2.2.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"redoc": "^2.0.0-rc.40",
"sass-loader": "^10.0.2",
"source-map-loader": "^2.0.0",
"style-loader": "^1.3.0",
"styled-components": "^5.2.0",
"webpack": "^4.44.2",
"webpack-dev-server": "^3.11.0"
"ts-loader": "^8.0.14",
"typescript": "^4.1.3",
"webpack": "^4.45.0",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.1"
}
}
87 changes: 0 additions & 87 deletions frontend/src/App.js

This file was deleted.

96 changes: 96 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as React from 'react';
import { useState } from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import Posts from './Posts';
import Login from './Login';
import Signup from './Signup';
import ProfilePage from './ProfilePage';
import { post } from './helpers/request';
import SinglePost from './SinglePost';
import Docs from './Docs';

import './styles/main.scss';
import { Tokens } from './types/types';

export default function App() {
const tokensString = window.localStorage.getItem('tokens');

const [tokens, setTokens]: [Tokens, (Tokens: Tokens) => void] = useState<Tokens>(JSON.parse(tokensString) as Tokens);
const [loggedIn, setLoggedIn] = useState(tokensString != null);
const [message, setMessage]: [string, (messages: string) => void] = useState('');

function handleLogout() {
window.localStorage.removeItem('tokens');
setLoggedIn(false);
return post('/auth/logout', { headers: { token: tokens.token } })
.then((res) => res.json())
.then(
() => {
setTokens({} as Tokens);
setLoggedIn(false);
setMessage('Logged out successfully');
},
(error) => {
setMessage('Error: ' + error.message);
},
);
}

if (window.location.pathname.startsWith('/docs')) {
return <Docs />;
}

return (
<Router>
<div className="main">
<div className="container">
<h1 className="header">Zwitter</h1>
{message && <p> {message} </p>}
{loggedIn && (
<p>
Logged in as <b>@{tokens.user.username}</b>
</p>
)}
<div>
<Link to="/">Home </Link>
{loggedIn ? (
<>
<Link to="/profile">Profile </Link>
<Link to="#" onClick={handleLogout}>
{' '}
Logout{' '}
</Link>
</>
) : (
<>
<Link to="/login">Login </Link>
<Link to="/signup">Signup </Link>
</>
)}

<Switch>
<Route
exact
path="/post/:id"
render={(props) => <SinglePost {...props} loggedIn={loggedIn} tokens={tokens} />}
/>
<Route exact path="/login">
<Login loggedIn={loggedIn} setTokens={setTokens} setLoggedIn={setLoggedIn} />
</Route>
<Route exact path="/signup">
<Signup loggedIn={loggedIn} setLoggedIn={setLoggedIn} />
</Route>
<Route exact path="/profile">
<ProfilePage loggedIn={loggedIn} tokens={tokens} self={true} />
</Route>
<Route exact path="/profile/:user">
<ProfilePage loggedIn={loggedIn} tokens={tokens} />
</Route>
<Route exact path="/" render={(props) => <Posts {...props} loggedIn={loggedIn} tokens={tokens} />} />
</Switch>
</div>
</div>
</div>
</Router>
);
}
24 changes: 0 additions & 24 deletions frontend/src/Docs.js

This file was deleted.

25 changes: 25 additions & 0 deletions frontend/src/Docs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from 'react';
import { useState, useEffect } from 'react';
import { RedocStandalone } from 'redoc';
import { baseUrl } from './const';

function Docs() {
const [service, setService]: [string, (service: string) => void] = useState('auth');

function handleChange(e: React.ChangeEvent<HTMLSelectElement>) {
setService(e.target.value);
}

return (
<div>
<select value={service} onChange={handleChange}>
<option value="auth">Auth Service</option>
<option value="posts">Posts Service</option>
<option value="users">Users Service</option>
</select>
<RedocStandalone specUrl={`${baseUrl}/${service}/swagger.json`} />
</div>
);
}

export default Docs;
46 changes: 0 additions & 46 deletions frontend/src/Login.js

This file was deleted.

Loading

0 comments on commit 9041193

Please sign in to comment.