From 66761c66586b3ce7eb2847255152dc0940a49507 Mon Sep 17 00:00:00 2001 From: Christopher Hubert Date: Thu, 5 Dec 2024 16:01:47 -0500 Subject: [PATCH] MAT-7791: add pagination --- .../argumentSection/ArgumentSection.tsx | 24 ++-- .../argumentSection/Arguments.tsx | 130 +++++++++++++++--- .../functionBuilder/FunctionBuilder.tsx | 1 - 3 files changed, 123 insertions(+), 32 deletions(-) diff --git a/src/CqlBuilderPanel/functionsSection/argumentSection/ArgumentSection.tsx b/src/CqlBuilderPanel/functionsSection/argumentSection/ArgumentSection.tsx index 0bc436b..e667fa2 100644 --- a/src/CqlBuilderPanel/functionsSection/argumentSection/ArgumentSection.tsx +++ b/src/CqlBuilderPanel/functionsSection/argumentSection/ArgumentSection.tsx @@ -14,10 +14,10 @@ import * as _ from "lodash"; import { useFormik } from "formik"; import Arguments from "./Arguments"; import { FunctionArgument } from "../../../model/CqlBuilderLookup"; +import ConfirmationDialog from "../../common/ConfirmationDialog"; interface ArgumentsProps { functionArguments?: FunctionArgument[]; - setConfirmationDialog: Function; canEdit: boolean; addArgumentToFunctionsArguments: Function; } @@ -35,12 +35,9 @@ const availableDataTypes = [ ]; export default function ArgumentSection(props: ArgumentsProps) { - const { - addArgumentToFunctionsArguments, - functionArguments, - setConfirmationDialog, - canEdit, - } = props; + const { addArgumentToFunctionsArguments, functionArguments, canEdit } = props; + const [functionDataType, setFunctionDataType] = useState(""); + const [confirmationDialog, setConfirmationDialog] = useState(false); const formik = useFormik({ initialValues: { @@ -109,12 +106,13 @@ export default function ArgumentSection(props: ArgumentsProps) { error={Boolean(formik.errors.dataType)} helperText={formik.errors.dataType} onChange={(evt) => { + setFunctionDataType(evt.target.value); formik.setFieldValue("dataType", evt.target.value); }} /> - {dataType && dataType === "Other" && ( + {functionDataType && functionDataType === "Other" && (
{ - // resetForm(); setConfirmationDialog(true); }} > @@ -160,6 +157,15 @@ export default function ArgumentSection(props: ArgumentsProps) {
+ setConfirmationDialog(false)} + onSubmit={() => { + resetForm(); + setFunctionDataType(""); + setConfirmationDialog(false); + }} + /> ); } diff --git a/src/CqlBuilderPanel/functionsSection/argumentSection/Arguments.tsx b/src/CqlBuilderPanel/functionsSection/argumentSection/Arguments.tsx index 69a7376..43d6280 100644 --- a/src/CqlBuilderPanel/functionsSection/argumentSection/Arguments.tsx +++ b/src/CqlBuilderPanel/functionsSection/argumentSection/Arguments.tsx @@ -4,13 +4,20 @@ import { getCoreRowModel, useReactTable, } from "@tanstack/react-table"; -import React, { useMemo } from "react"; +import React, { useCallback, useEffect, useMemo, useState } from "react"; import { FunctionArgument } from "../../../model/CqlBuilderLookup"; import tw from "twin.macro"; import "styled-components/macro"; +import ArrowDropUpIcon from "@mui/icons-material/ArrowDropUp"; +import ArrowDropDownIcon from "@mui/icons-material/ArrowDropDown"; +import DeleteOutlineIcon from "@mui/icons-material/DeleteOutline"; +import { Pagination } from "@madie/madie-design-system/dist/react"; +import { Stack } from "@mui/material"; +import ToolTippedIcon from "../../../toolTippedIcon/ToolTippedIcon"; type PropTypes = { functionArguments: Array; + handleDeleteArgument?: (argument) => void; canEdit: boolean; }; @@ -23,10 +30,72 @@ type RowDef = { const TH = tw.th`p-3 text-left text-sm font-bold capitalize`; -const Arguments = ({ functionArguments, canEdit }: PropTypes) => { +const Arguments = ({ + functionArguments, + handleDeleteArgument, + canEdit, +}: PropTypes) => { + const [visibleArguments, setVisibleArguments] = useState( + [] + ); + + // pagination utilities + const [totalPages, setTotalPages] = useState(0); + const [totalItems, setTotalItems] = useState(0); + const [visibleItems, setVisibleItems] = useState(0); + const [offset, setOffset] = useState(0); + const [currentLimit, setCurrentLimit] = useState(5); + const [currentPage, setCurrentPage] = useState(1); + + const managePagination = useCallback(() => { + if (functionArguments.length < currentLimit) { + setOffset(0); + setVisibleArguments([...functionArguments]); + setVisibleItems(functionArguments.length); + setTotalItems(functionArguments.length); + setTotalPages(1); + } else { + const start = (currentPage - 1) * currentLimit; + const end = start + currentLimit; + const newVisibleArguments = [...functionArguments].slice(start, end); + setVisibleArguments(newVisibleArguments); + setOffset(start); + setVisibleItems(newVisibleArguments.length); + setTotalItems(functionArguments.length); + setTotalPages(Math.ceil(functionArguments.length / currentLimit)); + } + }, [ + currentLimit, + currentPage, + functionArguments, + setOffset, + setVisibleArguments, + setVisibleItems, + setTotalItems, + setTotalPages, + ]); + + const canGoNext = (() => { + return currentPage < totalPages; + })(); + const canGoPrev = currentPage > 1; + + const handlePageChange = (e, v) => { + setCurrentPage(v); + }; + const handleLimitChange = (e) => { + setCurrentLimit(e.target.value); + setCurrentPage(1); + }; + + useEffect(() => { + managePagination(); + }, [functionArguments, currentPage, currentLimit]); + // table data - const data = functionArguments.map((argument) => { + const data = visibleArguments.map((argument) => { return { + // id: i, arrows: null, name: argument.argumentName, datatype: argument.dataType, @@ -50,6 +119,23 @@ const Arguments = ({ functionArguments, canEdit }: PropTypes) => { { header: "", accessorKey: "action", + cell: (row: any) => { + return ( + + {}, + }} + > + + + + ); + }, }, ]; }, [functionArguments]); @@ -110,25 +196,25 @@ const Arguments = ({ functionArguments, canEdit }: PropTypes) => { )} - {/* {functionArguments?.length > 0 && ( -
- -
- )} */} + {functionArguments?.length > 0 && ( +
+ +
+ )}
); diff --git a/src/CqlBuilderPanel/functionsSection/functionBuilder/FunctionBuilder.tsx b/src/CqlBuilderPanel/functionsSection/functionBuilder/FunctionBuilder.tsx index 0f4e3f2..1f4e47a 100644 --- a/src/CqlBuilderPanel/functionsSection/functionBuilder/FunctionBuilder.tsx +++ b/src/CqlBuilderPanel/functionsSection/functionBuilder/FunctionBuilder.tsx @@ -124,7 +124,6 @@ export default function FunctionBuilder({ showHeaderContent={argumentsEditorOpen} >