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 { 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 = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -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();
|
||||||
@ -177,6 +177,7 @@ export function AdminLayout({ route, ...others }: any) {
|
|||||||
console.log('current?.title', current, current?.title, defaultSelectedKeys);
|
console.log('current?.title', current, current?.title, defaultSelectedKeys);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<SystemSettingsProvider>
|
||||||
<DesignableSwitchProvider>
|
<DesignableSwitchProvider>
|
||||||
<CollectionsProvider>
|
<CollectionsProvider>
|
||||||
<PageTitleProvider defaultPageTitle={current?.title}>
|
<PageTitleProvider defaultPageTitle={current?.title}>
|
||||||
@ -188,6 +189,7 @@ export function AdminLayout({ route, ...others }: any) {
|
|||||||
</PageTitleProvider>
|
</PageTitleProvider>
|
||||||
</CollectionsProvider>
|
</CollectionsProvider>
|
||||||
</DesignableSwitchProvider>
|
</DesignableSwitchProvider>
|
||||||
|
</SystemSettingsProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user