diff --git a/src/app/Marine2/components/boxes/Inverter/Inverter.tsx b/src/app/Marine2/components/boxes/Inverter/Inverter.tsx index c07193e61..3611c0422 100644 --- a/src/app/Marine2/components/boxes/Inverter/Inverter.tsx +++ b/src/app/Marine2/components/boxes/Inverter/Inverter.tsx @@ -1,23 +1,20 @@ -import { useEffect, useState } from "react" +import { useState } from "react" import { observer } from "mobx-react-lite" -import { translate } from "react-i18nify" import { ComponentMode } from "@m2Types/generic/component-mode" import { ISize } from "@m2Types/generic/size" import { InverterInstanceId, useAppStore, useInverter } from "@victronenergy/mfd-modules" import InverterChargerIcon from "../../../images/icons/inverter-charger.svg" import GeneratorIcon from "../../../images/icons/generator.svg" -import { INVERTER_MODE } from "../../../utils/constants" import classNames from "classnames" import ValueBar from "../../ui/ValueBar" import Button from "../../ui/Button" -import DeviceSettingModal from "../../ui/DeviceSettingModal/DeviceSettingModal" -import RadioButton from "../../ui/RadioButton" import Box from "../../ui/Box" import { applyStyles, defaultBoxStyles } from "../../../utils/media" import ValueOverview from "../../ui/ValueOverview" import { formatModeFor } from "../../../utils/formatters/devices/inverter/format-mode-for" import { translatedStateFor } from "../../../utils/helpers/devices/translated-state-for" import { titleFor } from "../../../utils/helpers/devices/title-for" +import { Modal } from "./Modal/Modal" interface Props { instanceId: InverterInstanceId @@ -28,12 +25,9 @@ interface Props { const Inverter = ({ instanceId, isVebusInverter, componentMode = "compact", compactBoxSize }: Props) => { const { locked } = useAppStore() // lock from theme settings - const source = isVebusInverter ? "vebus" : "inverter" - const { state, mode, voltage, current, power, productName, customName, updateMode } = useInverter(instanceId, source) - // Vebus inverters use mode 3 instead of 2 for ON. - const onMode = isVebusInverter ? INVERTER_MODE.VEBUS_ON : INVERTER_MODE.ON + const { state, mode, voltage, current, power, customName, productName } = useInverter(instanceId, source) const title = titleFor(customName, productName) const subTitle = translatedStateFor(state) @@ -44,22 +38,7 @@ const Inverter = ({ instanceId, isVebusInverter, componentMode = "compact", comp const [boxSize, setBoxSize] = useState({ width: 0, height: 0 }) const activeStyles = applyStyles(boxSize, defaultBoxStyles) - const [isModeModalOpen, setIsModeModalOpen] = useState(false) - const [modeForSubmission, setModeForSubmission] = useState(Number(mode)) - - useEffect(() => { - setModeForSubmission(Number(mode)) - }, [mode]) - - const closeModeModal = () => { - setIsModeModalOpen(false) - setModeForSubmission(Number(mode)) - } - - const submitMode = () => { - updateMode(modeForSubmission) - closeModeModal() - } + const [openModal, setOpenModal] = useState(false) if (componentMode === "compact" && compactBoxSize) { return ( @@ -98,62 +77,17 @@ const Inverter = ({ instanceId, isVebusInverter, componentMode = "compact", comp ]} /> - - {/* TODO Refactor to seperate modal file. */} - - {/* TODO Refactor to list item component, too much duplicate code. */} -
- - - {!isVebusInverter && ( - - )} -
-
+ open={openModal} + onClose={setOpenModal} + /> ) diff --git a/src/app/Marine2/components/boxes/Inverter/Modal/Modal.tsx b/src/app/Marine2/components/boxes/Inverter/Modal/Modal.tsx new file mode 100644 index 000000000..b53e602cd --- /dev/null +++ b/src/app/Marine2/components/boxes/Inverter/Modal/Modal.tsx @@ -0,0 +1,51 @@ +import { FC, useEffect, useState } from "react" +import { observer } from "mobx-react" +import { translate } from "react-i18nify" +import { InverterInstanceId, useInverter } from "@victronenergy/mfd-modules" +import DeviceSettingModal from "../../../ui/DeviceSettingModal/DeviceSettingModal" +import { Options } from "./Options/Options" + +interface Props { + instanceId: InverterInstanceId + isVebusInverter: boolean + title?: string + open: boolean + onClose: (arg: boolean) => void +} + +export const Modal: FC = observer(({ instanceId, isVebusInverter, title, open, onClose }) => { + const source = isVebusInverter ? "vebus" : "inverter" + + const { mode, updateMode } = useInverter(instanceId, source) + const [modeForSubmission, setModeForSubmission] = useState(Number(mode)) + + useEffect(() => { + setModeForSubmission(Number(mode)) + }, [mode]) + + const closeModeModal = () => { + onClose(!open) + setModeForSubmission(Number(mode)) + } + + const submitMode = () => { + updateMode(modeForSubmission) + closeModeModal() + } + + return ( + + + + ) +}) diff --git a/src/app/Marine2/components/boxes/Inverter/Modal/Options/Options.tsx b/src/app/Marine2/components/boxes/Inverter/Modal/Options/Options.tsx new file mode 100644 index 000000000..ddea60841 --- /dev/null +++ b/src/app/Marine2/components/boxes/Inverter/Modal/Options/Options.tsx @@ -0,0 +1,25 @@ +import { FC } from "react" +import { translate } from "react-i18nify" +import { inverterOptionsFor } from "../../../../../utils/helpers/devices/inverter-options-for" +import { OptionList } from "../../../../ui/OptionList/OptionList" +import { RadioOption } from "../../../../ui/OptionList/RadioOption/RadioOption" + +interface Props { + mode: number + onChange: (arg: number) => void + isVebus: boolean +} + +export const Options: FC = ({ mode, onChange, isVebus }) => { + const options = inverterOptionsFor(isVebus) + + return ( + + {options.map(({ key, value }) => ( + + {translate(key)} + + ))} + + ) +} diff --git a/src/app/Marine2/components/boxes/InverterCharger/Modal/Modal.tsx b/src/app/Marine2/components/boxes/InverterCharger/Modal/Modal.tsx index 538876074..c569eeefc 100644 --- a/src/app/Marine2/components/boxes/InverterCharger/Modal/Modal.tsx +++ b/src/app/Marine2/components/boxes/InverterCharger/Modal/Modal.tsx @@ -39,7 +39,7 @@ export const Modal: FC = observer(({ title, open, onClose }) => { onClose={closeModeModal} onSet={submitMode} > - + ) }) diff --git a/src/app/Marine2/components/boxes/InverterCharger/Modal/Options/Options.tsx b/src/app/Marine2/components/boxes/InverterCharger/Modal/Options/Options.tsx index d0bc60730..1d43db0c8 100644 --- a/src/app/Marine2/components/boxes/InverterCharger/Modal/Options/Options.tsx +++ b/src/app/Marine2/components/boxes/InverterCharger/Modal/Options/Options.tsx @@ -1,33 +1,20 @@ import { FC } from "react" import { translate } from "react-i18nify" -import { SYSTEM_MODE } from "app/Marine2/utils/constants" -import RadioButton from "../../../../ui/RadioButton" +import { OptionList } from "../../../../ui/OptionList/OptionList" +import { inverterChargerOptions } from "../../../../../utils/constants/mode-options" +import { RadioOption } from "../../../../ui/OptionList/RadioOption/RadioOption" interface Props { - modeForSubmission: number + mode: number onChange: (arg: number) => void } -export const Options: FC = ({ modeForSubmission, onChange }) => { - const options = [ - { key: "common.on", value: SYSTEM_MODE.ON }, - { key: "common.off", value: SYSTEM_MODE.OFF }, - { key: "common.chargerOnly", value: SYSTEM_MODE.CHARGER_ONLY }, - { key: "common.inverterOnly", value: SYSTEM_MODE.INVERTER_ONLY }, - ] - - return ( -
- {options.map((option) => ( - - ))} -
- ) -} +export const Options: FC = ({ mode, onChange }) => ( + + {inverterChargerOptions.map(({ key, value }) => ( + + {translate(key)} + + ))} + +) diff --git a/src/app/Marine2/components/ui/Modal/Modal.tsx b/src/app/Marine2/components/ui/Modal/Modal.tsx index 3249b9ac3..6d94731bd 100644 --- a/src/app/Marine2/components/ui/Modal/Modal.tsx +++ b/src/app/Marine2/components/ui/Modal/Modal.tsx @@ -10,7 +10,7 @@ interface Props { const Frame: FC = ({ children, open = true, onClose, className }) => { const classes = classnames( - "fixed inset-0 z-10 p-8 text-neutral-600 modal dark:text-white items-center justify-center", + "fixed inset-0 z-10 p-4 md:p-8 text-neutral-600 modal dark:text-white items-center justify-center", `${open ? "flex" : "hidden"}` // control visibility via `open` attribute (or render conditionally) ) return ( diff --git a/src/app/Marine2/components/ui/OptionList/OptionList.tsx b/src/app/Marine2/components/ui/OptionList/OptionList.tsx new file mode 100644 index 000000000..81fdc5141 --- /dev/null +++ b/src/app/Marine2/components/ui/OptionList/OptionList.tsx @@ -0,0 +1,5 @@ +import { FC } from "react" + +export const OptionList: FC = ({ children }) => ( +
{children}
+) \ No newline at end of file diff --git a/src/app/Marine2/components/ui/OptionList/RadioOption/RadioOption.tsx b/src/app/Marine2/components/ui/OptionList/RadioOption/RadioOption.tsx new file mode 100644 index 000000000..08e716b2b --- /dev/null +++ b/src/app/Marine2/components/ui/OptionList/RadioOption/RadioOption.tsx @@ -0,0 +1,16 @@ +import { FC } from "react" +import RadioButton from "../../RadioButton" + +interface Props { + mode: number + onChange: (arg: number) => void + key: string + value: number +} + +export const RadioOption: FC = ({ mode, onChange, value, children }) => ( + +) diff --git a/src/app/Marine2/utils/constants/mode-options.ts b/src/app/Marine2/utils/constants/mode-options.ts new file mode 100644 index 000000000..a61e68fbc --- /dev/null +++ b/src/app/Marine2/utils/constants/mode-options.ts @@ -0,0 +1,19 @@ +import { INVERTER_MODE, SYSTEM_MODE } from "../constants" + +export const inverterOptions = [ + { key: "common.on", value: INVERTER_MODE.ON }, + { key: "common.off", value: INVERTER_MODE.OFF }, + { key: "common.eco", value: INVERTER_MODE.ECO }, +] + +export const vebusInverOptions = [ + { key: "common.on", value: INVERTER_MODE.VEBUS_ON }, + { key: "common.off", value: INVERTER_MODE.OFF }, +] + +export const inverterChargerOptions = [ + { key: "common.on", value: SYSTEM_MODE.ON }, + { key: "common.off", value: SYSTEM_MODE.OFF }, + { key: "common.chargerOnly", value: SYSTEM_MODE.CHARGER_ONLY }, + { key: "common.inverterOnly", value: SYSTEM_MODE.INVERTER_ONLY }, +] diff --git a/src/app/Marine2/utils/helpers/devices/inverter-options-for.ts b/src/app/Marine2/utils/helpers/devices/inverter-options-for.ts new file mode 100644 index 000000000..90932b0af --- /dev/null +++ b/src/app/Marine2/utils/helpers/devices/inverter-options-for.ts @@ -0,0 +1,3 @@ +import { inverterOptions, vebusInverOptions } from "../../constants/mode-options" + +export const inverterOptionsFor = (isVebus: boolean) => (isVebus ? vebusInverOptions : inverterOptions)