Skip to content

Commit

Permalink
Step 11. 处理用户编辑
Browse files Browse the repository at this point in the history
  • Loading branch information
JobbyM committed Apr 27, 2017
1 parent e987ed3 commit 358caf1
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/components/Users/UserModal.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

.normal {
}
96 changes: 96 additions & 0 deletions src/components/Users/UserModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import React, { Component } from 'react';
import { Modal, Form, Input } from 'antd';

const FormItem = Form.Item;

class UserEditModal extends Component {

constructor(props) {
super(props);
this.state = {
visible: false,
};
}

showModalHandler = (e) => {
if (e) e.stopPropagation();
this.setState({
visible: true,
});
};

hideModalHandler = () => {
this.setState({
visible: false,
});
};

okHandler = () => {
const { onOk } = this.props;
this.props.form.validateFields((err, values) => {
if (!err) {
onOk(values);
this.hideModalHandler();
}
});
};

render() {
const { children } = this.props;
const { getFieldDecorator } = this.props.form;
const { name, email, website } = this.props.record;
const formItemLayout = {
labelCol: { span: 6 },
wrapperCol: { span: 14 },
};

return (
<span>
<span onClick={this.showModalHandler}>
{ children }
</span>
<Modal
title="Edit User"
visible={this.state.visible}
onOk={this.okHandler}
onCancel={this.hideModalHandler}
>
<Form horizontal onSubmit={this.okHandler}>
<FormItem
{...formItemLayout}
label="Name"
>
{
getFieldDecorator('name', {
initialValue: name,
})(<Input />)
}
</FormItem>
<FormItem
{...formItemLayout}
label="Email"
>
{
getFieldDecorator('email', {
initialValue: email,
})(<Input />)
}
</FormItem>
<FormItem
{...formItemLayout}
label="Website"
>
{
getFieldDecorator('website', {
initialValue: website,
})(<Input />)
}
</FormItem>
</Form>
</Modal>
</span>
);
}
}

export default Form.create()(UserEditModal);
20 changes: 15 additions & 5 deletions src/components/Users/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Table, Pagination, Popconfirm } from 'antd';
import { routerRedux } from 'dva/router';
import styles from './Users.css';
import { PAGE_SIZE } from '../../constants';
import UserModal from './UserModal';

function Users({ dispatch, list: dataSource, loading, total, page: current }) {
function deleteHandler(id) {
Expand All @@ -17,7 +18,14 @@ function Users({ dispatch, list: dataSource, loading, total, page: current }) {
dispatch(routerRedux.push({
pathname: '/users',
query: { page },
}))
}));
}

function editHandler(id, values) {
dispatch({
type: 'users/patch',
payload: { id, values },
});
}

const columns = [
Expand All @@ -40,11 +48,13 @@ function Users({ dispatch, list: dataSource, loading, total, page: current }) {
{
title: 'Operation',
key: 'operation',
render: (text, { id }) => (
render: (text, record) => (
<span className={styles.operation}>
<a href="">Edit</a>
<Popconfirm title="Confirm to delete?" onConfirm={deleteHandler.bind(null, id)}>
<a href="">Delete</a>
<UserModal record={record} onOk={editHandler.bind(null, record.id)}>
<a>Edit</a>
</UserModal>
<Popconfirm title="Confirm to delete?" onConfirm={deleteHandler.bind(null, record.id)}>
<a>Delete</a>
</Popconfirm>
</span>
),
Expand Down
5 changes: 5 additions & 0 deletions src/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export default {
const page = yield select(state => state.users.page);
yield put({ type: 'fetch', payload: { page } });
},
*patch({ payload: { id, values } }, { call, put, select }) {
yield call(usersService.patch, id, values);
const page = yield select(state => state.users.page);
yield put({ type: 'fetch', payload: { page } });
},
},
subscriptions: {
setup({ dispatch, history }) {
Expand Down
7 changes: 7 additions & 0 deletions src/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ export function remove(id) {
method: 'DELETE',
});
}

export function patch(id, values) {
return request(`/api/users/${id}`, {
method: 'PATCH',
body: JSON.stringify(values),
});
}

0 comments on commit 358caf1

Please sign in to comment.