2020-11-11 15:23:39 +08:00
|
|
|
|
2020-11-21 23:49:59 +08:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-11-19 21:12:15 +08:00
|
|
|
import api from '@/api-client';
|
|
|
|
import { useRequest } from 'umi';
|
|
|
|
import { Spin } from '@nocobase/client';
|
|
|
|
import { SimpleTable } from './SimpleTable';
|
2020-11-11 15:23:39 +08:00
|
|
|
import { Table } from './Table';
|
2020-12-08 21:19:39 +08:00
|
|
|
import { Form, DrawerForm, FilterForm } from './Form/index';
|
2020-11-11 15:23:39 +08:00
|
|
|
import { Details } from './Details';
|
2020-11-29 16:26:53 +08:00
|
|
|
import './style.less';
|
|
|
|
import { Login } from './Form/Login';
|
|
|
|
import { Register } from './Form/Register';
|
2020-11-11 15:23:39 +08:00
|
|
|
|
|
|
|
const TEMPLATES = new Map<string, any>();
|
|
|
|
|
|
|
|
export function registerView(template: string, Template: any) {
|
|
|
|
TEMPLATES.set(template, Template);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getViewTemplate(template: string) {
|
|
|
|
return TEMPLATES.get(template);
|
|
|
|
}
|
|
|
|
|
2020-12-08 21:19:39 +08:00
|
|
|
registerView('FilterForm', FilterForm)
|
2020-11-11 15:23:39 +08:00
|
|
|
registerView('DrawerForm', DrawerForm);
|
|
|
|
registerView('PermissionForm', DrawerForm);
|
|
|
|
registerView('Form', Form);
|
|
|
|
registerView('Table', Table);
|
|
|
|
registerView('SimpleTable', SimpleTable);
|
|
|
|
registerView('Details', Details);
|
2020-11-29 16:26:53 +08:00
|
|
|
registerView('Login', Login);
|
|
|
|
registerView('Register', Register);
|
2020-11-11 15:23:39 +08:00
|
|
|
|
2020-11-19 21:12:15 +08:00
|
|
|
export interface ViewProps {
|
|
|
|
resourceName: string;
|
|
|
|
resourceKey?: string | number;
|
|
|
|
associatedName?: string;
|
|
|
|
associatedKey?: string | number;
|
|
|
|
viewName?: string;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ViewFactory(props: ViewProps) {
|
|
|
|
const {
|
|
|
|
associatedName,
|
|
|
|
associatedKey,
|
|
|
|
resourceName,
|
2020-12-25 16:15:58 +08:00
|
|
|
resourceTarget,
|
2020-11-19 21:12:15 +08:00
|
|
|
viewName,
|
2020-12-04 21:09:39 +08:00
|
|
|
mode,
|
2020-11-19 21:12:15 +08:00
|
|
|
reference,
|
2020-12-28 11:11:49 +08:00
|
|
|
isAssociationView = false,
|
2020-11-19 21:12:15 +08:00
|
|
|
} = props;
|
2020-12-13 00:09:25 +08:00
|
|
|
console.log('propspropspropspropspropspropsprops', props);
|
2020-11-19 21:12:15 +08:00
|
|
|
const { data = {}, loading } = useRequest(() => {
|
|
|
|
const params = {
|
|
|
|
resourceKey: viewName,
|
2020-12-13 00:09:25 +08:00
|
|
|
values: {
|
|
|
|
associatedKey: associatedKey,
|
|
|
|
associatedName: associatedName,
|
|
|
|
mode
|
|
|
|
},
|
2020-11-19 21:12:15 +08:00
|
|
|
};
|
2020-12-25 16:15:58 +08:00
|
|
|
return api.resource(resourceTarget||resourceName).getView(params);
|
2020-11-19 21:12:15 +08:00
|
|
|
}, {
|
2020-12-25 16:15:58 +08:00
|
|
|
refreshDeps: [associatedName, resourceTarget, resourceName, viewName],
|
2020-11-11 15:23:39 +08:00
|
|
|
});
|
|
|
|
if (loading) {
|
|
|
|
return <Spin/>;
|
|
|
|
}
|
2020-12-28 11:11:49 +08:00
|
|
|
let { template } = data;
|
|
|
|
if (isAssociationView && template === 'Table') {
|
|
|
|
template = 'SimpleTable';
|
|
|
|
}
|
2020-11-11 15:23:39 +08:00
|
|
|
const Template = getViewTemplate(template);
|
2020-11-19 21:12:15 +08:00
|
|
|
return Template && <Template {...props} ref={reference} schema={data}/>;
|
2020-11-11 15:23:39 +08:00
|
|
|
}
|