Created
April 5, 2023 18:49
-
-
Save samyarkd/9b4b3cd3ff5e655c6b0141da6b113a09 to your computer and use it in GitHub Desktop.
I wan to make a pagination in grammy but idk how, the bellow code is incomplete im looking for a way to store the skip variable to be accessible to all the buttons or do you know any other way of implementing pagination using grammy and prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const take = 5 | |
const transactions = new Menu<CustomContext>('transactions', { | |
onMenuOutdated: (ctx) => ctx.deleteMessage(), | |
}) | |
// Prev btn | |
.text( | |
(ctx) => { | |
const skip = ctx.msg.text.startsWith('skip-') ? Number(ctx.msg.text.split('-')[1]) : 0 | |
if (skip === 0) { | |
return '🚫' | |
} else { | |
return 'Prev' | |
} | |
}, | |
async (ctx) => { | |
const skip = ctx.msg.text.startsWith('skip-') ? Number(ctx.msg.text.split('-')[1]) : 0 | |
if (skip === 0) { | |
await setTransactionText({ skip: skip - take }, ctx) | |
} | |
} | |
) | |
// Next btn | |
.text( | |
async (ctx) => { | |
const skip = ctx.msg.text.startsWith('skip-') ? Number(ctx.msg.text.split('-')[1]) : 0 | |
const transactionCount = await prismaClient.transaction.count() | |
let pageCount = (transactionCount - (transactionCount % take)) / take | |
if (transactionCount % take !== 0) { | |
pageCount = pageCount + 1 | |
} | |
const currentPage = skip / take + 2 | |
console.log('asdas5', currentPage, pageCount, skip) | |
if (currentPage === pageCount) { | |
return '🚫' | |
} | |
return 'Next' | |
}, | |
async (ctx) => { | |
const skip = ctx.msg.text.startsWith('skip-') ? Number(ctx.msg.text.split('-')[1]) : 0 | |
const transactionCount = await prismaClient.transaction.count() | |
const pageCount = (transactionCount - (transactionCount % take)) / take | |
const currentPage = skip / take | |
if (pageCount !== currentPage) { | |
await setTransactionText({ skip: skip + take }, ctx) | |
} | |
} | |
) | |
.row() | |
.back('Back') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment