Skip to content

Commit

Permalink
10. Handle user edit
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Feb 22, 2018
1 parent f12e32b commit 6044e2f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
96 changes: 96 additions & 0 deletions src/pages/users/components/UserModal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { 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,
};
}

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

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

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

This comment has been minimized.

Copy link
@gccdChen

gccdChen Jul 10, 2019

如果在这里调用 hideModelHandler() , 会不会表单没成功 , 但是对话框已经消失?

This comment has been minimized.

Copy link
@zhi-zhi-zhi

zhi-zhi-zhi Nov 9, 2019

有可能。
正常来说要对onOk事件回执做个判断

}
});
};

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.showModelHandler}>
{ children }
</span>
<Modal
title="Edit User"
visible={this.state.visible}
onOk={this.okHandler}
onCancel={this.hideModelHandler}
>
<Form horizontal onSubmit={this.okHandler}>

This comment has been minimized.

Copy link
@xuyingnan

xuyingnan Aug 6, 2018

改为

<Form layout="horizontal" onSubmit={this.okHandler}>

解决 Warning: Received true for a non-boolean attribute horizontal 的警告

<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);
16 changes: 13 additions & 3 deletions src/pages/users/components/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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 @@ -19,6 +20,13 @@ function Users({ dispatch, list: dataSource, loading, total, page: current }) {
}));
}

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

const columns = [
{
title: 'Name',
Expand All @@ -39,10 +47,12 @@ 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)}>
<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 href="">Delete</a>
</Popconfirm>
</span>
Expand Down
5 changes: 5 additions & 0 deletions src/pages/users/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/pages/users/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 6044e2f

Please sign in to comment.