Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 2ff7f00

Browse files
authored
Feaure/redux (#8)
* added redux FF-164 * working redux exaple * remove one bug * refactor splitting of actions * changed requested by @open-schnick FF-164
1 parent 961506e commit 2ff7f00

File tree

12 files changed

+243
-67
lines changed

12 files changed

+243
-67
lines changed

webapp_frontend/package-lock.json

Lines changed: 62 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webapp_frontend/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
"@types/react": "^16.9.51",
1313
"@types/react-bootstrap": "^0.32.24",
1414
"@types/react-dom": "^16.9.8",
15+
"@types/react-redux": "^7.1.11",
1516
"axios": "^0.20.0",
1617
"bootstrap": "^4.5.3",
1718
"node-sass": "^4.14.1",
1819
"react": "^16.13.1",
1920
"react-bootstrap": "^1.4.0",
2021
"react-dom": "^16.13.1",
22+
"react-redux": "^7.2.2",
2123
"react-scripts": "3.4.3",
24+
"redux": "^4.0.5",
2225
"typescript": "^3.8.3"
2326
},
2427
"scripts": {

webapp_frontend/src/background/constants.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
interface constants {
32
url: { API_URL: string }
43
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {TokensState} from "./tokenTypes";
2+
3+
export interface SystemState {
4+
tokens: TokensState
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export const ADD_REFRESH_TOKEN = "ADD_REFRESH_TOKEN";
2+
export const ADD_ACCESS_TOKEN = "ADD_ACCESS_TOKEN";
3+
4+
5+
export interface AccessToken{
6+
token: string | null;
7+
timestamp: number | null;
8+
}
9+
10+
11+
export interface TokensState {
12+
refreshToken: string | null;
13+
accessToken: AccessToken | null;
14+
}
15+
16+
17+
interface AddRefreshToken {
18+
type: typeof ADD_REFRESH_TOKEN
19+
payload: string
20+
}
21+
22+
interface AddAccessToken {
23+
type: typeof ADD_ACCESS_TOKEN
24+
payload: AccessToken
25+
}
26+
27+
export type TokenActionsTypes = AddRefreshToken | AddAccessToken;
28+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {ADD_REFRESH_TOKEN, ADD_ACCESS_TOKEN, AccessToken} from "./tokenTypes";
2+
3+
4+
export const addRefreshToken = (content: string) => ({
5+
type: ADD_REFRESH_TOKEN,
6+
payload: content
7+
});
8+
9+
export const addAccessToken = (content: AccessToken) => ({
10+
type: ADD_ACCESS_TOKEN,
11+
payload: content
12+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {combineReducers} from "redux";
2+
import tokens from "./tokens";
3+
4+
// this combines all the stores from the reducers
5+
export default combineReducers({tokens});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {ADD_REFRESH_TOKEN, ADD_ACCESS_TOKEN, TokenActionsTypes, TokensState, AccessToken} from "../actions/tokenTypes";
2+
3+
const initialState: TokensState = {
4+
refreshToken: null,
5+
accessToken: null,
6+
};
7+
8+
export default function (state = initialState, action: TokenActionsTypes) {
9+
switch (action.type) {
10+
case ADD_REFRESH_TOKEN: {
11+
const refreshToken: string = action.payload;
12+
return {
13+
refreshToken: refreshToken,
14+
accessToken: initialState.accessToken
15+
};
16+
}
17+
case ADD_ACCESS_TOKEN: {
18+
const accessToken: AccessToken = action.payload;
19+
return {
20+
refreshToken: initialState.refreshToken,
21+
accessToken: accessToken
22+
};
23+
}
24+
default:
25+
return state;
26+
}
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { createStore } from "redux";
2+
import rootReducer from "./reducers";
3+
4+
export default createStore(rootReducer);
Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,40 @@
11
import React, {ReactElement, useEffect, useState} from 'react';
2-
import logo from '../assets/images/logos/logo.png';
3-
import './App.css';
4-
import {callBackendHealth} from "../background/api/api";
2+
import {connect, ConnectedProps} from 'react-redux'
3+
import {addAccessToken, addRefreshToken} from "../background/redux/actions/tokens";
4+
import {SystemState} from "../background/redux/actions/sytemState";
5+
import {Button} from "react-bootstrap";
56

67

7-
import {Button, Table, Container} from 'react-bootstrap';
8-
import Header from "./basicElements/Header";
8+
// this takes the redux store and maps everything that is needed to the function props
9+
const mapState = (state: SystemState) => ({
10+
tokens: {refreshToken: state.tokens.refreshToken, accessToken: state.tokens.accessToken}
11+
})
912

10-
function App():ReactElement {
11-
12-
13-
14-
const [backendLiveTime, setBackendLiveTime] = useState<number | string>("not reachable");
15-
const [backendUserCount, setBackendUserCount] = useState<number | string>("not reachable");
13+
// this takes the redux actions and maps them to the props
14+
const mapDispatch = {
15+
addRefreshToken, addAccessToken
16+
}
1617

18+
const connector = connect(mapState, mapDispatch)
1719

20+
type PropsFromRedux = ConnectedProps<typeof connector>
1821

22+
// this defines the component props and also adds the redux imported props
23+
type Props = PropsFromRedux & {}
1924

25+
function App(props: Props): ReactElement {
2026

21-
useEffect(() => {
22-
callInitialBackendRequests()
23-
// eslint-disable-next-line react-hooks/exhaustive-deps
24-
},[]);
2527

26-
function callInitialBackendRequests():void {
27-
updateVariables()
28-
}
28+
console.log(props.tokens.refreshToken)
29+
console.log(props.tokens)
2930

30-
function updateVariables(): void {
31-
Promise.all([callBackendHealth()])
32-
.then(([backendHealthData]) => {
33-
setBackendLiveTime(backendHealthData.uptimeInSeconds);
34-
setBackendUserCount(backendHealthData.userCount)
35-
})
36-
}
3731

3832
return (
3933
<div className="App">
40-
<Header></Header>
41-
<Container>
42-
<h1>
43-
FileFighter
44-
</h1>
45-
46-
47-
<img src={logo} alt="logo"/>
48-
49-
50-
51-
<div>
52-
<Button className={"mt-3 mb-2 float-right"} onClick={() => updateVariables()}>Refresh</Button>
53-
<Table striped bordered hover variant="dark" >
54-
<thead>
55-
<tr>
56-
<th>Backend information</th>
57-
58-
</tr>
59-
</thead>
60-
<tbody>
61-
<tr>
62-
<td>Uptime</td>
63-
<td>{backendLiveTime}</td>
64-
</tr>
65-
<tr>
66-
<td>Usercount</td>
67-
<td>{backendUserCount}</td>
68-
</tr>
69-
</tbody>
70-
</Table>
71-
</div>
72-
</Container>
34+
<Button onClick={() => props.addAccessToken({token:"bva",timestamp:1243})}>test (look in the console)</Button>
35+
<Button onClick={() => props.addRefreshToken("bgsgfhz")}>Refreshtoken (look in the console)</Button>
7336
</div>
7437
);
7538
}
7639

77-
export default App;
40+
export default connector(App);

0 commit comments

Comments
 (0)