feat: add SystemSettingsContext
This commit is contained in:
parent
b7e450dcf9
commit
852d3d6f7a
@ -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 = {
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -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 (
|
||||
<SystemSettingsContext.Provider value={{ service, resource }}>
|
||||
{props.children}
|
||||
</SystemSettingsContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export const SiteTitle = () => {
|
||||
const { service = {} } = useContext(SystemSettingsContext);
|
||||
const { loading, data } = service;
|
||||
return (
|
||||
<div className={'site-info'}>
|
||||
<img
|
||||
className={'site-logo'}
|
||||
src={'https://www.nocobase.com/dist/images/logo-white.png'}
|
||||
/>
|
||||
<div className={'site-title'}>NocoBase</div>
|
||||
{!loading && data?.logo?.url && (
|
||||
<img className={'site-logo'} src={data?.logo?.url} />
|
||||
)}
|
||||
{!loading && data.title && (
|
||||
<div className={'site-title'}>{data.title}</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
@ -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();
|
||||
@ -177,6 +177,7 @@ export function AdminLayout({ route, ...others }: any) {
|
||||
console.log('current?.title', current, current?.title, defaultSelectedKeys);
|
||||
|
||||
return (
|
||||
<SystemSettingsProvider>
|
||||
<DesignableSwitchProvider>
|
||||
<CollectionsProvider>
|
||||
<PageTitleProvider defaultPageTitle={current?.title}>
|
||||
@ -188,6 +189,7 @@ export function AdminLayout({ route, ...others }: any) {
|
||||
</PageTitleProvider>
|
||||
</CollectionsProvider>
|
||||
</DesignableSwitchProvider>
|
||||
</SystemSettingsProvider>
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user