diff --git a/src/pages/Bluetooth.tsx b/src/pages/Bluetooth.tsx index e212ff7..2e7f586 100644 --- a/src/pages/Bluetooth.tsx +++ b/src/pages/Bluetooth.tsx @@ -1,10 +1,12 @@ import { NavBar, SpinLoading, Toast } from 'antd-mobile'; import './Bluetooth.scss'; import { useEffect } from 'react'; -import { useAppSelector } from '../utils/hooks'; +import { useAppDispatch, useAppSelector } from '../utils/hooks'; import Bridge from '../utils/bridge'; +import { updateBleLinkStatus } from '../store/features/contextSlice'; export default function Bluetooth() { + const dispatch = useAppDispatch(); const contextState = useAppSelector((state) => state.context); useEffect(() => { @@ -22,6 +24,8 @@ export default function Bluetooth() { Bridge.connectPeripheral({ id }).then((res) => { if (!res.success) { Toast.show(res.message); + } else { + dispatch(updateBleLinkStatus({id, link: true})) } }); }; @@ -29,6 +33,8 @@ export default function Bluetooth() { Bridge.disconnectPeripheral().then((res) => { if (!res.success) { Toast.show(res.message); + } else { + dispatch(updateBleLinkStatus({id, link: false})) } }); }; diff --git a/src/store/features/contextSlice.ts b/src/store/features/contextSlice.ts index 4e71528..d46ddf2 100644 --- a/src/store/features/contextSlice.ts +++ b/src/store/features/contextSlice.ts @@ -100,6 +100,15 @@ export const contextSlice = createSlice({ state.bleList = action.payload; }, + updateBleLinkStatus: (state, action: PayloadAction<{ id: string; link: boolean }>) => { + state.bleList = state.bleList.map((ble) => { + if (ble.mac === action.payload.id) { + ble.linked = action.payload.link; + } + return ble; + }); + }, + updateSyncProgress: (state, action: PayloadAction) => { state.syncProgress = action.payload; }, @@ -118,6 +127,12 @@ export const contextSlice = createSlice({ }, }); -export const { updateOrg, updateDevice, updateRailTypeId, updateBleList, updateSyncProgress } = - contextSlice.actions; +export const { + updateOrg, + updateDevice, + updateRailTypeId, + updateBleList, + updateBleLinkStatus, + updateSyncProgress, +} = contextSlice.actions; export default contextSlice.reducer;