Skip to content

Commit

Permalink
chore(rematch-tsx): update rematch.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 23, 2021
1 parent b85af33 commit 158b217
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 65 deletions.
2 changes: 1 addition & 1 deletion example/rematch-tsx/src/models/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface RootModel extends Models<RootModel>, FullModel {

type FullModel = ExtraModelsFromLoading<RootModel>;

export const models = { global } as RootModel;
export const models = { global, login } as RootModel;
export const store = init<RootModel, FullModel>({
models,
plugins: [loadingPlugin()],
Expand Down
3 changes: 3 additions & 0 deletions example/rematch-tsx/src/models/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default createModel<RootModel>()({
updateState: (state, payload: LoginState): LoginState => ({ ...state, ...payload }),
},
effects: (dispatch) => ({
async logout(_) {
// await logout();
},
async submit(payload: LoginState['userData']) {
await login({ ...payload } as LoginState['userData']);
history.push('/');
Expand Down
4 changes: 1 addition & 3 deletions example/rematch-tsx/src/pages/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Button } from 'uiw';
import { DefaultProps } from '../../';

function Home(props: DefaultProps) {
export default function Home(props: DefaultProps) {
const { history } = props || {};
return (
<div>
Expand All @@ -11,5 +11,3 @@ function Home(props: DefaultProps) {
</div>
);
}

export default Home;
34 changes: 17 additions & 17 deletions example/rematch-tsx/src/pages/login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Form, Row, Col, Button, Input, Checkbox } from 'uiw';
import { connect } from 'react-redux';
import { useSelector, useDispatch } from 'react-redux';
import logo from '../../assets/logo-dark.svg';
import styles from './index.module.less';
import { RootState, Dispatch } from '../../models';
import { DefaultProps } from '../../';

const mapState = ({ login, loading }: RootState) => ({
loading: loading.effects.login.submit,
account: login.userData,
});
type Props = DefaultProps;

const mapDispatch: any = (dispatch: Dispatch) => ({
submit: dispatch.login.submit,
});
export default function Login(props: Props) {
const { loading, token } = useSelector(({ loading, login }: RootState) => ({
loading: loading.effects.login.submit,
account: login.userData,
token: login.token,
}));

type StateProps = ReturnType<typeof mapState>;
type DispatchProps = ReturnType<typeof mapDispatch>;
type Props = StateProps & DispatchProps & DefaultProps;
const dispatch = useDispatch<Dispatch>();
useEffect(() => {
if (token) {
dispatch.login.logout();
}
// eslint-disable-next-line
}, []);

function Login(props: Props) {
const { loading } = props;
return (
<Row justify="center" align="middle" style={{ height: '100%' }}>
<Col span="5">
Expand All @@ -38,7 +40,7 @@ function Login(props: Props) {
err.filed = errorObj;
throw err;
}
props.submit({
dispatch.login.submit({
username: current.username,
password: current.password,
terms: current.terms,
Expand Down Expand Up @@ -109,5 +111,3 @@ function Login(props: Props) {
</Row>
);
}

export default connect(mapState, mapDispatch)(Login);
113 changes: 69 additions & 44 deletions example/rematch-tsx/src/routes/Controller.tsx
Original file line number Diff line number Diff line change
@@ -1,51 +1,76 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Switch, Route } from 'react-router-dom';
import { connect } from 'react-redux';
import { Dispatch, RootState } from '../models';
import { useDispatch } from 'react-redux';
import { Dispatch } from '../models';
import { getRouterData } from '../routes/router';
import { DefaultProps } from '../';

type StateProps = ReturnType<typeof mapState>;
type DispatchProps = ReturnType<typeof mapDispatch>;
type Props = StateProps &
DispatchProps & {
routerData: typeof getRouterData;
};
type Props = {
routerData: typeof getRouterData;
} & DefaultProps;

const mapState = ({ global }: RootState) => ({
// token: global.token,
// userData: global.userData,
});
export default function Controller(props: Props) {
const { routerData } = props || {};
const dispatch = useDispatch<Dispatch>();
// const token = useSelector(({ global }: RootState) => global.token)
const BasicLayout: any = routerData['/'].component;
const UserLayout: any = routerData['/login'].component;
useEffect(() => {
if (!/^(\/login)/.test(window.location.pathname)) {
dispatch.global.verify();
}
// eslint-disable-next-line
}, []);
return (
<Switch>
<Route path="/login" render={(props) => <UserLayout {...props} routerData={routerData} />} />
<Route path="/" render={(props) => <BasicLayout {...props} routerData={routerData} />} />
</Switch>
);
}

const mapDispatch: any = (dispatch: Dispatch) => ({
// verify: dispatch.global.verify,
// verify: global.verify,
});
// type StateProps = ReturnType<typeof mapState>;
// type DispatchProps = ReturnType<typeof mapDispatch>;
// type Props = StateProps &
// DispatchProps & {
// routerData: typeof getRouterData;
// };

class Controller extends React.PureComponent<Props> {
componentDidMount() {
// this.props.verify();
}
render() {
const { routerData } = this.props;
// console.log('this.props:', this.props)
const BasicLayout = routerData['/'].component;
const UserLayout = routerData['/login'].component;
// isAuthenticated = true 表示身份经过验证
// 请求是否登录验证
// if (!this.props.isAuthenticated) {
// return (
// <span>是否登录的验证</span>
// );
// }
// resetProps.token = token;
// resetProps.userData = userData;
return (
<Switch>
<Route path="/login" render={(props) => <UserLayout {...props} routerData={routerData} />} />
<Route path="/" render={(props) => <BasicLayout {...props} routerData={routerData} />} />
</Switch>
);
}
}
// const mapState = ({ global }: RootState) => ({
// // token: global.token,
// // userData: global.userData,
// });

// const mapDispatch: any = (dispatch: Dispatch) => ({
// // verify: dispatch.global.verify,
// // verify: global.verify,
// });

// class Controller extends React.PureComponent<Props> {
// componentDidMount() {
// // this.props.verify();
// }
// render() {
// const { routerData } = this.props;
// // console.log('this.props:', this.props)
// const BasicLayout = routerData['/'].component;
// const UserLayout = routerData['/login'].component;
// // isAuthenticated = true 表示身份经过验证
// // 请求是否登录验证
// // if (!this.props.isAuthenticated) {
// // return (
// // <span>是否登录的验证</span>
// // );
// // }
// // resetProps.token = token;
// // resetProps.userData = userData;
// return (
// <Switch>
// <Route path="/login" render={(props) => <UserLayout {...props} routerData={routerData} />} />
// <Route path="/" render={(props) => <BasicLayout {...props} routerData={routerData} />} />
// </Switch>
// );
// }
// }

export default connect(mapState, mapDispatch)(Controller);
// export default connect(mapState, mapDispatch)(Controller);

0 comments on commit 158b217

Please sign in to comment.