mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: add pagination buttons
This commit is contained in:
1 parent
fa36b63fbe
commit
3b68693177
8 files changed
+52
-19
No files matched your search
@@ -8,12 +8,15 @@ export async function commandListController(req, res) {
|
|||||||
const itemsPerPage = 100
|
const itemsPerPage = 100
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const payload = await errorCommands(
|
const pagination = new Pagination(order, itemsPerPage, nextKey)
|
||||||
new Pagination(order, itemsPerPage, nextKey)
|
const payload = await errorCommands(pagination)
|
||||||
)
|
|
||||||
const newNextKey = payload[payload.length - 1]?._id
|
const newNextKey = payload[payload.length - 1]?._id
|
||||||
|
|
||||||
res.render('commands', { errorCommands: payload, nextKey: newNextKey })
|
res.render('commands', {
|
||||||
|
errorCommands: payload,
|
||||||
|
nextKey: newNextKey,
|
||||||
|
order: pagination.getOrder(),
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
res.status(500).send('Error fetching data')
|
res.status(500).send('Error fetching data')
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ export async function sessionListController(req, res) {
|
|||||||
const itemsPerPage = 50
|
const itemsPerPage = 50
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// const totalSessions = await sessionCount()
|
const pagination = new Pagination(order, itemsPerPage, nextKey)
|
||||||
// const maxPages = Math.ceil(totalSessions / itemsPerPage)
|
const sessions = await sessionList(pagination)
|
||||||
|
|
||||||
const sessions = await sessionList(
|
|
||||||
new Pagination(order, itemsPerPage, nextKey)
|
|
||||||
)
|
|
||||||
const newNextKey = sessions[sessions.length - 1]?._id
|
const newNextKey = sessions[sessions.length - 1]?._id
|
||||||
|
|
||||||
res.render('sessions', { sessions, nextKey: newNextKey })
|
res.render('sessions', {
|
||||||
|
sessions,
|
||||||
|
nextKey: newNextKey,
|
||||||
|
order: pagination.getOrder(),
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.log(error)
|
||||||
res.status(500).send('Error fetching session data')
|
res.status(500).send('Error fetching session data')
|
||||||
|
|||||||
@@ -6,10 +6,15 @@ export async function userListController(req, res) {
|
|||||||
const itemsPerPage = 50
|
const itemsPerPage = 50
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const users = await userList(new Pagination(order, itemsPerPage, nextKey))
|
const pagination = new Pagination(order, itemsPerPage, nextKey)
|
||||||
|
const users = await userList(pagination)
|
||||||
const newNextKey = users[users.length - 1]?._id
|
const newNextKey = users[users.length - 1]?._id
|
||||||
|
|
||||||
res.render('users', { users, nextKey: newNextKey })
|
res.render('users', {
|
||||||
|
users,
|
||||||
|
nextKey: newNextKey,
|
||||||
|
order: pagination.getOrder(),
|
||||||
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
res.status(500).send('Error fetching user data')
|
res.status(500).send('Error fetching user data')
|
||||||
|
|||||||
@@ -14,6 +14,10 @@ class Asc {
|
|||||||
getOrder() {
|
getOrder() {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getName() {
|
||||||
|
return ASC
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Desc {
|
class Desc {
|
||||||
@@ -24,6 +28,10 @@ class Desc {
|
|||||||
getOrder() {
|
getOrder() {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getName() {
|
||||||
|
return DESC
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Pagination {
|
export default class Pagination {
|
||||||
@@ -59,4 +67,8 @@ export default class Pagination {
|
|||||||
limit() {
|
limit() {
|
||||||
return { $limit: this.itemsPerPage }
|
return { $limit: this.itemsPerPage }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getOrder() {
|
||||||
|
return this.order.getName()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,8 @@ import Pagination, { ASC, DESC } from '../src/models/pagination.js'
|
|||||||
|
|
||||||
describe('Pagination', () => {
|
describe('Pagination', () => {
|
||||||
it('should failed when not provide order', () => {
|
it('should failed when not provide order', () => {
|
||||||
expect(() => new Pagination()).toThrow('order must be asc or desc')
|
expect(new Pagination('').getOrder()).toBe(DESC)
|
||||||
|
expect(() => new Pagination('Hahaha')).toThrow()
|
||||||
})
|
})
|
||||||
it('should return correct limit', () => {
|
it('should return correct limit', () => {
|
||||||
const pagination = new Pagination(DESC, 10)
|
const pagination = new Pagination(DESC, 10)
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
<h1>Commands</h1>
|
<h1>Commands</h1>
|
||||||
<div>
|
<div>
|
||||||
<h2>Error commands</h2>
|
<h2>Error commands</h2>
|
||||||
|
<div class='pagination'>
|
||||||
|
<a href='/commands?order=asc'>Ascending</a>
|
||||||
|
<a href='/commands?order=desc'>Descending</a>
|
||||||
|
</div>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Command</th>
|
<th>Command</th>
|
||||||
@@ -21,7 +25,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
</table>
|
</table>
|
||||||
</div>
|
<div class='pagination'>
|
||||||
<div class='pagination'>
|
<a href='/commands?nextKey={{nextKey}}&order={{order}}'>Next</a>
|
||||||
<a href='/commands?nextKey={{nextKey}}'>Next</a>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
<h1>Sessions</h1>
|
<h1>Sessions</h1>
|
||||||
|
<div class='pagination'>
|
||||||
|
<a href='/sessions?order=asc'>Ascending</a>
|
||||||
|
<a href='/sessions?order=desc'>Descending</a>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -26,5 +30,5 @@
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<div class='pagination'>
|
<div class='pagination'>
|
||||||
<a href='/sessions?nextKey={{nextKey}}'>Next</a>
|
<a href='/sessions?nextKey={{nextKey}}&order={{order}}'>Next</a>
|
||||||
</div>
|
</div>
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
<h1>Users List</h1>
|
<h1>Users List</h1>
|
||||||
|
<div class='pagination'>
|
||||||
|
<a href='/users?order=asc'>Ascending</a>
|
||||||
|
<a href='/users?order=desc'>Descending</a>
|
||||||
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -22,5 +26,5 @@
|
|||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
<div class='pagination'>
|
<div class='pagination'>
|
||||||
<a href='/users?nextKey={{nextKey}}'>Next</a>
|
<a href='/users?nextKey={{nextKey}}&order={{order}}'>Next</a>
|
||||||
</div>
|
</div>
|
||||||
Reference in new issue
Block a user