Skip to content

Commit a08e141

Browse files
AndrewByunctstamperjimmallykelvinmirhan
committed
test changes for RTK update
Co-authored-by: ctstamper <[email protected]> Co-authored-by: jimmally <[email protected]> Co-authored-by: kelvinmirhan <[email protected]>
1 parent b4e6988 commit a08e141

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
"@mui/icons-material": "^5.14.1",
187187
"@mui/material": "^5.14.1",
188188
"@mui/styled-engine-sc": "^5.12.0",
189+
"@reduxjs/toolkit": "^1.9.6",
189190
"@types/react-dom": "^17.0.14",
190191
"@types/react-router-dom": "^5.3.3",
191192
"@visx/axis": "^1.0.0",
@@ -229,13 +230,15 @@
229230
"react-hover": "^2.0.0",
230231
"react-html-parser": "^2.0.2",
231232
"react-json-tree": "^0.11.2",
233+
"react-redux": "^8.1.3",
232234
"react-router-dom": "^5.2.0",
233235
"react-select": "^3.2.0",
234236
"react-spinners": "^0.11.0",
235237
"recoil": "0.0.10",
238+
"redux": "^4.2.1",
236239
"styled-components": "^6.0.4",
237240
"util": "^0.12.4",
238241
"web-vitals": "^1.1.0",
239242
"yarn": "^1.22.19"
240243
}
241-
}
244+
}

src/app/RTKslices.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { createSlice } from '@reduxjs/toolkit';
2+
import { InitialStateProps } from './FrontendTypes';
3+
4+
const initialState: InitialStateProps = { // we initialize what our initialState is here
5+
port: null,
6+
currentTab: null,
7+
currentTitle: 'No Target',
8+
tabs: {},
9+
currentTabInApp: null,
10+
};
11+
12+
const findName = (index, obj) => {
13+
// eslint-disable-next-line eqeqeq
14+
if (obj && obj.index == index) {
15+
return obj.name;
16+
}
17+
const objChildArray = [];
18+
if (obj) {
19+
// eslint-disable-next-line no-restricted-syntax
20+
for (const objChild of obj.children) {
21+
objChildArray.push(findName(index, objChild));
22+
}
23+
}
24+
// eslint-disable-next-line no-restricted-syntax
25+
for (const objChildName of objChildArray) {
26+
if (objChildName) {
27+
return objChildName;
28+
}
29+
}
30+
};
31+
32+
export const historySlice = createSlice({
33+
name: 'history',
34+
initialState: initialState,
35+
reducers: {
36+
changeView: (state, action) => {
37+
const {tabs, currentTab} = state;
38+
const {viewIndex} = tabs[currentTab] || {};
39+
40+
tabs[currentTab].viewIndex = viewIndex === action.payload ? -1 : action.payload;
41+
},
42+
changeSlider: (state, action) => {
43+
const { port, currentTab, tabs } = state;
44+
const { hierarchy, snapshots } = tabs[currentTab] || {};
45+
46+
const nameFromIndex = findName(action.payload, hierarchy);
47+
48+
port.postMessage({
49+
action: 'jumpToSnap',
50+
payload: snapshots[action.payload],
51+
index: action.payload,
52+
name: nameFromIndex,
53+
tabId: currentTab,
54+
});
55+
56+
tabs[currentTab].sliderIndex = action.payload;
57+
},
58+
setCurrentTabInApp: (state, action) => {
59+
state.currentTabInApp = action.payload;
60+
},
61+
},
62+
});
63+
64+
65+
66+
export const { changeView, changeSlider, setCurrentTabInApp } = historySlice.actions;

src/app/RTKstore.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { configureStore } from '@reduxjs/toolkit'; //Import store from redux tool kit
2+
import { historySlice } from './RTKslices'
3+
4+
//Export Store
5+
export const store = configureStore({
6+
reducer: {
7+
history: historySlice.reducer
8+
}
9+
10+
});

src/app/components/StateRoute/History.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import React, { useEffect } from 'react';
66
import { diff, formatters } from 'jsondiffpatch';
77
import * as d3 from 'd3';
88
import { DefaultMargin } from '../../FrontendTypes';
9-
import { changeView, changeSlider, setCurrentTabInApp } from '../../actions/actions';
9+
//removed changeView from line 11 and added it as an import on line 16 from RTKSlices
10+
// import { changeView, changeSlider,setCurrentTabInApp } from '../../actions/actions';
1011
import { useStoreContext } from '../../store';
11-
12+
//importing these methods for RTK
13+
import { useSelector, useDispatch } from 'react-redux';
14+
//import the relevant actions that are used within this file for the slice we are creating
15+
import { changeView, changeSlider, setCurrentTabInApp } from '../../RTKslices'
1216
/*
1317
Render's history page after history button has been selected. Allows user to traverse state history and relevant state branches.
1418
*/
@@ -32,7 +36,14 @@ function History(props: Record<string, unknown>): JSX.Element {
3236
currLocation, // from 'tabs[currentTab]' object in 'MainContainer'
3337
snapshots, // from 'tabs[currentTab].snapshotDisplay' object in 'MainContainer'
3438
} = props;
35-
const [, dispatch] = useStoreContext(); // use the dispatch that is connected with our storeContext
39+
40+
//Commented out dispatch: 10/03/23 3:18 PM from original
41+
// const [, dispatch] = useStoreContext(); // use the dispatch that is connected with our storeContext
42+
43+
//here we are adding useSelector and useDispatch for RTK state conversion
44+
const dispatch = useDispatch();
45+
const currentTab = useSelector(state => state.history.currentTab)
46+
const tabs = useSelector(state => state.history.tabs)
3647

3748
const svgRef = React.useRef(null);
3849
const root = JSON.parse(JSON.stringify(hierarchy)); // why do we stringify and then parse our hierarchy back to JSON? (asked 7/31/23)

src/app/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ import { createRoot } from 'react-dom/client';
44
import App from './components/App';
55
import './styles/main.scss';
66

7+
//adding this line below 11:35 AM 10/03/23
8+
import {store} from './RTKstore'; //imported RTK Store
9+
import {Provider} from 'react-redux'; //imported Provider
10+
711
//Updated rendering sytax for React 18
812
const root = createRoot(document.getElementById("root"));
913
root.render(
1014
// Strict mode is for developers to better track best practices
1115
// <StrictMode>
16+
<Provider store={store}>
1217
<App/>
18+
</Provider>
1319
// </StrictMode>
1420
);

0 commit comments

Comments
 (0)