Skip to content

Commit

Permalink
panel-toggles: Convert to TypeScript
Browse files Browse the repository at this point in the history
This uses the newly defined types in the controls reducer.

The migration doc recommends replacing connect with the hooks API¹.

¹ https://redux.js.org/usage/migrating-to-modern-redux#migrating-connect-to-hooks
  • Loading branch information
victorlin committed Oct 11, 2023
1 parent f6431f8 commit c33630d
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/components/controls/panel-toggles.tsx
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import React from "react";
import { connect } from "react-redux";
import { withTranslation } from 'react-i18next';
import { useDispatch, useSelector } from "react-redux";
import { useTranslation } from 'react-i18next';

import Toggle from "./toggle";
import { togglePanelDisplay } from "../../actions/panelDisplay";
import { RootState } from "../../store";

@connect((state) => ({
panelsAvailable: state.controls.panelsAvailable,
panelsToDisplay: state.controls.panelsToDisplay,
showTreeToo: state.controls.showTreeToo
}))
class PanelToggles extends React.Component {
render() {
const { t } = this.props;
export default function PanelToggles() {
const dispatch = useDispatch();
const { t } = useTranslation();

const panels = this.props.panelsAvailable.slice();
if (this.props.showTreeToo && panels.indexOf("map") !== -1) {
panels.splice(panels.indexOf("map"), 1);
}
return panels.map((n) => (
const panelsAvailable = useSelector((state: RootState) => state.controls.panelsAvailable);
const panelsToDisplay = useSelector((state: RootState) => state.controls.panelsToDisplay);
const showTreeToo = useSelector((state: RootState) => state.controls.showTreeToo);

const panels = panelsAvailable.slice();
if (showTreeToo && panels.indexOf("map") !== -1) {
panels.splice(panels.indexOf("map"), 1);
}
return <>
{panels.map((n) => (
<Toggle
key={n}
display
on={this.props.panelsToDisplay.indexOf(n) !== -1}
callback={() => this.props.dispatch(togglePanelDisplay(n))}
on={panelsToDisplay.indexOf(n) !== -1}
callback={() => dispatch(togglePanelDisplay(n))}
label={t("sidebar:Show " + n)}
style={{paddingBottom: "10px"}}
style={{ paddingBottom: "10px" }}
/>
));
}
))}
</>
}

const WithTranslation = withTranslation()(PanelToggles);
export default WithTranslation;

0 comments on commit c33630d

Please sign in to comment.