feat: add SystemSettingsContext

This commit is contained in:
chenos 2021-08-16 14:34:58 +08:00
parent b7e450dcf9
commit 852d3d6f7a
3 changed files with 61 additions and 46 deletions

View File

@ -6,46 +6,29 @@ import { Resource } from '../../resource';
import { useRequest } from 'ahooks'; import { useRequest } from 'ahooks';
import { VisibleContext } from '../../context'; import { VisibleContext } from '../../context';
import { ISchema } from '../../schemas'; import { ISchema } from '../../schemas';
import { SystemSettingsContext } from './SiteTitle';
const useResource = ({ onSuccess }) => { const useResource = ({ onSuccess }) => {
const resource = Resource.make({ const { service, resource } = useContext(SystemSettingsContext);
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);
useEffect(() => { useEffect(() => {
if (visible) { onSuccess(service.data);
service.run({}); }, []);
}
}, [visible]);
return { resource, service, initialValues: service.data, ...service }; return { resource, service, initialValues: service.data, ...service };
}; };
const useOkAction = () => { const useOkAction = () => {
const form = useForm(); const form = useForm();
const { service, resource } = useContext(SystemSettingsContext);
return { return {
async run() { async run() {
const resource = Resource.make({ console.log('system_settings.values', form.values);
resourceName: 'system_settings',
resourceKey: 1,
});
console.log('system_settings.values', form.values)
await resource.save(form.values); await resource.save(form.values);
} await service.refresh();
} },
} };
};
const schema: ISchema = { const schema: ISchema = {
type: 'void', type: 'void',
@ -102,7 +85,7 @@ const schema: ISchema = {
}, },
}, },
}, },
} },
}, },
}; };

View File

@ -2,15 +2,45 @@ import React, { useContext, useEffect } from 'react';
import { Button, Dropdown, Menu } from 'antd'; import { Button, Dropdown, Menu } from 'antd';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { request } from '../../schemas'; 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 (
<SystemSettingsContext.Provider value={{ service, resource }}>
{props.children}
</SystemSettingsContext.Provider>
);
}
export const SiteTitle = () => { export const SiteTitle = () => {
const { service = {} } = useContext(SystemSettingsContext);
const { loading, data } = service;
return ( return (
<div className={'site-info'}> <div className={'site-info'}>
<img {!loading && data?.logo?.url && (
className={'site-logo'} <img className={'site-logo'} src={data?.logo?.url} />
src={'https://www.nocobase.com/dist/images/logo-white.png'} )}
/> {!loading && data.title && (
<div className={'site-title'}>NocoBase</div> <div className={'site-title'}>{data.title}</div>
)}
</div> </div>
); );
}; };

View File

@ -37,7 +37,7 @@ import { uid } from '@formily/shared';
import { Permissions } from './Permissions'; import { Permissions } from './Permissions';
import { More } from './More'; import { More } from './More';
import { UserInfo } from './UserInfo'; import { UserInfo } from './UserInfo';
import { SiteTitle } from './SiteTitle'; import { SiteTitle, SystemSettingsProvider } from './SiteTitle';
function DesignableToggle() { function DesignableToggle() {
const { designable, setDesignable } = useDesignableSwitchContext(); const { designable, setDesignable } = useDesignableSwitchContext();
@ -103,7 +103,7 @@ function LayoutWithMenu(props: LayoutWithMenuProps) {
<DesignableToggle /> <DesignableToggle />
<Database /> <Database />
<Permissions /> <Permissions />
<More/> <More />
<UserInfo /> <UserInfo />
</Layout.Header> </Layout.Header>
<Layout> <Layout>
@ -177,17 +177,19 @@ export function AdminLayout({ route, ...others }: any) {
console.log('current?.title', current, current?.title, defaultSelectedKeys); console.log('current?.title', current, current?.title, defaultSelectedKeys);
return ( return (
<DesignableSwitchProvider> <SystemSettingsProvider>
<CollectionsProvider> <DesignableSwitchProvider>
<PageTitleProvider defaultPageTitle={current?.title}> <CollectionsProvider>
<LayoutWithMenu <PageTitleProvider defaultPageTitle={current?.title}>
defaultSelectedKeys={defaultSelectedKeys} <LayoutWithMenu
current={current} defaultSelectedKeys={defaultSelectedKeys}
schema={data} current={current}
/> schema={data}
</PageTitleProvider> />
</CollectionsProvider> </PageTitleProvider>
</DesignableSwitchProvider> </CollectionsProvider>
</DesignableSwitchProvider>
</SystemSettingsProvider>
); );
} }