Skip to content

Commit

Permalink
user detail
Browse files Browse the repository at this point in the history
  • Loading branch information
zuiidea committed Apr 27, 2017
1 parent 19ee8ea commit fbd0fcb
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default {
},
*'delete' ({ payload }, { call, put }) {
const data = yield call(remove, { id: payload })
if (data && data.success) {
if (data.success) {
yield put({ type: 'requery' })
} else {
throw data
Expand All @@ -69,7 +69,7 @@ export default {
*create ({ payload }, { call, put }) {
yield put({ type: 'hideModal' })
const data = yield call(create, payload)
if (data && data.success) {
if (data.success) {
yield put({ type: 'requery' })
} else {
throw data
Expand All @@ -80,7 +80,7 @@ export default {
const id = yield select(({ user }) => user.currentItem.id)
const newUser = { ...payload, id }
const data = yield call(update, newUser)
if (data && data.success) {
if (data.success) {
yield put({ type: 'requery' })
} else {
throw data
Expand Down
51 changes: 51 additions & 0 deletions src/models/user/detail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pathToRegexp from 'path-to-regexp'
import { query } from '../../services/user'

export default {

namespace: 'userDetail',

state: {
data: {},
},

subscriptions: {
setup ({ dispatch, history }) {
history.listen(() => {
const match = pathToRegexp('/user/:id').exec(location.pathname)
if (match) {
dispatch({ type: 'query', payload: { id: match[1] } })
}
})
},
},

effects: {
*query ({
payload,
}, { call, put }) {
const data = yield call(query, payload)
const { success, message, status, ...other } = data
if (success) {
yield put({
type: 'querySuccess',
payload: {
data: other,
},
})
} else {
throw data
}
},
},

reducers: {
querySuccess (state, { payload }) {
const { data } = payload
return {
...state,
data,
}
},
},
}
8 changes: 8 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ const Routers = function ({ history, app }) {
cb(null, require('./routes/user/'))
}, 'user')
},
}, {
path: 'user/:id',
getComponent (nextState, cb) {
require.ensure([], require => {
registerModel(app, require('./models/user/detail'))
cb(null, require('./routes/user/detail/'))
}, 'user-detail')
},
}, {
path: 'login',
getComponent (nextState, cb) {
Expand Down
6 changes: 4 additions & 2 deletions src/routes/user/UserModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ const modal = ({
rules: [
{
required: true,
message: '不能为空',
message: '请填写正确手机号',
pattern: /^1[34578]\d{9}$/,
},
],
})(<Input />)}
Expand All @@ -116,7 +117,8 @@ const modal = ({
rules: [
{
required: true,
message: '不能为空',
message: '请填写正确邮箱',
pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
},
],
})(<Input />)}
Expand Down
29 changes: 29 additions & 0 deletions src/routes/user/detail/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'dva'
import styles from './index.less'

const Detail = ({ userDetail }) => {
const { data } = userDetail
const content = []
for (let key in data) {
if ({}.hasOwnProperty.call(data, key)) {
content.push(<div key={key} className={styles.item}>
<div>{key}</div>
<div>{String(data[key])}</div>
</div>)
}
}
return (<div className="content-inner">
<div className={styles.content}>
{content}
</div>
</div>)
}

Detail.propTypes = {
userDetail: PropTypes.object,
loading: PropTypes.bool,
}

export default connect(({ userDetail, loading }) => ({ userDetail, loading: loading.models.userDetail }))(Detail)
14 changes: 14 additions & 0 deletions src/routes/user/detail/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.content {
line-height: 2.4;
font-size: 14px;

.item {
display: flex;

& > div {
&:first-child {
width: 100px;
}
}
}
}
6 changes: 3 additions & 3 deletions src/routes/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import UserList from './UserList'
import UserFilter from './UserFilter'
import UserModal from './UserModal'

function Users ({ location, dispatch, user, loading }) {
const User = ({ location, dispatch, user, loading }) => {
const { list, pagination, currentItem, modalVisible, modalType, isMotion } = user
const { field, keyword } = location.query

Expand Down Expand Up @@ -101,11 +101,11 @@ function Users ({ location, dispatch, user, loading }) {
)
}

Users.propTypes = {
User.propTypes = {
user: PropTypes.object,
location: PropTypes.object,
dispatch: PropTypes.func,
loading: PropTypes.bool,
}

export default connect(({ user, loading }) => ({ user, loading: loading.models.user }))(Users)
export default connect(({ user, loading }) => ({ user, loading: loading.models.user }))(User)

0 comments on commit fbd0fcb

Please sign in to comment.