Skip to content

Commit c68263b

Browse files
committed
feat: Users: add query param for current page, refactor code
1 parent f3a1e22 commit c68263b

File tree

3 files changed

+66
-41
lines changed

3 files changed

+66
-41
lines changed

src/router/index.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,28 @@ function configRoutes () {
111111
},
112112
{
113113
path: 'users',
114-
meta: { label: 'Users'},
114+
meta: {
115+
label: 'Users'
116+
},
115117
component: {
116-
render (c) { return c('router-view') }
118+
render(c) {
119+
return c('router-view')
120+
}
117121
},
118122
children: [
119123
{
120124
path: '',
121-
component: Users,
125+
name: 'Users',
126+
component: Users
122127
},
123128
{
124129
path: ':id',
125-
meta: { label: 'User Details'},
130+
meta: {
131+
label: 'User Details'
132+
},
126133
name: 'User',
127-
component: User,
128-
},
134+
component: User
135+
}
129136
]
130137
},
131138
{

src/views/users/User.vue

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
striped
1111
small
1212
fixed
13-
:items="getUserData($route.params.id)"
14-
:fields="$options.fields"
13+
:items="visibleData"
14+
:fields="fields"
1515
/>
1616
</CCardBody>
1717
<CCardFooter>
@@ -26,16 +26,28 @@
2626
import usersData from './UsersData'
2727
export default {
2828
name: 'User',
29-
fields: [
30-
{ key: 'key', _style: 'width:150px' },
31-
{ key: 'value' , _style: 'width:150px;' }
32-
],
33-
methods: {
34-
getUserData (id) {
29+
computed: {
30+
fields () {
31+
return [
32+
{
33+
key: 'key',
34+
label: this.userData.filter(param => param.key === 'username')[0].value,
35+
_style: 'width:150px'
36+
},
37+
{ key: 'value', label: '', _style: 'width:150px;' }
38+
]
39+
},
40+
userData () {
41+
const id = this.$route.params.id
3542
const user = usersData.find((user, index) => index + 1 == id)
3643
const userDetails = user ? Object.entries(user) : [['id', 'Not found']]
3744
return userDetails.map(([key, value]) => { return { key, value } })
3845
},
46+
visibleData () {
47+
return this.userData.filter(param => param.key !== 'username')
48+
}
49+
},
50+
methods: {
3951
goBack() {
4052
this.$router.go(-1)
4153
}

src/views/users/Users.vue

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,11 @@
1212
striped
1313
:items="items"
1414
:fields="fields"
15-
:items-per-page="perPage"
16-
@row-clicked="rowClicked"
17-
:pagination="$options.paginationProps"
18-
index-column
15+
:items-per-page="5"
1916
clickable-rows
17+
:active-page="activePage"
18+
@row-clicked="rowClicked"
2019
>
21-
<template #username="data">
22-
<td>
23-
<strong>{{data.item.username}}</strong>
24-
</td>
25-
</template>
26-
2720
<template #status="data">
2821
<td>
2922
<CBadge :color="getBadge(data.item.status)">
@@ -32,6 +25,13 @@
3225
</td>
3326
</template>
3427
</CDataTable>
28+
<CPagination
29+
align="center"
30+
:double-arrows="false"
31+
:active-page="activePage"
32+
:pages="5"
33+
@update:activePage="pageChange"
34+
/>
3535
</CCardBody>
3636
</CCard>
3737
</transition>
@@ -43,37 +43,43 @@
4343
import usersData from './UsersData'
4444
export default {
4545
name: 'Users',
46-
data: () => {
46+
data () {
4747
return {
4848
items: usersData,
4949
fields: [
50-
{ key: 'username', label: 'Name' },
50+
{ key: 'username', label: 'Name', _classes: 'font-weight-bold' },
5151
{ key: 'registered' },
5252
{ key: 'role' },
5353
{ key: 'status' }
5454
],
55-
perPage: 5,
55+
activePage: 1
5656
}
5757
},
58-
paginationProps: {
59-
align: 'center',
60-
doubleArrows: false,
61-
previousButtonHtml: 'prev',
62-
nextButtonHtml: 'next'
58+
watch: {
59+
$route: {
60+
immediate: true,
61+
handler (route) {
62+
if (route.query && route.query.page) {
63+
this.activePage = Number(route.query.page)
64+
}
65+
}
66+
}
6367
},
6468
methods: {
6569
getBadge (status) {
66-
return status === 'Active' ? 'success'
67-
: status === 'Inactive' ? 'secondary'
68-
: status === 'Pending' ? 'warning'
69-
: status === 'Banned' ? 'danger' : 'primary'
70-
},
71-
userLink (id) {
72-
return `users/${id.toString()}`
70+
switch (status) {
71+
case 'Active': return 'success'
72+
case 'Inactive': return 'secondary'
73+
case 'Pending': return 'warning'
74+
case 'Banned': return 'danger'
75+
default: 'primary'
76+
}
7377
},
7478
rowClicked (item, index) {
75-
const userLink = this.userLink(index + 1)
76-
this.$router.push({path: userLink})
79+
this.$router.push({path: `users/${index + 1}`})
80+
},
81+
pageChange (val) {
82+
this.$router.push({ query: { page: val }})
7783
}
7884
}
7985
}

0 commit comments

Comments
 (0)