Skip to content

Commit 6e31a42

Browse files
authored
Redux Toolkit Redux Rewrite (#2003)
Signed-off-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
1 parent f6cab5a commit 6e31a42

File tree

240 files changed

+6294
-10741
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

240 files changed

+6294
-10741
lines changed

portal-ui/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@mui/material": "^5.4.0",
1717
"@mui/styled-engine-sc": "^5.3.0",
1818
"@mui/styles": "^5.3.0",
19+
"@reduxjs/toolkit": "^1.8.1",
1920
"@types/history": "^4.7.3",
2021
"@types/jest": "27.4.0",
2122
"@types/lodash": "^4.14.149",
@@ -25,7 +26,7 @@
2526
"@types/react-copy-to-clipboard": "^5.0.2",
2627
"@types/react-dom": "17.0.11",
2728
"@types/react-grid-layout": "^1.1.1",
28-
"@types/react-redux": "^7.1.5",
29+
"@types/react-redux": "^7.1.24",
2930
"@types/react-router": "^5.1.3",
3031
"@types/react-router-dom": "^5.1.2",
3132
"@types/react-virtualized": "^9.21.10",
@@ -92,8 +93,8 @@
9293
"react-app-rewire-hot-loader": "^2.0.1",
9394
"react-app-rewired": "^2.1.6",
9495
"react-scripts": "5.0.1",
95-
"typescript": "^4.4.3",
96-
"testcafe": "^1.18.6"
96+
"testcafe": "^1.18.6",
97+
"typescript": "^4.4.3"
9798
},
9899
"resolutions": {
99100
"nth-check": "^2.0.1",

portal-ui/src/ProtectedRoutes.tsx

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,74 +16,63 @@
1616

1717
import React, { useEffect, useState } from "react";
1818
import { Redirect } from "react-router-dom";
19-
import { connect } from "react-redux";
20-
import { AppState } from "./store";
21-
import {
22-
consoleOperatorMode,
23-
setDistributedMode,
24-
setErrorSnackMessage,
25-
setSiteReplicationInfo,
26-
userLoggedIn,
27-
} from "./actions";
19+
2820
import api from "./common/api";
29-
import { saveSessionResponse } from "./screens/Console/actions";
3021
import { ISessionResponse } from "./screens/Console/types";
3122
import useApi from "./screens/Console/Common/Hooks/useApi";
3223
import { ErrorResponseHandler } from "./common/types";
3324
import { ReplicationSite } from "./screens/Console/Configurations/SiteReplication/SiteReplication";
34-
import { SRInfoStateType } from "./types";
3525
import { baseUrl } from "./history";
26+
import { useDispatch, useSelector } from "react-redux";
27+
import {
28+
globalSetDistributedSetup,
29+
operatorMode,
30+
setSiteReplicationInfo,
31+
userLogged,
32+
} from "./systemSlice";
33+
import { SRInfoStateType } from "./types";
34+
import { AppState } from "./store";
35+
import { saveSessionResponse } from "./screens/Console/consoleSlice";
3636

3737
interface ProtectedRouteProps {
38-
loggedIn: boolean;
3938
Component: any;
40-
userLoggedIn: typeof userLoggedIn;
41-
consoleOperatorMode: typeof consoleOperatorMode;
42-
saveSessionResponse: typeof saveSessionResponse;
43-
setDistributedMode: typeof setDistributedMode;
44-
setSiteReplicationInfo: typeof setSiteReplicationInfo;
4539
}
4640

47-
const ProtectedRoute = ({
48-
Component,
49-
loggedIn,
50-
userLoggedIn,
51-
consoleOperatorMode,
52-
saveSessionResponse,
53-
setDistributedMode,
54-
setSiteReplicationInfo,
55-
}: ProtectedRouteProps) => {
41+
const ProtectedRoute = ({ Component }: ProtectedRouteProps) => {
42+
const dispatch = useDispatch();
43+
44+
const isOperatorMode = useSelector(
45+
(state: AppState) => state.system.operatorMode
46+
);
47+
5648
const [sessionLoading, setSessionLoading] = useState<boolean>(true);
49+
const userLoggedIn = useSelector((state: AppState) => state.system.loggedIn);
5750

5851
useEffect(() => {
5952
api
6053
.invoke("GET", `/api/v1/session`)
6154
.then((res: ISessionResponse) => {
62-
saveSessionResponse(res);
63-
userLoggedIn(true);
55+
dispatch(saveSessionResponse(res));
56+
dispatch(userLogged(true));
6457
setSessionLoading(false);
65-
setDistributedMode(res.distributedMode || false);
58+
dispatch(globalSetDistributedSetup(res.distributedMode || false));
6659
// check for tenants presence, that indicates we are in operator mode
6760
if (res.operator) {
68-
consoleOperatorMode(true);
61+
dispatch(operatorMode(true));
6962
document.title = "MinIO Operator";
7063
}
7164
})
7265
.catch(() => setSessionLoading(false));
73-
}, [
74-
saveSessionResponse,
75-
consoleOperatorMode,
76-
userLoggedIn,
77-
setDistributedMode,
78-
]);
66+
}, [dispatch]);
7967

8068
const [, invokeSRInfoApi] = useApi(
8169
(res: any) => {
82-
const {
83-
sites: siteList = [],
84-
name: curSiteName,
85-
enabled = false,
86-
} = res || {};
70+
const { name: curSiteName, enabled = false } = res || {};
71+
72+
let siteList = res.site;
73+
if (!siteList) {
74+
siteList = [];
75+
}
8776
const isSiteNameInList = siteList.find((si: ReplicationSite) => {
8877
return si.name === curSiteName;
8978
});
@@ -95,42 +84,31 @@ const ProtectedRoute = ({
9584
siteName: isCurSite ? curSiteName : "",
9685
};
9786

98-
setSiteReplicationInfo(siteReplicationDetail);
87+
dispatch(setSiteReplicationInfo(siteReplicationDetail));
9988
},
10089
(err: ErrorResponseHandler) => {
101-
setErrorSnackMessage(err);
90+
// we will fail this call silently, but show it on the console
91+
console.error(`Error loading site replication status`, err);
10292
}
10393
);
10494

10595
useEffect(() => {
106-
if (loggedIn && !sessionLoading) {
96+
if (userLoggedIn && !sessionLoading && !isOperatorMode) {
10797
invokeSRInfoApi("GET", `api/v1/admin/site-replication`);
10898
}
10999
// eslint-disable-next-line react-hooks/exhaustive-deps
110-
}, [loggedIn, sessionLoading]);
100+
}, [userLoggedIn, sessionLoading]);
111101

112-
// if we still trying to retrieve user session render nothing
102+
// if we're still trying to retrieve user session render nothing
113103
if (sessionLoading) {
114104
return null;
115105
}
116106
// redirect user to the right page based on session status
117-
return loggedIn ? (
107+
return userLoggedIn ? (
118108
<Component />
119109
) : (
120110
<Redirect to={{ pathname: `${baseUrl}login` }} />
121111
);
122112
};
123113

124-
const mapState = (state: AppState) => ({
125-
loggedIn: state.system.loggedIn,
126-
});
127-
128-
const connector = connect(mapState, {
129-
userLoggedIn,
130-
consoleOperatorMode,
131-
saveSessionResponse,
132-
setDistributedMode,
133-
setSiteReplicationInfo,
134-
});
135-
136-
export default connector(ProtectedRoute);
114+
export default ProtectedRoute;

portal-ui/src/actions.ts

Lines changed: 0 additions & 126 deletions
This file was deleted.

portal-ui/src/common/SecureComponent/__tests__/accessControl.test.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616

1717
import hasPermission from "../accessControl";
1818
import { store } from "../../../store";
19-
import { SESSION_RESPONSE } from "../../../screens/Console/actions";
2019
import { IAM_PAGES, IAM_PAGES_PERMISSIONS, IAM_SCOPES } from "../permissions";
20+
import { saveSessionResponse } from "../../../screens/Console/consoleSlice";
2121

2222
const setPolicy1 = () => {
23-
store.dispatch({
24-
type: SESSION_RESPONSE,
25-
message: {
23+
store.dispatch(
24+
saveSessionResponse({
2625
distributedMode: true,
2726
features: ["log-search"],
2827
permissions: {
@@ -50,13 +49,12 @@ const setPolicy1 = () => {
5049
},
5150
operator: false,
5251
status: "ok",
53-
},
54-
});
52+
})
53+
);
5554
};
5655
const setPolicy2 = () => {
57-
store.dispatch({
58-
type: SESSION_RESPONSE,
59-
message: {
56+
store.dispatch(
57+
saveSessionResponse({
6058
distributedMode: true,
6159
operator: false,
6260
features: [],
@@ -93,13 +91,12 @@ const setPolicy2 = () => {
9391
"console-ui": ["admin:CreateServiceAccount", "admin:CreateUser"],
9492
},
9593
status: "ok",
96-
},
97-
});
94+
})
95+
);
9896
};
9997
const setPolicy3 = () => {
100-
store.dispatch({
101-
type: SESSION_RESPONSE,
102-
message: {
98+
store.dispatch(
99+
saveSessionResponse({
103100
distributedMode: true,
104101
features: [],
105102
permissions: {
@@ -112,14 +109,13 @@ const setPolicy3 = () => {
112109
},
113110
status: "ok",
114111
operator: false,
115-
},
116-
});
112+
})
113+
);
117114
};
118115

119116
const setPolicy4 = () => {
120-
store.dispatch({
121-
type: SESSION_RESPONSE,
122-
message: {
117+
store.dispatch(
118+
saveSessionResponse({
123119
distributedMode: true,
124120
features: [],
125121
permissions: {
@@ -129,8 +125,8 @@ const setPolicy4 = () => {
129125
},
130126
status: "ok",
131127
operator: false,
132-
},
133-
});
128+
})
129+
);
134130
};
135131

136132
test("Upload button disabled", () => {

portal-ui/src/icons/mkube_logo_temp.svg

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)