From 6413bf342ae3ff930083a688595377b48e9b9781 Mon Sep 17 00:00:00 2001 From: QuentinHsu Date: Wed, 6 Mar 2024 17:57:53 +0800 Subject: [PATCH 1/2] refactor(OtherSetting): change UI, improve interaction --- web/src/components/OtherSetting.js | 241 ++++++++++++++++------------- 1 file changed, 136 insertions(+), 105 deletions(-) diff --git a/web/src/components/OtherSetting.js b/web/src/components/OtherSetting.js index c3d56ec..cf4cb9f 100644 --- a/web/src/components/OtherSetting.js +++ b/web/src/components/OtherSetting.js @@ -1,15 +1,15 @@ -import React, { useEffect, useState } from 'react'; -import { Button, Divider, Form, Grid, Header, Message, Modal } from 'semantic-ui-react'; +import React, { useEffect, useRef, useState } from 'react'; +import { Col, Row , Form, Button, Banner } from '@douyinfe/semi-ui'; import { API, showError, showSuccess } from '../helpers'; import { marked } from 'marked'; const OtherSetting = () => { let [inputs, setInputs] = useState({ - Footer: '', Notice: '', - About: '', SystemName: '', Logo: '', + Footer: '', + About: '', HomePageContent: '' }); let [loading, setLoading] = useState(false); @@ -19,25 +19,6 @@ const OtherSetting = () => { content: '' }); - const getOptions = async () => { - const res = await API.get('/api/option/'); - const { success, message, data } = res.data; - if (success) { - let newInputs = {}; - data.forEach((item) => { - if (item.key in inputs) { - newInputs[item.key] = item.value; - } - }); - setInputs(newInputs); - } else { - showError(message); - } - }; - - useEffect(() => { - getOptions().then(); - }, []); const updateOption = async (key, value) => { setLoading(true); @@ -54,33 +35,69 @@ const OtherSetting = () => { setLoading(false); }; - const handleInputChange = async (e, { name, value }) => { + const [loadingInput, setLoadingInput] = useState({ + Notice: false, + SystemName: false, + Logo: false, + HomePageContent: false, + About: false, + Footer: false + }); + const handleInputChange = async (value, e) => { + const name = e.target.id; setInputs((inputs) => ({ ...inputs, [name]: value })); }; + // 通用设置 + const formAPISettingGeneral = useRef(); + // 通用设置 - Notice const submitNotice = async () => { + setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: true })); await updateOption('Notice', inputs.Notice); + showSuccess('公告已更新'); + setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: false })); }; - - const submitFooter = async () => { - await updateOption('Footer', inputs.Footer); - }; - + // 个性化设置 + const formAPIPersonalization = useRef(); + // 个性化设置 - SystemName const submitSystemName = async () => { + setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: true })); await updateOption('SystemName', inputs.SystemName); + showSuccess('系统名称已更新'); + setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: false })); }; + // Logo const submitLogo = async () => { + setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: true })); await updateOption('Logo', inputs.Logo); + showSuccess('Logo 已更新'); + setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: false })); }; - - const submitAbout = async () => { - await updateOption('About', inputs.About); - }; - + // 首页内容 const submitOption = async (key) => { + setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: true })); await updateOption(key, inputs[key]); + showSuccess('首页内容已更新'); + setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: false })); }; + // 关于 + const submitAbout = async () => { + setLoadingInput((loadingInput) => ({ ...loadingInput, About: true })); + await updateOption('About', inputs.About); + showSuccess('关于内容已更新'); + setLoadingInput((loadingInput) => ({ ...loadingInput, About: false })); + }; + // 页脚 + const submitFooter = async () => { + setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: true })); + await updateOption('Footer', inputs.Footer); + showSuccess('页脚内容已更新'); + setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: false })); + }; + + + const openGitHubRelease = () => { window.location = @@ -102,82 +119,96 @@ const OtherSetting = () => { setShowUpdateModal(true); } }; + const getOptions = async () => { + const res = await API.get('/api/option/'); + const { success, message, data } = res.data; + if (success) { + let newInputs = {}; + data.forEach((item) => { + if (item.key in inputs) { + newInputs[item.key] = item.value; + } + }); + setInputs(newInputs); + formAPISettingGeneral.current.setValues(newInputs); + formAPIPersonalization.current.setValues(newInputs); + } else { + showError(message); + } + }; + + useEffect( () => { + getOptions(); + }, []); + return ( - - -
-
通用设置
- {/*检查更新*/} - + + + {/* 通用设置 */} + formAPISettingGeneral.current = formAPI} style={{marginBottom: 15}}> + - - 保存公告 - -
个性化设置
- - - - 设置系统名称 - - - - 设置 Logo - - - - submitOption('HomePageContent')}>保存首页内容 - - - - 保存关于 - 移除 One API 的版权标识必须首先获得授权,项目维护需要花费大量精力,如果本项目对你有意义,请主动支持本项目。 - - - - 设置页脚 + /> + + -
+ {/* 个性化设置 */} +
formAPIPersonalization.current = formAPI} style={{marginBottom: 15}}> + + + + + + + + + + {/* */} + + + + +
+ {/* setShowUpdateModal(false)}*/} {/* onOpen={() => setShowUpdateModal(true)}*/} @@ -200,7 +231,7 @@ const OtherSetting = () => { {/* />*/} {/* */} {/**/} -
+ ); }; From 931d22c96f6fe045b414c14d90f0993fd6ceb1fd Mon Sep 17 00:00:00 2001 From: QuentinHsu Date: Wed, 6 Mar 2024 18:26:22 +0800 Subject: [PATCH 2/2] perf(OtherSetting): code logic --- web/src/components/OtherSetting.js | 92 +++++++++++++++++++++--------- 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/web/src/components/OtherSetting.js b/web/src/components/OtherSetting.js index cf4cb9f..ca3ec7e 100644 --- a/web/src/components/OtherSetting.js +++ b/web/src/components/OtherSetting.js @@ -52,48 +52,84 @@ const OtherSetting = () => { const formAPISettingGeneral = useRef(); // 通用设置 - Notice const submitNotice = async () => { - setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: true })); - await updateOption('Notice', inputs.Notice); - showSuccess('公告已更新'); - setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: false })); + try { + setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: true })); + await updateOption('Notice', inputs.Notice); + showSuccess('公告已更新'); + } catch (error) { + console.error("公告更新失败", error); + showError("公告更新失败") + } finally { + setLoadingInput((loadingInput) => ({ ...loadingInput, Notice: false })); + } }; // 个性化设置 const formAPIPersonalization = useRef(); // 个性化设置 - SystemName const submitSystemName = async () => { - setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: true })); - await updateOption('SystemName', inputs.SystemName); - showSuccess('系统名称已更新'); - setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: false })); + try { + setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: true })); + await updateOption('SystemName', inputs.SystemName); + showSuccess('系统名称已更新'); + } catch (error) { + console.error("系统名称更新失败", error); + showError("系统名称更新失败") + } finally { + setLoadingInput((loadingInput) => ({ ...loadingInput, SystemName: false })); + } }; - // Logo + // 个性化设置 - Logo const submitLogo = async () => { - setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: true })); - await updateOption('Logo', inputs.Logo); - showSuccess('Logo 已更新'); - setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: false })); + try { + setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: true })); + await updateOption('Logo', inputs.Logo); + showSuccess('Logo 已更新'); + } catch (error) { + console.error("Logo 更新失败", error); + showError("Logo 更新失败") + } finally { + setLoadingInput((loadingInput) => ({ ...loadingInput, Logo: false })); + } }; - // 首页内容 + // 个性化设置 - 首页内容 const submitOption = async (key) => { - setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: true })); - await updateOption(key, inputs[key]); - showSuccess('首页内容已更新'); - setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: false })); + try { + setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: true })); + await updateOption(key, inputs[key]); + showSuccess('首页内容已更新'); + } catch (error) { + console.error("首页内容更新失败", error); + showError("首页内容更新失败") + } finally { + setLoadingInput((loadingInput) => ({ ...loadingInput, HomePageContent: false })); + } }; - // 关于 + // 个性化设置 - 关于 const submitAbout = async () => { - setLoadingInput((loadingInput) => ({ ...loadingInput, About: true })); - await updateOption('About', inputs.About); - showSuccess('关于内容已更新'); - setLoadingInput((loadingInput) => ({ ...loadingInput, About: false })); + try { + setLoadingInput((loadingInput) => ({ ...loadingInput, About: true })); + await updateOption('About', inputs.About); + showSuccess('关于内容已更新'); + } catch (error) { + console.error("关于内容更新失败", error); + showError("关于内容更新失败"); + } finally { + setLoadingInput((loadingInput) => ({ ...loadingInput, About: false })); + } }; - // 页脚 + // 个性化设置 - 页脚 const submitFooter = async () => { - setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: true })); - await updateOption('Footer', inputs.Footer); - showSuccess('页脚内容已更新'); - setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: false })); + try { + setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: true })); + await updateOption('Footer', inputs.Footer); + showSuccess('页脚内容已更新'); + } catch (error) { + console.error("页脚内容更新失败", error); + showError("页脚内容更新失败"); + } finally { + setLoadingInput((loadingInput) => ({ ...loadingInput, Footer: false })); + } };