提交 a55004e2 authored 作者: vipcxj's avatar vipcxj

优化story代码结构

上级 f895b268
/* eslint-disable no-console */
import React from 'react';
import partial from 'lodash/partial';
import { storiesOf } from '@storybook/react';
......@@ -5,77 +6,17 @@ import { storiesOf } from '@storybook/react';
import ReactJson from 'react-json-view';
import { DatePicker, Form, Input, Modal, Button } from 'antd';
import dva from 'dva';
import createLoading from 'dva-loading';
import DsTable from '../src/components/table/dstable';
import TableInput from '../src/components/table/input-table';
import UCA from '../src/components/uca';
import { makeCancelable } from '../src/utils/promise';
import { login, authorize } from '../src/services/login';
import { validate } from '../src/services/login/password';
import { switchDomain } from '../src/services/domain';
import { setToken } from '../src/utils/auth';
import { encrypt } from '../src/utils/helper';
const loginIt = async (userName, password, domainPath) => {
const { tokenId } = await login({ type: 'userName', data: userName });
const authRequest = await validate(password, encrypt(tokenId));
await authorize(authRequest);
await setToken(tokenId);
await switchDomain(domainPath);
};
const wrap = () => Comp => (props) => {
const app = dva({});
app.use(createLoading({
effects: true,
}));
const { children, ...rest } = props;
app.router(() => <Comp app={app} {...rest}>{ children }</Comp>);
return app.start()();
};
const lazy = action => (Comp) => {
class Lazy extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
loading: true,
};
}
componentDidMount() {
this.action = makeCancelable(action());
this.action.run(promise => promise.then(() => {
this.setState({
loading: false,
});
}));
}
componentWillUnmount() {
this.action.cancel();
}
render() {
if (this.state.loading) {
return <div>加载中...</div>;
} else {
const { children, ...rest } = this.props;
return (
<Comp {...rest}>
{ children }
</Comp>
);
}
}
}
return Lazy;
};
import dva from './lib/dva';
import lazy from './lib/lazy';
import loginIt from './lib/login';
storiesOf('a', module)
.add('1', () => {
const Comp = lazy(partial(loginIt, 'admin', 'admin', '/'))(wrap()(DsTable));
const Comp = lazy(partial(loginIt, 'admin', 'admin', '/'))(dva()(DsTable));
const coordinate = {
containerType: 'module',
containerName: 'test',
......
import createLoading from 'dva-loading';
import dva from 'dva';
import React from 'react';
export default function (opt) {
return Comp => (props) => {
const app = dva(opt || {});
app.use(createLoading({
effects: true,
}));
const { children, ...rest } = props;
app.router(() => <Comp app={app} {...rest}>{ children }</Comp>);
return app.start()();
};
}
import React from 'react';
import { makeCancelable } from '../../src/utils/promise';
export default function (action) {
return (Comp) => {
class Lazy extends React.Component {
constructor() {
super();
this.state = {
loading: true,
};
}
componentDidMount() {
this.action = makeCancelable(action());
this.action.run(promise => promise.then(() => {
this.setState({
loading: false,
});
}));
}
componentWillUnmount() {
this.action.cancel();
}
render() {
if (this.state.loading) {
return <div>加载中...</div>;
} else {
const { children, ...rest } = this.props;
return (
<Comp {...rest}>
{ children }
</Comp>
);
}
}
}
return Lazy;
};
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论