Skip to content

Commit

Permalink
12. Handle user create
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Dec 19, 2016
1 parent ed7a4ee commit 72cd736
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/components/Users/Users.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
.normal {
}

.create {
margin-bottom: 1.5em;
}

.operation a {
margin: 0 .5em;
}
14 changes: 13 additions & 1 deletion src/components/Users/Users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { connect } from 'dva';
import { Table, Pagination, Popconfirm } from 'antd';
import { Table, Pagination, Popconfirm, Button } from 'antd';
import { routerRedux } from 'dva/router';
import styles from './Users.css';
import { PAGE_SIZE } from '../../constants';
Expand Down Expand Up @@ -28,6 +28,13 @@ function Users({ dispatch, list: dataSource, loading, total, page: current }) {
});
}

function createHandler(values) {
dispatch({
type: 'users/create',
payload: values,
});
}

const columns = [
{
title: 'Name',
Expand Down Expand Up @@ -64,6 +71,11 @@ function Users({ dispatch, list: dataSource, loading, total, page: current }) {
return (
<div className={styles.normal}>
<div>
<div className={styles.create}>
<UserModal record={{}} onOk={createHandler}>
<Button type="primary">Create User</Button>
</UserModal>
</div>
<Table
columns={columns}
dataSource={dataSource}
Expand Down
14 changes: 10 additions & 4 deletions src/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ export default {
},
});
},
*remove({ payload: id }, { call, put, select }) {
*remove({ payload: id }, { call, put }) {
yield call(usersService.remove, id);
const page = yield select(state => state.users.page);
yield put({ type: 'fetch', payload: { page } });
yield put({ type: 'reload' });
},
*patch({ payload: { id, values } }, { call, put, select }) {
*patch({ payload: { id, values } }, { call, put }) {
yield call(usersService.patch, id, values);
yield put({ type: 'reload' });
},
*create({ payload: values }, { call, put }) {
yield call(usersService.create, values);
yield put({ type: 'reload' });
},
*reload(action, { put, select }) {

This comment has been minimized.

Copy link
@bytemofan

bytemofan Aug 17, 2017

这里的action参数没有用到啊?它作为参数的作用是啥?

This comment has been minimized.

Copy link
@sorrycc

sorrycc Aug 17, 2017

Author Member

占位。

const page = yield select(state => state.users.page);
yield put({ type: 'fetch', payload: { page } });
},
Expand Down
7 changes: 7 additions & 0 deletions src/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ export function patch(id, values) {
body: JSON.stringify(values),
});
}

export function create(values) {
return request('/api/users', {
method: 'POST',
body: JSON.stringify(values),
});
}

3 comments on commit 72cd736

@Blackgan3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你好,新建完成之后,再次点击新建,弹出的model层中还有上次新建的数据,请问怎么清除那些缓存的数据呢?

@jarvislin94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blackgan3 可以给Modal加个属性 'key'

@areyouse7en
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Blackgan3 在UserModal.js里okHandler里加一句this.props.form.resetFields()

Please sign in to comment.