From c5559845d27cdeba278c9969330c474e26db9ac6 Mon Sep 17 00:00:00 2001 From: monster Date: Fri, 9 Jun 2023 22:32:41 +0800 Subject: [PATCH] feat: complete settings --- src/core/storage.ts | 7 ++ src/pages/settings/application.tsx | 139 ++++++++++++++++++++++++++++- src/pages/settings/main.tsx | 8 +- 3 files changed, 150 insertions(+), 4 deletions(-) diff --git a/src/core/storage.ts b/src/core/storage.ts index a9fd9d0..34267f8 100644 --- a/src/core/storage.ts +++ b/src/core/storage.ts @@ -6,6 +6,13 @@ export enum StorageKeys { 'hamsterPassword' = 'hamsterPassword', } +export const defaultSettingValue = { + [StorageKeys.hamsterbaseURL]: 'https://memo-talk.onrender.com/', + [StorageKeys.hamsterUsername]: '', + [StorageKeys.hamsterPassword]: '', +}; +export type SettingType = typeof defaultSettingValue; + export interface ISettingService { get(key: string, defaultValue: V): Promise; diff --git a/src/pages/settings/application.tsx b/src/pages/settings/application.tsx index 466fe60..b66e477 100644 --- a/src/pages/settings/application.tsx +++ b/src/pages/settings/application.tsx @@ -1,10 +1,143 @@ -import { NavBar } from 'antd-mobile'; -import React from 'react'; +import { Button, Form, Input, List, NavBar, Popup, Space } from 'antd-mobile'; +import { UploadOutline } from 'antd-mobile-icons'; +import React, { useEffect, useState } from 'react'; +import { + ISettingService, + SettingType, + StorageKeys, + defaultSettingValue, +} from '../../core/storage'; + +const CloudSync: React.FC<{ + visible: boolean; + setVisible: (value: boolean) => void; + setting: SettingType; + onSave: (url: { + url: string; + name: string; + password: string; + }) => Promise; +}> = (props) => { + const [form] = Form.useForm(); + const onSubmit = async () => { + const values = form.getFieldsValue(); + await props.onSave(values); + props.setVisible(false); + }; + return ( + { + props.setVisible(false); + }} + bodyStyle={{ + borderTopLeftRadius: '8px', + borderTopRightRadius: '8px', + minHeight: '40vh', + }} + > +
+ 保存 + + } + > + + + + + + + + + +
+
+ ); +}; + +export const App: React.FC<{ + settingService: ISettingService; +}> = (props) => { + const [visible, setVisible] = useState(false); + const [setting, setSettings] = useState(null); + + useEffect(() => { + props.settingService.readConfig(defaultSettingValue).then((res) => { + setSettings(res); + }); + }, [props.settingService]); + + const isEnable = + setting && + setting[StorageKeys.hamsterbaseURL] && + setting[StorageKeys.hamsterUsername] && + setting[StorageKeys.hamsterPassword]; + + if (!setting) { + return null; + } -export const App: React.FC = () => { return (
window.history.back()}>设置 + + + } + extra={isEnable ? '已开启' : '未开启'} + clickable + onClick={() => setVisible(true)} + > + 云同步 + + + { + const newSetting: SettingType = { + ...setting, + [StorageKeys.hamsterbaseURL]: value.url, + [StorageKeys.hamsterPassword]: value.password, + [StorageKeys.hamsterUsername]: value.name, + }; + setSettings(newSetting); + await props.settingService.set( + StorageKeys.hamsterbaseURL, + newSetting['hamsterbaseURL'] + ); + await props.settingService.set( + StorageKeys.hamsterUsername, + value.name + ); + await props.settingService.set( + StorageKeys.hamsterPassword, + value.password + ); + }} + />
); }; diff --git a/src/pages/settings/main.tsx b/src/pages/settings/main.tsx index 94418c4..93f2602 100644 --- a/src/pages/settings/main.tsx +++ b/src/pages/settings/main.tsx @@ -1,9 +1,15 @@ import React from 'react'; import ReactDOM from 'react-dom/client'; import { App } from './application.tsx'; +import { + ISettingService, + IndexedDBSettingService, +} from '../../core/storage.ts'; + +const settingService: ISettingService = new IndexedDBSettingService(); ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( - + );