Skip to content

Commit

Permalink
5. Add Users components and make user list work
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Feb 22, 2018
1 parent f511a77 commit ee9ccba
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/pages/users/components/Users.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

.normal {
}

.operation a {
margin: 0 .5em;
}
71 changes: 71 additions & 0 deletions src/pages/users/components/Users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { connect } from 'dva';
import { Table, Pagination, Popconfirm } from 'antd';
import styles from './Users.css';
import { PAGE_SIZE } from '../constants';

function Users({ list: dataSource, total, page: current }) {

This comment has been minimized.

Copy link
@saoyu

saoyu Sep 25, 2019

list: dataSource 是什么意思啊

This comment has been minimized.

Copy link
@Jeff-Tian

Jeff-Tian Oct 15, 2019

list: dataSource 是什么意思啊

es6 语法,解构时,重命名变量。传进来的 props 里有一个 list 属性,但是我们后面想用 dataSource 来用它。即等价于:

function Users(props) {
  const dataSource = props.list;
  const total = props.total;
  const current = props.page;
  ...
}
function deleteHandler(id) {
console.warn(`TODO: ${id}`);
}

const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
render: text => <a href="">{text}</a>,
},
{
title: 'Email',
dataIndex: 'email',
key: 'email',
},
{
title: 'Website',
dataIndex: 'website',
key: 'website',
},
{
title: 'Operation',
key: 'operation',
render: (text, { id }) => (
<span className={styles.operation}>
<a href="">Edit</a>
<Popconfirm title="Confirm to delete?" onConfirm={deleteHandler.bind(null, id)}>
<a href="">Delete</a>
</Popconfirm>
</span>
),
},
];

return (
<div className={styles.normal}>
<div>
<Table
columns={columns}
dataSource={dataSource}
rowKey={record => record.id}
pagination={false}
/>
<Pagination
className="ant-table-pagination"
total={total}
current={current}
pageSize={PAGE_SIZE}
/>
</div>
</div>
);
}

function mapStateToProps(state) {
const { list, total, page } = state.users;
return {
list,
total,
page,
};
}

export default connect(mapStateToProps)(Users);
2 changes: 2 additions & 0 deletions src/pages/users/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

export const PAGE_SIZE = 3;
16 changes: 12 additions & 4 deletions src/pages/users/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@ export default {
state: {
list: [],
total: null,
page: null,
},
reducers: {
save(state, { payload: { data: list, total } }) {
return { ...state, list, total };
save(state, { payload: { data: list, total, page } }) {
return { ...state, list, total, page };
},
},
effects: {
*fetch({ payload: { page } }, { call, put }) {
*fetch({ payload: { page = 1 } }, { call, put }) {
const { data, headers } = yield call(usersService.fetch, { page });
yield put({ type: 'save', payload: { data, total: headers['x-total-count'] } });
yield put({
type: 'save',
payload: {
data,
total: parseInt(headers['x-total-count'], 10),
page: parseInt(page, 10),
},
});
},
},
subscriptions: {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/users/page.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Users from './components/Users';

export default () => {
return (
<div>
Users Page
<Users />
</div>
)
}
3 changes: 2 additions & 1 deletion src/pages/users/services/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PAGE_SIZE } from '../constants';
import request from '../../../utils/request';

export function fetch({ page = 1 }) {
return request(`/api/users?_page=${page}&_limit=5`);
return request(`/api/users?_page=${page}&_limit=${PAGE_SIZE}`);
}

6 comments on commit ee9ccba

@weihuayao
Copy link

Choose a reason for hiding this comment

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

为啥 我导入 组件后 报错:TypeError: Cannot read property 'list' of undefined

@Tzixiong
Copy link

Choose a reason for hiding this comment

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

我也是

@xgqfrms
Copy link

Choose a reason for hiding this comment

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

什么鬼教程,完全看不懂;

到底是 dva 的项目,还是 umi 的项目,结构目录完全不同

@RichardTibco
Copy link

Choose a reason for hiding this comment

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

为啥 我导入 组件后 报错:TypeError: Cannot read property 'list' of undefined

需要在index.js里加入model,否则就看不到models/users.js里的任何变量

@zjonejj
Copy link

Choose a reason for hiding this comment

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

需要有点基础的来看,教程没问题。思路很清晰

@Sunlei012
Copy link

Choose a reason for hiding this comment

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

什么鬼教程,完全看不懂;

到底是 dva 的项目,还是 umi 的项目,结构目录完全不同

同意。国产框架不错,文档不行。

这文档太垃圾了, 跟着文档完全就做不出来

Please sign in to comment.