Skip to content

Commit e8fa9e8

Browse files
committed
Move updateInterval to redux store
1 parent bc766f1 commit e8fa9e8

File tree

5 files changed

+20
-24
lines changed

5 files changed

+20
-24
lines changed

mithril-explorer/components/EpochSettings/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {useSelector} from "react-redux";
66
export default function EpochSettings(props) {
77
const [epochSettings, setEpochSettings] = useState({});
88
const autoUpdate = useSelector((state) => state.settings.autoUpdate);
9+
const updateInterval = useSelector((state) => state.settings.updateInterval);
910

1011
useEffect(() => {
1112
if (!autoUpdate) {
@@ -25,9 +26,9 @@ export default function EpochSettings(props) {
2526
// Fetch it once without waiting
2627
fetchEpochSettings();
2728

28-
const interval = setInterval(fetchEpochSettings, props.updateInterval);
29+
const interval = setInterval(fetchEpochSettings, updateInterval);
2930
return () => clearInterval(interval);
30-
}, [props.aggregator, props.updateInterval, autoUpdate]);
31+
}, [props.aggregator, updateInterval, autoUpdate]);
3132

3233
return (
3334
<div>

mithril-explorer/components/PendingCertificate/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {useSelector} from "react-redux";
77
export default function PendingCertificate(props) {
88
const [pendingCertificate, setPendingCertificate] = useState({});
99
const autoUpdate = useSelector((state) => state.settings.autoUpdate);
10+
const updateInterval = useSelector((state) => state.settings.updateInterval);
1011

1112
useEffect(() => {
1213
if (!autoUpdate) {
@@ -26,9 +27,9 @@ export default function PendingCertificate(props) {
2627
// Fetch it once without waiting
2728
fetchPendingCertificate();
2829

29-
const interval = setInterval(fetchPendingCertificate, props.updateInterval);
30+
const interval = setInterval(fetchPendingCertificate, updateInterval);
3031
return () => clearInterval(interval);
31-
}, [props.aggregator, props.updateInterval, autoUpdate]);
32+
}, [props.aggregator, updateInterval, autoUpdate]);
3233

3334
return (
3435
<div className={props.className}>

mithril-explorer/components/SnapshotsList/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default function SnapshotsList(props) {
2323
const [snapshots, setSnapshots] = useState([]);
2424
const [selectedCertificateHash, setSelectedCertificateHash] = useState(undefined);
2525
const autoUpdate = useSelector((state) => state.settings.autoUpdate);
26+
const updateInterval = useSelector((state) => state.settings.updateInterval);
2627

2728
useEffect(() => {
2829
if (!autoUpdate) {
@@ -42,9 +43,9 @@ export default function SnapshotsList(props) {
4243
// Fetch them once without waiting
4344
fetchSnapshots();
4445

45-
const interval = setInterval(fetchSnapshots, props.updateInterval);
46+
const interval = setInterval(fetchSnapshots, updateInterval);
4647
return () => clearInterval(interval);
47-
}, [props.aggregator, props.updateInterval, autoUpdate]);
48+
}, [props.aggregator, updateInterval, autoUpdate]);
4849

4950
function handleCertificateHashChange(hash) {
5051
setSelectedCertificateHash(hash);

mithril-explorer/pages/index.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ import available_aggregators from "../aggregators-list";
1010
import {useRouter} from "next/router";
1111
import EpochSettings from "../components/EpochSettings";
1212
import {useDispatch, useSelector} from "react-redux";
13-
import {toggleAutoUpdate} from "../store/settingsSlice";
13+
import {setUpdateInterval, toggleAutoUpdate} from "../store/settingsSlice";
1414

1515
function IntervalSetter(props) {
1616
const autoUpdate = useSelector((state) => state.settings.autoUpdate);
17+
const updateInterval = useSelector((state) => state.settings.updateInterval);
1718
const dispatch = useDispatch();
18-
19-
function handleChange(event) {
20-
props.onIntervalChange(parseInt(event.target.value));
21-
}
2219

2320
return (
2421
<Form.Group as={Col} className={props.className}>
@@ -27,7 +24,7 @@ function IntervalSetter(props) {
2724
<Button type="button" onClick={() => dispatch(toggleAutoUpdate())} variant={autoUpdate ? "primary" : "success"}>
2825
{autoUpdate ? "Pause ⏸" : "Resume ▶"}
2926
</Button>
30-
<Form.Select value={props.interval} onChange={handleChange}>
27+
<Form.Select value={updateInterval} onChange={(e) => dispatch(setUpdateInterval(e.target.value))}>
3128
<option value={1000}>1 seconds</option>
3229
<option value={5000}>5 seconds</option>
3330
<option value={10000}>10 seconds</option>
@@ -40,8 +37,6 @@ function IntervalSetter(props) {
4037
export default function Explorer() {
4138
const router = useRouter();
4239
const [aggregator, setAggregator] = useState(available_aggregators[0]);
43-
const [interval, setInterval] = useState(10000);
44-
const autoUpdate = useSelector((state) => state.settings.autoUpdate);
4540

4641
useEffect(() => {
4742
if (router.query?.aggregator && router.query?.aggregator !== aggregator) {
@@ -80,20 +75,18 @@ export default function Explorer() {
8075
aggregator={aggregator}
8176
onAggregatorChange={handleApiChange}
8277
defaultAvailableAggregators={available_aggregators} />
83-
<IntervalSetter
84-
interval={interval}
85-
onIntervalChange={handleIntervalChange} />
78+
<IntervalSetter />
8679
</Row>
8780
</Form>
8881
<Row>
8982
<Col>
90-
<EpochSettings aggregator={aggregator} updateInterval={interval} />
83+
<EpochSettings aggregator={aggregator} />
9184
</Col>
9285
<Col xs={8}>
93-
<PendingCertificate aggregator={aggregator} updateInterval={interval} />
86+
<PendingCertificate aggregator={aggregator} />
9487
</Col>
9588
</Row>
96-
<SnapshotsList aggregator={aggregator} updateInterval={interval} autoUpdate={autoUpdate} />
89+
<SnapshotsList aggregator={aggregator} />
9790
</Stack>
9891
</main>
9992
</div>

mithril-explorer/store/settingsSlice.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ export const settingsSlice = createSlice({
55
name: 'settings',
66
initialState: {
77
aggregator: available_aggregators[0],
8-
interval: 10000,
8+
updateInterval: 10000,
99
autoUpdate: true,
1010
},
1111
reducers: {
12-
setFetchInterval: (state, action) => {
13-
state.interval = state.value;
12+
setUpdateInterval: (state, action) => {
13+
state.updateInterval = action.payload;
1414
},
1515
toggleAutoUpdate: (state) => {
1616
state.autoUpdate = !state.autoUpdate;
1717
},
1818
}
1919
});
2020

21-
export const { setFetchInterval, toggleAutoUpdate } = settingsSlice.actions;
21+
export const { setUpdateInterval, toggleAutoUpdate } = settingsSlice.actions;
2222

2323
export default settingsSlice.reducer;

0 commit comments

Comments
 (0)