/* * Copyright (c) 2020 - present Cloudogu GmbH * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU Affero General Public License as published by the Free * Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see https://www.gnu.org/licenses/. */ import React, { FC } from "react"; import { useTranslation } from "react-i18next"; import { UnknownTotalCollection } from "@scm-manager/ui-types"; import { Button, LinkButton } from "@scm-manager/ui-core"; type Props = { collection: UnknownTotalCollection; page: number; filter?: string | URLSearchParams; baseUrl: string; }; type PageButtonProps = { className: string; label: string; link: string; disabled?: boolean; }; const PageButton: FC = ({ className, label, link, disabled }) => { if (disabled) { return ( ); } return ( {label} ); }; const LinkPaginator: FC = ({ collection, page, filter, baseUrl }) => { const [t] = useTranslation("commons"); const createLink = (pageNumber: string | number) => { const link = `/${baseUrl}/${pageNumber}`; if (typeof filter === "string") { return `${link}?q=${filter}`; } else if (typeof filter === "object") { return `${link}?${(filter as URLSearchParams).toString()}`; } return link; }; const renderFirstButton = () => { return ; }; const renderPreviousButton = (className: string, label?: string) => { const previousPage = page - 1; return ( ); }; const hasLink = (name: string) => { return collection._links[name]; }; const renderNextButton = (className: string, label?: string) => { const nextPage = page + 1; const link = createLink(`${nextPage}`); return ( ); }; const renderLastButton = () => { return ( ); }; const renderGenericLastButton = () => { return ; }; const separator = () => { return …; }; const currentPage = (page: number) => { return ( ); }; const pageLinks = () => { const links = []; const page = collection.page + 1; const pageTotal = collection.pageTotal; if (page > 1) { links.push(renderFirstButton()); } if (page > 3) { links.push(separator()); } if (page > 2) { links.push(renderPreviousButton("pagination-link")); } links.push(currentPage(page)); if (typeof pageTotal !== "number" || page + 1 < pageTotal) { links.push(renderNextButton("pagination-link")); } if (typeof pageTotal === "number" && page + 2 < pageTotal) links.push(separator()); //if there exists pages between next and last if (typeof pageTotal === "number" && page < pageTotal) { links.push(renderLastButton()); } if (typeof pageTotal !== "number") { links.push(separator()); links.push(renderGenericLastButton()); } return links; }; if (!collection) { return null; } return ( ); }; export default LinkPaginator;