From ff2005ba5b5cd20c798e91c4874e3f91dd27a447 Mon Sep 17 00:00:00 2001 From: zhangjiming Date: Thu, 3 Apr 2025 18:13:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=20mock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/Setting.tsx | 41 ++++++++++++++++++++------ src/store/features/contextSlice.ts | 59 ++++++++++++++++++++++++++++++-------- 2 files changed, 80 insertions(+), 20 deletions(-) diff --git a/src/pages/Setting.tsx b/src/pages/Setting.tsx index 914da8a..960f011 100644 --- a/src/pages/Setting.tsx +++ b/src/pages/Setting.tsx @@ -1,21 +1,42 @@ import { NavBar } from 'antd-mobile'; import icon_arr_r from '../assets/icon_arr_s_r.svg'; -import { ChangeEvent, useState } from 'react'; +import { ChangeEvent, useEffect, useRef, useState } from 'react'; import { useNavigate } from 'react-router'; +import { useAppDispatch, useAppSelector } from '../utils/hooks'; +import { fetchConfig, saveConfig } from '../store/features/contextSlice'; export default function Setting() { const navigate = useNavigate(); - const [addr, setAddr] = useState(''); + const dispatch = useAppDispatch(); + const context = useAppSelector((state) => state.context); + + const addrInput = useRef(null); + // const [addr, setAddr] = useState(''); // const [port, setPort] = useState(80); - const onAddrChange = (evt: ChangeEvent) => { - setAddr(evt.target.value); - }; + // const onAddrChange = (evt: ChangeEvent) => { + // setAddr(evt.target.value); + // }; // const onPortChange = (evt: ChangeEvent) => { // if (/^\d*$/.test(evt.target.value)) { // setPort(+evt.target.value); // } // }; + useEffect(() => { + dispatch(fetchConfig()); + }, [dispatch]); + + useEffect(() => { + if (addrInput.current) { + addrInput.current!.value = context.setting.server; + } + }, [context.setting.server]); + + const onSaveClick = () => { + const server = addrInput.current!.value; + dispatch(saveConfig({ server })); + }; + const back = () => navigate(-1); return (
@@ -33,16 +54,20 @@ export default function Setting() {
服务地址
-
保存
+
+ 保存 +
); diff --git a/src/store/features/contextSlice.ts b/src/store/features/contextSlice.ts index d0f5041..865bc3b 100644 --- a/src/store/features/contextSlice.ts +++ b/src/store/features/contextSlice.ts @@ -1,5 +1,11 @@ -import { createSlice, PayloadAction } from '@reduxjs/toolkit'; -import { BleList, PeripheralStatus, SyncItemFinish, SyncProgress } from '../../services/mobileWsType'; +import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'; +import { + BleList, + PeripheralStatus, + SyncItemFinish, + SyncProgress, +} from '../../services/mobileWsType'; +import Bridge from '../../utils/bridge'; interface ContextState { device: PeripheralStatus['data']; @@ -8,9 +14,13 @@ interface ContextState { currGWDCode: string; // 工务段 currXMCode: string; // 线名 - bleList: BleList["data"]; - syncProgress: SyncProgress["data"]; - syncItems: Array + bleList: BleList['data']; + syncProgress: SyncProgress['data']; + syncItems: Array; + + setting: { + server: string; + }; } const orgGwdXmStr = localStorage.getItem('org_gwd_xm'); @@ -41,9 +51,27 @@ const initialState: ContextState = { total: 0, finish: true, }, - syncItems:[] + syncItems: [], + + setting: { + server: '', + }, }; +export const fetchConfig = createAsyncThunk('context/fetchConfig', async () => { + const conf = await Bridge.getConfig(); + return conf.success ? conf.data : null; +}); + +export const saveConfig = createAsyncThunk( + 'context/saveConfig', + async (param: { server: string }, thunkAPI) => { + const res = await Bridge.saveConfig(param); + res.success && thunkAPI.dispatch(fetchConfig()); + return res + } +); + export const contextSlice = createSlice({ name: 'context', initialState, @@ -52,7 +80,7 @@ export const contextSlice = createSlice({ state.currOrgCode = action.payload[0]; state.currGWDCode = action.payload[1]; state.currXMCode = action.payload[2]; - localStorage.setItem('org_gwd_xm', action.payload.join(',')) + localStorage.setItem('org_gwd_xm', action.payload.join(',')); }, updateDevice: (state, action: PayloadAction) => { @@ -63,16 +91,23 @@ export const contextSlice = createSlice({ state.currRailTypeId = action.payload; }, - updateBleList:(state, action: PayloadAction) => { - state.bleList = action.payload + updateBleList: (state, action: PayloadAction) => { + state.bleList = action.payload; }, - updateSyncProgress: (state, action: PayloadAction) => { + updateSyncProgress: (state, action: PayloadAction) => { state.syncProgress = action.payload; }, - + }, + extraReducers: (builder) => { + builder.addCase(fetchConfig.fulfilled, (state, action) => { + if (action.payload) { + state.setting = action.payload; + } + }); }, }); -export const { updateOrg, updateDevice, updateRailTypeId, updateBleList, updateSyncProgress } = contextSlice.actions; +export const { updateOrg, updateDevice, updateRailTypeId, updateBleList, updateSyncProgress } = + contextSlice.actions; export default contextSlice.reducer;