Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

added pagination for dashboard

kruzhambus081ef80ffbef
diff --git a/src/api/index.js b/src/api/index.js
index ad9584e41..b31e94227 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -162,9 +162,9 @@ export const verifyShare = ({ path, id }) =>
: error.response.data.message
})
-export const getProjects = () =>
+export const getProjects = (take = 0, skip = 0) =>
api
- .get('/project')
+ .get(`/project?take=${take}&skip=${skip}`)
.then((response) => response.data)
.catch((error) => {
debug('%s', error)
diff --git a/src/pages/Dashboard/Dashboard.jsx b/src/pages/Dashboard/Dashboard.jsx
index 293d015ae..9c2d6ba45 100644
--- a/src/pages/Dashboard/Dashboard.jsx
+++ b/src/pages/Dashboard/Dashboard.jsx
@@ -1,5 +1,7 @@
/* eslint-disable react/forbid-prop-types */
-import React, { memo, useState } from 'react'
+import React, {
+ memo, useState, useMemo, useEffect,
+} from 'react'
import { Link, useHistory } from 'react-router-dom'
import cx from 'clsx'
import dayjs from 'dayjs'
@@ -22,10 +24,12 @@ import Loader from 'ui/Loader'
import { ActivePin, InactivePin, WarningPin } from 'ui/Pin'
import PulsatingCircle from 'ui/icons/PulsatingCircle'
import routes from 'routes'
-import { isSelfhosted } from 'redux/constants'
+import { isSelfhosted, ENTRIES_PER_PAGE_DASHBOARD } from 'redux/constants'
import EventsRunningOutBanner from 'components/EventsRunningOutBanner'
-import { acceptShareProject } from 'api'
+import { acceptShareProject, getProjects } from 'api'
+
+import Pagination from 'ui/Pagination'
const ProjectCart = ({
name, created, active, overall, t, language, live, isPublic, confirmed, id, deleteProjectFailed,
@@ -172,6 +176,10 @@ const Dashboard = ({
const { t, i18n: { language } } = useTranslation('common')
const [showActivateEmailModal, setShowActivateEmailModal] = useState(false)
const history = useHistory()
+ const [paginationProject, setPaginationProject] = useState(projects)
+
+ const [currentPage, setCurrentPage] = useState(1)
+ const pageAmount = useMemo(() => Math.ceil(paginationProject.total / ENTRIES_PER_PAGE_DASHBOARD), [paginationProject])
const onNewProject = () => {
if (user.isActive || isSelfhosted) {
@@ -181,6 +189,20 @@ const Dashboard = ({
}
}
+ useEffect(() => {
+ getProjects(ENTRIES_PER_PAGE_DASHBOARD, (currentPage - 1) * ENTRIES_PER_PAGE_DASHBOARD)
+ .then((res) => {
+ const { results } = res
+
+ if (!_isEmpty(results)) {
+ setPaginationProject({ ...res })
+ }
+ })
+ .catch((e) => {
+ console.log(e)
+ })
+ }, [currentPage])
+
if (error && !isLoading) {
return (
<Title title={t('titles.dashboard')}>
@@ -220,7 +242,7 @@ const Dashboard = ({
) : (
<div className='shadow overflow-hidden sm:rounded-md mt-10'>
<ul className='divide-y divide-gray-200 dark:divide-gray-500'>
- {_map(_filter(projects, ({ uiHidden }) => !uiHidden), ({
+ {_map(_filter(paginationProject.results, ({ uiHidden }) => !uiHidden), ({
name, id, created, active, overall, live, public: isPublic, confirmed, shared,
}) => (
<div key={confirmed ? `${id}-confirmed` : id}>
@@ -268,6 +290,11 @@ const Dashboard = ({
</div>
)}
</div>
+ {
+ pageAmount > 1 && (
+ <Pagination page={currentPage} setPage={(page) => setCurrentPage(page)} pageAmount={pageAmount || 0} />
+ )
+ }
</div>
</div>
<Modal
diff --git a/src/redux/constants/index.js b/src/redux/constants/index.js
index 4f5376be5..d3b7b9b8f 100644
--- a/src/redux/constants/index.js
+++ b/src/redux/constants/index.js
@@ -165,3 +165,4 @@ export const roleAdmin = {
}
export const INVITATION_EXPIRES_IN = 48 // hours
+export const ENTRIES_PER_PAGE_DASHBOARD = 5
diff --git a/src/ui/Pagination.jsx b/src/ui/Pagination.jsx
new file mode 100644
index 000000000..70b387483
--- /dev/null
+++ b/src/ui/Pagination.jsx
@@ -0,0 +1,56 @@
+import React from 'react'
+import { ArrowNarrowLeftIcon, ArrowNarrowRightIcon } from '@heroicons/react/solid'
+import _map from 'lodash/map'
+import cx from 'clsx'
+
+const Pagination = ({ page, setPage, pageAmount }) => {
+ console.log(page, pageAmount)
+ return (
+ <nav className='border-t border-gray-200 px-4 flex items-center justify-between sm:px-0'>
+ <div className='-mt-px w-0 flex-1 flex'>
+ <button
+ type='button'
+ onClick={() => setPage(page - 1)}
+ className='border-t-2 border-transparent pt-4 pr-1 inline-flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300'
+ >
+ <ArrowNarrowLeftIcon className='mr-3 h-5 w-5 text-gray-400' aria-hidden='true' />
+ Previous
+ </button>
+ </div>
+ <div className='hidden md:-mt-px md:flex'>
+ {
+ _map(Array(pageAmount), (_, index) => {
+ return (
+ <button
+ key={index}
+ type='button'
+ onClick={() => setPage(index + 1)}
+ className={cx({
+ 'border-indigo-500 text-indigo-600 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium': index + 1 === page,
+ 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium': index + 1 !== page,
+ })}
+ >
+ {index + 1}
+ </button>
+ )
+ })
+ }
+ {/* <span className='border-transparent text-gray-500 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium'>
+ ...
+ </span> */}
+ </div>
+ <div className='-mt-px w-0 flex-1 flex justify-end'>
+ <button
+ type='button'
+ onClick={() => setPage(page + 1)}
+ className='border-t-2 border-transparent pt-4 pl-1 inline-flex items-center text-sm font-medium text-gray-500 hover:text-gray-700 hover:border-gray-300'
+ >
+ Next
+ <ArrowNarrowRightIcon className='ml-3 h-5 w-5 text-gray-400' aria-hidden='true' />
+ </button>
+ </div>
+ </nav>
+ )
+}
+
+export default Pagination