Skip to content

Commit 414cd63

Browse files
committed
[dashboard] redux added for state management
1 parent ac5ee60 commit 414cd63

File tree

9 files changed

+118
-26
lines changed

9 files changed

+118
-26
lines changed

dashboard/package-lock.json

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

dashboard/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
"@testing-library/user-event": "^7.1.2",
99
"axios": "^0.19.0",
1010
"bootstrap": "^4.4.1",
11+
"lodash": "^4.17.15",
1112
"react": "^16.12.0",
1213
"react-bootstrap": "^1.0.0-beta.16",
1314
"react-dom": "^16.12.0",
14-
"react-scripts": "3.3.0"
15+
"react-redux": "^7.1.3",
16+
"react-scripts": "3.3.0",
17+
"redux": "^4.0.5",
18+
"redux-thunk": "^2.3.0"
1519
},
1620
"scripts": {
1721
"start": "react-scripts start",

dashboard/src/actions/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scanApi from '../apis/scanApi';
2+
import { FETCH_SCANS, ADD_SCAN } from './types';
3+
4+
export const fetchScans = () => async dispatch => {
5+
try {
6+
const response = await scanApi.get('/results');
7+
dispatch({ type: FETCH_SCANS, payload: response.data });
8+
} catch (e) {
9+
throw new Error('could not fetch scans');
10+
}
11+
};
12+
13+
export const createScan = repoName => async (dispatch, getState) => {
14+
try {
15+
const response = await scanApi.post('/results', { repoName });
16+
dispatch({ type: ADD_SCAN, payload: response.data });
17+
} catch (e) {
18+
throw new Error('Could not submit scan');
19+
}
20+
};

dashboard/src/actions/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const FETCH_SCANS = 'FETCH_SCANS';
2+
export const ADD_SCAN = 'ADD_SCAN';

dashboard/src/apis/scanApi.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import axios from 'axios';
2+
3+
export default axios.create({
4+
baseURL: 'http://localhost:3090',
5+
});

dashboard/src/components/useScan.js

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
1-
import { useState, useEffect } from 'react';
2-
import axios from 'axios';
3-
4-
const url = 'http://localhost:3090/results';
1+
import { useEffect } from 'react';
2+
import { useSelector, useDispatch } from 'react-redux';
3+
import { createScan, fetchScans } from '../actions';
54

65
const useScan = () => {
7-
const [scans, setScans] = useState([]);
8-
9-
const addScan = async repoName => {
10-
let response;
11-
try {
12-
response = await axios.post(url, { repoName });
13-
setScans([...scans, response.data]);
14-
} catch (e) {
15-
throw new Error('The name you entered does not contain authorized characters!');
16-
}
17-
};
6+
const scans = useSelector(state => state.scans.scans);
7+
const dispatch = useDispatch();
188

199
useEffect(() => {
20-
(async () => {
21-
try {
22-
const response = await axios.get(url);
23-
setScans(response.data);
24-
} catch (e) {
25-
throw new Error('could not fetch scan, there is likely a problem with the server');
26-
}
27-
})();
28-
}, []);
10+
dispatch(fetchScans());
11+
}, [dispatch]);
12+
13+
const addScan = repoName => dispatch(createScan(repoName));
2914

3015
return [scans, addScan];
3116
};

dashboard/src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@ import ReactDOM from 'react-dom';
33
import './index.css';
44
import App from './App';
55
import * as serviceWorker from './serviceWorker';
6+
import { createStore, applyMiddleware, compose } from 'redux';
7+
import { Provider } from 'react-redux';
8+
import reduxThunk from 'redux-thunk';
9+
import reducers from './reducers';
610

7-
ReactDOM.render(<App />, document.getElementById('root'));
11+
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
12+
const store = createStore(reducers, composeEnhancers(applyMiddleware(reduxThunk)));
13+
14+
ReactDOM.render(
15+
<Provider store={store}>
16+
<App />
17+
</Provider>,
18+
document.getElementById('root')
19+
);
820

921
// If you want your app to work offline and load faster, you can change
1022
// unregister() to register() below. Note this comes with some pitfalls.

dashboard/src/reducers/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { combineReducers } from 'redux';
2+
import scansReducer from './scansReducer';
3+
4+
export default combineReducers({
5+
scans: scansReducer,
6+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { FETCH_SCANS, ADD_SCAN } from '../actions/types';
2+
3+
export default (state = { scans: [] }, action) => {
4+
let { scans } = state;
5+
6+
switch (action.type) {
7+
case FETCH_SCANS:
8+
scans = action.payload;
9+
break;
10+
case ADD_SCAN:
11+
scans = [...scans, action.payload];
12+
break;
13+
default:
14+
break;
15+
}
16+
17+
return { scans };
18+
};

0 commit comments

Comments
 (0)