Skip to content

Commit d7735e5

Browse files
authored
Merge pull request #21 from juherr/feature/redirect-pages
feat: use redirections
2 parents 9549d38 + f604f3d commit d7735e5

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

src/components/Settings.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {useState, useEffect} from "react";
22
import {configAtom} from "../store/store.ts";
33
import {useAtom} from "jotai/index";
44
import {DefaultBootNotification} from "../cp/OcppTypes.ts";
5+
import {useNavigate} from "react-router-dom";
56

67
const Settings: React.FC = () => {
78
const [wsURL, setWsURL] = useState<string>("");
@@ -21,7 +22,7 @@ const Settings: React.FC = () => {
2122
const [experimental, setExperimental] = useState<string | null>(null);
2223
const [bootNotification, setBootNotification] = useState<string | null>(JSON.stringify(DefaultBootNotification));
2324
const [config, setConfig] = useAtom(configAtom);
24-
25+
const navigate = useNavigate();
2526

2627
useEffect(() => {
2728
if (config) {
@@ -62,10 +63,10 @@ const Settings: React.FC = () => {
6263
interval: autoMeterValueInterval,
6364
value: autoMeterValue
6465
},
65-
Experimental: experimental !== "" ? experimental && JSON.parse(experimental) : null,
66-
BootNotification: bootNotification !== "" ? bootNotification && JSON.parse(bootNotification) : null,
67-
});
68-
// navigate(`/${location.hash}`)
66+
Experimental: experimental && experimental !== "" ? JSON.parse(experimental) : null,
67+
BootNotification: bootNotification && bootNotification !== "" ? JSON.parse(bootNotification) : null,
68+
} as Config);
69+
navigate("/");
6970
};
7071

7172
return (

src/components/TopPage.tsx

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,45 @@ import {ChargePoint as OCPPChargePoint} from "../cp/ChargePoint.ts";
66
import {useAtom} from 'jotai'
77
import {configAtom} from "../store/store.ts";
88
import {BootNotification, DefaultBootNotification} from "../cp/OcppTypes.ts";
9-
9+
import {useNavigate} from "react-router-dom";
1010

1111
const TopPage: React.FC = () => {
1212
const [cps, setCps] = useState<OCPPChargePoint[]>([]);
13-
const [connectorNumber, setConnectorNumber] = useState<number>(2);
1413
const [config] = useAtom(configAtom);
1514
const [tagIDs, setTagIDs] = useState<string[]>([]);
15+
const navigate = useNavigate();
1616

1717
useEffect(() => {
18-
console.log(`Connector Number: ${config?.connectorNumber} WSURL: ${config?.wsURL} CPID: ${config?.ChargePointID} TagID: ${config?.tagID}`);
19-
if (config?.Experimental === null) {
20-
setConnectorNumber(config?.connectorNumber || 2);
21-
setCps([
22-
NewChargePoint(connectorNumber, config.ChargePointID, config.BootNotification ?? DefaultBootNotification, config.wsURL, config.basicAuthSettings,config.autoMeterValueSetting)
23-
]);
24-
} else {
25-
const cps = config?.Experimental?.ChargePointIDs.map((cp) =>
26-
NewChargePoint(cp.ConnectorNumber, cp.ChargePointID, config.BootNotification ?? DefaultBootNotification, config.wsURL,config.basicAuthSettings,config.autoMeterValueSetting)
18+
if (!config) {
19+
navigate('/settings');
20+
return;
21+
}
22+
console.log(`Connector Number: ${config.connectorNumber} WSURL: ${config.wsURL} CPID: ${config.ChargePointID} TagID: ${config.tagID}`);
23+
if (config.Experimental) {
24+
const cps = config.Experimental.ChargePointIDs.map((cp) =>
25+
NewChargePoint(cp.ConnectorNumber, cp.ChargePointID, config.BootNotification ?? DefaultBootNotification, config.wsURL, config.basicAuthSettings, config.autoMeterValueSetting)
2726
)
2827
setCps(cps ?? []);
29-
const tagIDs = config?.Experimental?.TagIDs;
28+
const tagIDs = config.Experimental.TagIDs;
3029
setTagIDs(tagIDs ?? []);
30+
} else {
31+
setCps([
32+
NewChargePoint(config.connectorNumber, config.ChargePointID, config.BootNotification ?? DefaultBootNotification, config.wsURL, config.basicAuthSettings, config.autoMeterValueSetting)
33+
]);
3134
}
32-
}, []);
33-
35+
}, [config, navigate]);
3436

3537
return (
3638
<div className="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
3739
{
38-
cps.length === 1 ? (
39-
<>
40-
<ChargePoint cp={cps[0]} TagID={config?.tagID ?? ""}/>
41-
</>
40+
config?.Experimental || cps.length !== 1 ? (
41+
<>
42+
<ExperimentalView cps={cps} tagIDs={tagIDs}/>
43+
</>
4244
) : (
43-
<>
44-
<ExperimentalView cps={cps} tagIDs={tagIDs}/>
45-
</>
45+
<>
46+
<ChargePoint cp={cps[0]} TagID={config?.tagID ?? ""}/>
47+
</>
4648
)
4749
}
4850
</div>

0 commit comments

Comments
 (0)