From 1a2c443c8a624db0f3df07b8cde7cffe69f1051c Mon Sep 17 00:00:00 2001 From: RadoslavDimchev Date: Fri, 5 Jan 2024 10:33:54 +0200 Subject: [PATCH] feat: use states and create new handlers with reducers --- .../ConnectViaIP/AddressAndPortInputs.tsx | 33 +++++++++++-------- src/redux/PairDevice/slice.ts | 12 +++---- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/pages/PairDevice/ConnectViaIP/AddressAndPortInputs.tsx b/src/pages/PairDevice/ConnectViaIP/AddressAndPortInputs.tsx index f849a373..aa75d733 100644 --- a/src/pages/PairDevice/ConnectViaIP/AddressAndPortInputs.tsx +++ b/src/pages/PairDevice/ConnectViaIP/AddressAndPortInputs.tsx @@ -1,5 +1,4 @@ import { Checkbox, Input, Text } from '@status-im/components' -import { useState } from 'react' import { useDispatch, useSelector } from 'react-redux' import { Stack, Switch, XStack, YStack } from 'tamagui' @@ -11,6 +10,8 @@ type AddressAndPortInputsProps = { isAdvanced?: boolean address: string port?: string + isSwitchOn?: boolean + isChecked?: boolean } const AddressAndPortInputs = ({ @@ -19,18 +20,24 @@ const AddressAndPortInputs = ({ portType, address, port, + isSwitchOn, + isChecked, }: AddressAndPortInputsProps) => { - const [isProtocolSwitchOn, setIsProtocolSwitchOn] = useState(true) - const [isChecked, setIsChecked] = useState(true) - const { beaconPort, vcPort } = useSelector((state: RootState) => state.pairDevice) + const { beaconPort, vcPort, isNodeChecked, isNodeSwitchOn } = useSelector( + (state: RootState) => state.pairDevice, + ) const dispatch = useDispatch() + const isSwitchOnResult = isAdvanced ? isSwitchOn : isNodeSwitchOn + const switchStyle = isSwitchOnResult + ? { backgroundColor: '#2A4AF5' } + : { backgroundColor: 'grey' } - const onProtocolSwitchChangeHandler = () => { - setIsProtocolSwitchOn(state => !state) + const onSwitchChange = (value: boolean) => { + dispatch({ type: 'pairDevice/setIsSwitchOn', payload: { value, switchType: addressType } }) } - const onCheckboxChangeHandler = () => { - setIsChecked(state => !state) + const onCheckboxChange = (value: boolean) => { + dispatch({ type: 'pairDevice/setIsChecked', payload: { value, checkType: addressType } }) } const onPortChange = (value: string, portType: string) => { @@ -58,9 +65,9 @@ const AddressAndPortInputs = ({ diff --git a/src/redux/PairDevice/slice.ts b/src/redux/PairDevice/slice.ts index fbf8a92a..41b847ca 100644 --- a/src/redux/PairDevice/slice.ts +++ b/src/redux/PairDevice/slice.ts @@ -20,12 +20,12 @@ const initialState: PairDeviceStateType = { nodeAddress: 'http://124.0.0.1', beaconAddress: 'http://124.0.0.1', vcAddress: 'http://124.0.0.1', - isNodeSwitchOn: false, - isBeaconSwitchOn: false, - isVcSwitchOn: false, - isNodeChecked: false, - isBeaconChecked: false, - isVcChecked: false, + isNodeSwitchOn: true, + isBeaconSwitchOn: true, + isVcSwitchOn: true, + isNodeChecked: true, + isBeaconChecked: true, + isVcChecked: true, } const pairDeviceSlice = createSlice({