From 852d3d6f7a5fd9a572cca8ab7eb2e42e255054ae Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 16 Aug 2021 14:34:58 +0800 Subject: [PATCH] feat: add SystemSettingsContext --- .../src/components/admin-layout/More.tsx | 39 +++++------------- .../src/components/admin-layout/SiteTitle.tsx | 40 ++++++++++++++++--- .../src/components/admin-layout/index.tsx | 28 +++++++------ 3 files changed, 61 insertions(+), 46 deletions(-) diff --git a/packages/client/src/components/admin-layout/More.tsx b/packages/client/src/components/admin-layout/More.tsx index 502dcccfb..133ad71d5 100644 --- a/packages/client/src/components/admin-layout/More.tsx +++ b/packages/client/src/components/admin-layout/More.tsx @@ -6,46 +6,29 @@ import { Resource } from '../../resource'; import { useRequest } from 'ahooks'; import { VisibleContext } from '../../context'; import { ISchema } from '../../schemas'; +import { SystemSettingsContext } from './SiteTitle'; const useResource = ({ onSuccess }) => { - const resource = Resource.make({ - resourceName: 'system_settings', - resourceKey: 1, - }); - const service = useRequest( - (params?: any) => { - return resource.get({ ...params, appends: ['logo'] }); - }, - { - formatResult: (result) => result?.data, - onSuccess, - manual: true, - }, - ); - const [visible] = useContext(VisibleContext); + const { service, resource } = useContext(SystemSettingsContext); useEffect(() => { - if (visible) { - service.run({}); - } - }, [visible]); + onSuccess(service.data); + }, []); return { resource, service, initialValues: service.data, ...service }; }; const useOkAction = () => { const form = useForm(); + const { service, resource } = useContext(SystemSettingsContext); return { async run() { - const resource = Resource.make({ - resourceName: 'system_settings', - resourceKey: 1, - }); - console.log('system_settings.values', form.values) + console.log('system_settings.values', form.values); await resource.save(form.values); - } - } -} + await service.refresh(); + }, + }; +}; const schema: ISchema = { type: 'void', @@ -102,7 +85,7 @@ const schema: ISchema = { }, }, }, - } + }, }, }; diff --git a/packages/client/src/components/admin-layout/SiteTitle.tsx b/packages/client/src/components/admin-layout/SiteTitle.tsx index 0b1d9cb45..76bd7a57a 100644 --- a/packages/client/src/components/admin-layout/SiteTitle.tsx +++ b/packages/client/src/components/admin-layout/SiteTitle.tsx @@ -2,15 +2,45 @@ import React, { useContext, useEffect } from 'react'; import { Button, Dropdown, Menu } from 'antd'; import { useHistory } from 'react-router-dom'; import { request } from '../../schemas'; +import { useRequest } from 'ahooks'; +import { Resource } from '../../resource'; +import { createContext } from 'react'; + +export const SystemSettingsContext = createContext(null); + +export function SystemSettingsProvider(props) { + const resource = Resource.make({ + resourceName: 'system_settings', + resourceKey: 1, + }); + const service = useRequest( + () => + resource.get({ + resourceKey: 1, + appends: ['logo'], + }), + { + formatResult: (data) => data?.data, + }, + ); + return ( + + {props.children} + + ); +} export const SiteTitle = () => { + const { service = {} } = useContext(SystemSettingsContext); + const { loading, data } = service; return (
- -
NocoBase
+ {!loading && data?.logo?.url && ( + + )} + {!loading && data.title && ( +
{data.title}
+ )}
); }; diff --git a/packages/client/src/components/admin-layout/index.tsx b/packages/client/src/components/admin-layout/index.tsx index 222b97e32..7f855814a 100644 --- a/packages/client/src/components/admin-layout/index.tsx +++ b/packages/client/src/components/admin-layout/index.tsx @@ -37,7 +37,7 @@ import { uid } from '@formily/shared'; import { Permissions } from './Permissions'; import { More } from './More'; import { UserInfo } from './UserInfo'; -import { SiteTitle } from './SiteTitle'; +import { SiteTitle, SystemSettingsProvider } from './SiteTitle'; function DesignableToggle() { const { designable, setDesignable } = useDesignableSwitchContext(); @@ -103,7 +103,7 @@ function LayoutWithMenu(props: LayoutWithMenuProps) { - + @@ -177,17 +177,19 @@ export function AdminLayout({ route, ...others }: any) { console.log('current?.title', current, current?.title, defaultSelectedKeys); return ( - - - - - - - + + + + + + + + + ); }