Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootzooka frontend - the more functional way #540

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend-start.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

sbt "~backend/reStart"
sbt "~backend/reStart" -mem 3000
3 changes: 3 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"axios": "^0.20.0",
"bootstrap": "^4.5.2",
"formik": "^2.2.0",
"fp-ts": "^2.9.5",
"immer": "^7.0.9",
"io-ts": "^2.2.14",
"io-ts-reporters": "^1.2.2",
"jest-environment-jsdom-sixteen": "^1.0.3",
"noop-ts": "^1.0.3",
"react": "^16.13.1",
Expand Down
31 changes: 13 additions & 18 deletions ui/src/contexts/UserContext/UserContext.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import React from "react";
import immer from "immer";
import noop from "noop-ts";

export interface UserDetails {
createdOn: string;
email: string;
login: string;
}
import { ApiKey, UserDetails } from "../../services/UserService/UserServiceFP";
import { none, Option } from "fp-ts/es6/Option";

export interface UserState {
apiKey: string | null;
user: UserDetails | null;
loggedIn: boolean | null;
apiKey: Option<ApiKey>;
user: Option<UserDetails>;
// TODO: do we really need this flag??
// loggedIn: boolean;
}

export const initialUserState: UserState = {
apiKey: null,
user: null,
loggedIn: null,
apiKey: none,
user: none,
// loggedIn: false,
};

export type UserAction =
| { type: "SET_API_KEY"; apiKey: string | null }
| { type: "SET_API_KEY"; apiKey: Option<ApiKey> }
| { type: "UPDATE_USER_DATA"; user: Partial<UserDetails> }
| { type: "LOG_IN"; user: UserDetails }
| { type: "LOG_IN"; user: Option<UserDetails> }
| { type: "LOG_OUT" };

const UserReducer = (state: UserState, action: UserAction): UserState => {
Expand All @@ -40,13 +37,11 @@ const UserReducer = (state: UserState, action: UserAction): UserState => {
case "LOG_IN":
return immer(state, (draftState) => {
draftState.user = action.user;
draftState.loggedIn = true;
});
case "LOG_OUT":
return immer(state, (draftState) => {
draftState.apiKey = null;
draftState.user = null;
draftState.loggedIn = false;
draftState.apiKey = none;
draftState.user = none;
});
}
};
Expand Down
23 changes: 8 additions & 15 deletions ui/src/main/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@ import Footer from "../Footer/Footer";
import Top from "../Top/Top";
import ForkMe from "../ForkMe/ForkMe";
import { UserContext } from "../../contexts/UserContext/UserContext";
import Loader from "../Loader/Loader";
import Routes from "../Routes/Routes";
import useLoginOnApiKey from "./useLoginOnApiKey";
import useLocalStoragedApiKey from "./useLocalStoragedApiKey";

const Main: React.FC = () => {
const {
state: { loggedIn },
state: { user },
} = React.useContext(UserContext);

useLocalStoragedApiKey();
useLoginOnApiKey();

if (loggedIn === null) {
return <Loader />;
}

return (
<>
<Top />
<ForkMe>
<Routes />
</ForkMe>
<Footer />
</>
);
return <>
<Top />
<ForkMe>
<Routes />
</ForkMe>
<Footer />
</>
};

export default Main;
32 changes: 17 additions & 15 deletions ui/src/main/Main/useLocalStoragedApiKey.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from "react";
import { UserContext } from "../../contexts/UserContext/UserContext";
import { fold, fromNullable, some } from "fp-ts/Option";
import { pipe } from "fp-ts/pipeable";

const useLocalStoragedApiKey = () => {
const {
dispatch,
state: { apiKey, loggedIn },
state: { user, apiKey },
} = React.useContext(UserContext);

const apiKeyRef = React.useRef(apiKey);
Expand All @@ -14,24 +16,24 @@ const useLocalStoragedApiKey = () => {
}, [apiKey, dispatch]);

React.useEffect(() => {
const storedApiKey = localStorage.getItem("apiKey");

if (!storedApiKey) return dispatch({ type: "LOG_OUT" });

dispatch({ type: "SET_API_KEY", apiKey: storedApiKey });
pipe(
fromNullable(localStorage.getItem("apiKey")),
fold(
() => dispatch({ type: 'LOG_OUT' }),
key => dispatch({ type: 'SET_API_KEY', apiKey: some(key) })
)
);
}, [dispatch]);

React.useEffect(() => {
switch (loggedIn) {
case true:
return localStorage.setItem("apiKey", apiKeyRef.current || "");
case false:
return localStorage.removeItem("apiKey");
case null:
default:
return;
}
}, [loggedIn]);
pipe(apiKeyRef.current,
fold(
() => localStorage.removeItem('apiKey'),
key => localStorage.setItem('apiKey', key),
)
)
}, [user]);
};

export default useLocalStoragedApiKey;
18 changes: 13 additions & 5 deletions ui/src/main/Main/useLoginOnApiKey.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from "react";
import { UserContext } from "../../contexts/UserContext/UserContext";
import userService from "../../services/UserService/UserService";
import { pipe } from "fp-ts/pipeable";
import { map, some } from "fp-ts/Option";

const useLoginOnApiKey = () => {
const {
Expand All @@ -9,12 +11,18 @@ const useLoginOnApiKey = () => {
} = React.useContext(UserContext);

React.useEffect(() => {
if (!apiKey) return;
console.log('useLoginOnApiKey');
pipe(
apiKey,
map(key => {
userService
.getCurrentUser(key)
.then((user) => dispatch({ type: "LOG_IN", user: some(user) }))
.catch(() => dispatch({ type: "LOG_OUT" }));
})
)


userService
.getCurrentUser(apiKey)
.then((user) => dispatch({ type: "LOG_IN", user }))
.catch(() => dispatch({ type: "LOG_OUT" }));
}, [apiKey, dispatch]);
};

Expand Down
14 changes: 10 additions & 4 deletions ui/src/main/Routes/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import React from "react";
import { Route, RouteProps } from "react-router-dom";
import { UserContext } from "../../contexts/UserContext/UserContext";
import Login from "../../pages/Login/Login";
import { fold } from "fp-ts/Option";
import { pipe } from "fp-ts/pipeable";

const ProtectedRoute: React.FC<RouteProps> = ({ children, ...props }) => {
const {
state: { loggedIn },
state: { user },
} = React.useContext(UserContext);

if (!loggedIn) return <Route {...props} component={Login} />;

return <Route {...props}>{children}</Route>;
return pipe(
user,
fold(
() => <Route {...props} component={Login} />,
_ => <Route {...props}>{children}</Route>
)
);
};

export default ProtectedRoute;
51 changes: 27 additions & 24 deletions ui/src/main/Top/Top.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import Nav from "react-bootstrap/Nav";
import Container from "react-bootstrap/Container";
import { LinkContainer } from "react-router-bootstrap";
import { UserContext } from "../../contexts/UserContext/UserContext";
import { BiPowerOff, BiHappy } from "react-icons/bi";
import { BiHappy, BiPowerOff } from "react-icons/bi";
import { pipe } from "fp-ts/pipeable";

const Top: React.FC = () => {
const {
state: { user, loggedIn },
state: { user },
dispatch,
} = React.useContext(UserContext);

Expand All @@ -30,29 +31,31 @@ const Top: React.FC = () => {
<Nav.Link>Home</Nav.Link>
</LinkContainer>
<div className="flex-grow-1" />
{loggedIn ? (
<>
<LinkContainer to="/profile">
<Nav.Link className="text-right">
<BiHappy />
&nbsp;{user?.login}
{pipe(
user,
() => (
<>
<LinkContainer to="/register">
<Nav.Link className="text-right">Register</Nav.Link>
</LinkContainer>
<LinkContainer to="/login">
<Nav.Link className="text-right">Login</Nav.Link>
</LinkContainer>
</>),
u => (
<>
<LinkContainer to="/profile">
<Nav.Link className="text-right">
<BiHappy />
&nbsp;{}
</Nav.Link>
</LinkContainer>{" "}
<Nav.Link className="text-right" onClick={handleLogOut}>
<BiPowerOff />
&nbsp;Logout
</Nav.Link>
</LinkContainer>{" "}
<Nav.Link className="text-right" onClick={handleLogOut}>
<BiPowerOff />
&nbsp;Logout
</Nav.Link>
</>
) : (
<>
<LinkContainer to="/register">
<Nav.Link className="text-right">Register</Nav.Link>
</LinkContainer>
<LinkContainer to="/login">
<Nav.Link className="text-right">Login</Nav.Link>
</LinkContainer>
</>
)}
</>))
}
</Nav>
</Navbar.Collapse>
</Container>
Expand Down
6 changes: 3 additions & 3 deletions ui/src/pages/Login/Login.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ beforeEach(() => {
test("renders header", () => {
const { getByText } = render(
<Router history={history}>
<UserContext.Provider value={{ state: { ...initialUserState, loggedIn: false }, dispatch }}>
<UserContext.Provider value={{ state: { ...initialUserState }, dispatch }}>
<Login />
</UserContext.Provider>
</Router>
Expand All @@ -30,7 +30,7 @@ test("renders header", () => {
test("redirects when logged in", () => {
render(
<Router history={history}>
<UserContext.Provider value={{ state: { ...initialUserState, loggedIn: true }, dispatch }}>
<UserContext.Provider value={{ state: { ...initialUserState }, dispatch }}>
<Login />
</UserContext.Provider>
</Router>
Expand Down Expand Up @@ -73,7 +73,7 @@ test("handles login error", async () => {

const { getByLabelText, getByText, findByRole } = render(
<Router history={history}>
<UserContext.Provider value={{ state: { ...initialUserState, loggedIn: false }, dispatch }}>
<UserContext.Provider value={{ state: { ...initialUserState }, dispatch }}>
<Login />
</UserContext.Provider>
</Router>
Expand Down