Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Page indicator #247

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions modules/CustomerService/Tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ BatchOperationMenu.propTypes = {
*/
export function useTickets() {
const [tickets, setTickets] = useState([])
const [totalCount, setTotalCount] = useState()
const { tagMetadatas } = useContext(AppContext)
const { filters } = useTicketFilters()

Expand Down Expand Up @@ -577,52 +578,59 @@ export function useTickets() {
query = query.where('objectId', 'in', searchMatchedTicketIds)
}

const ticketObjects = await query
const [ticketObjects, count] = await query
.include('author', 'assignee')
.limit(PAGE_SIZE)
.skip(parseInt(page) * PAGE_SIZE)
.orderBy('latestReply.updatedAt', 'desc')
.orderBy('updatedAt', 'desc')
.find()
.findAndCount()
setTickets(ticketObjects.map((t) => t.toJSON()))
setTotalCount(count)
}, [filters])

useEffect(() => {
findTickets()
}, [filters])

return { tickets, reload: findTickets }
return { tickets, totalCount, reload: findTickets }
}

function TicketPager({ noMore }) {
function TicketPager({ totalCount }) {
const { t } = useTranslation()
const { filters, updatePath } = useTicketFilters()
const { page = '0' } = filters
const isFirstPage = page === '0'
const { page: pageParam = '0' } = filters
const page = parseInt(pageParam)
const isFirstPage = page === 0
const noMore = totalCount <= (page + 1) * PAGE_SIZE

if (isFirstPage && noMore) {
return null
}
return (
<div className="my-2 d-flex justify-content-between">
<Button
variant="light"
disabled={isFirstPage}
onClick={() => updatePath({ page: parseInt(page) - 1 })}
>
&larr; {t('previousPage')}
</Button>
<Button
variant="light"
disabled={noMore}
onClick={() => updatePath({ page: parseInt(page) + 1 })}
>
<div className="my-2 d-flex justify-content-between align-items-center">
<div>
<Button variant="light" disabled={isFirstPage} onClick={() => updatePath({ page: 0 })}>
{t('firstPage')}
</Button>{' '}
<Button
variant="light"
disabled={isFirstPage}
onClick={() => updatePath({ page: page - 1 })}
>
&larr; {t('previousPage')}
</Button>
</div>
{totalCount > 0 && (
<span>
{page * PAGE_SIZE + 1} - {Math.min(totalCount, (page + 1) * PAGE_SIZE)} / {totalCount}
</span>
)}
<Button variant="light" disabled={noMore} onClick={() => updatePath({ page: page + 1 })}>
{t('nextPage')} &rarr;
</Button>
</div>
)
}
TicketPager.propTypes = {
totalCount: PropTypes.number,
noMore: PropTypes.bool,
}

Expand All @@ -633,7 +641,7 @@ export default function CustomerServiceTickets() {
const [checkedTickets, setCheckedTickets] = useState(new Set())
const customerServices = useCustomerServices()
const categories = useCategories()
const { tickets, reload } = useTickets()
const { tickets, totalCount, reload } = useTickets()

const handleCheckAll = (e) => {
if (e.target.checked) {
Expand Down Expand Up @@ -715,7 +723,7 @@ export default function CustomerServiceTickets() {
checkedTicketIds={checkedTickets}
onCheckTicket={handleCheckTicket}
/>
<TicketPager noMore={tickets.length < PAGE_SIZE} />
<TicketPager totalCount={totalCount} />
</div>
)
}
4 changes: 4 additions & 0 deletions modules/i18n/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ const messages = {
'Previous',
'上一页'
],
'firstPage': [
'Page 1',
'第一页'
],
'private': [
'Private',
'非公开'
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"mailgun-js": "^0.18.0",
"mini-css-extract-plugin": "^1.3.5",
"moment": "^2.29.1",
"open-leancloud-storage": "0.0.24",
"open-leancloud-storage": "0.0.25",
"query-string": "^6.14.0",
"randomcolor": "^0.5.3",
"randomstring": "^1.1.5",
Expand Down