|
| 1 | +import React, { useState, useRef, useCallback, useMemo } from "react"; |
| 2 | +import { useDispatch, useSelector } from "react-redux"; |
| 3 | +import { useQuery } from "@tanstack/react-query"; |
| 4 | +import { useTranslation } from "react-i18next"; |
| 5 | +import { push } from "connected-react-router"; |
| 6 | +import { Submission } from "../types/submissions"; |
| 7 | +import { getSubmissionList } from "../api/queryServices/analyzeSubmissionServices"; |
| 8 | +import { formatDate } from "../helper/helper"; |
| 9 | +import { |
| 10 | + setAnalyzeSubmissionSort, |
| 11 | + setAnalyzeSubmissionPage, |
| 12 | + setAnalyzeSubmissionLimit, |
| 13 | +} from "../actions/analyzeSubmissionActions"; |
| 14 | +import { |
| 15 | + ReusableResizableTable, |
| 16 | + TableFooter, |
| 17 | + CustomButton, |
| 18 | + SortableHeader, |
| 19 | +} from "@formsflow/components"; |
| 20 | +import { MULTITENANCY_ENABLED } from "../constants"; |
| 21 | + |
| 22 | +interface Column { |
| 23 | + name: string; |
| 24 | + width: number; |
| 25 | + sortKey: string; |
| 26 | + resizable?: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +const TaskSubmissionList: React.FC = () => { |
| 30 | + const { t } = useTranslation(); |
| 31 | + const dispatch = useDispatch(); |
| 32 | + const scrollWrapperRef = useRef<HTMLDivElement>(null); |
| 33 | + const sortParams = useSelector( |
| 34 | + (state: any) => state?.analyzeSubmission.analyzeSubmissionSortParams ?? {} |
| 35 | + ); |
| 36 | + const limit = useSelector( |
| 37 | + (state: any) => state?.analyzeSubmission.limit ?? 10 |
| 38 | + ); |
| 39 | + const { page } = useSelector( |
| 40 | + (state: any) => state?.analyzeSubmission.page ?? 1 |
| 41 | + ); |
| 42 | + const tenantKey = useSelector( |
| 43 | + (state: any) => state.tenants?.tenantData?.tenantkey |
| 44 | + ); |
| 45 | + const redirectUrl = MULTITENANCY_ENABLED ? `/tenant/${tenantKey}/` : "/"; |
| 46 | + |
| 47 | + const columns: Column[] = useMemo( |
| 48 | + () => [ |
| 49 | + { name: "Submission ID", sortKey: "id", width: 200, resizable: true }, |
| 50 | + { name: "Form Name", sortKey: "formName", width: 200, resizable: true }, |
| 51 | + { name: "Submitter", sortKey: "createdBy", width: 200, resizable: true }, |
| 52 | + { |
| 53 | + name: "Submission Date", |
| 54 | + sortKey: "submissionDate", |
| 55 | + width: 180, |
| 56 | + resizable: true, |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "Status", |
| 60 | + sortKey: "applicationStatus", |
| 61 | + width: 160, |
| 62 | + resizable: true, |
| 63 | + }, |
| 64 | + { name: "", sortKey: "actions", width: 100 }, |
| 65 | + ], |
| 66 | + [] |
| 67 | + ); |
| 68 | + |
| 69 | + const activeSortKey = sortParams.activeKey; |
| 70 | + const activeSortOrder = sortParams?.[activeSortKey]?.sortOrder ?? "asc"; |
| 71 | + |
| 72 | + const { data } = useQuery({ |
| 73 | + queryKey: ["submissions", page, limit, activeSortKey, activeSortOrder], |
| 74 | + queryFn: () => |
| 75 | + getSubmissionList(limit, page, activeSortOrder, activeSortKey), |
| 76 | + keepPreviousData: true, |
| 77 | + staleTime: 0, |
| 78 | + }); |
| 79 | + |
| 80 | + const submissions = data?.submissions ?? []; |
| 81 | + const totalCount = data?.totalCount ?? 0; |
| 82 | + |
| 83 | + const handleSort = useCallback( |
| 84 | + (key: string) => { |
| 85 | + const newOrder = sortParams[key]?.sortOrder === "asc" ? "desc" : "asc"; |
| 86 | + const updatedSort = Object.fromEntries( |
| 87 | + Object.keys(sortParams).map((k) => [ |
| 88 | + k, |
| 89 | + { sortOrder: k === key ? newOrder : "asc" }, |
| 90 | + ]) |
| 91 | + ); |
| 92 | + dispatch(setAnalyzeSubmissionSort({ ...updatedSort, activeKey: key })); |
| 93 | + }, |
| 94 | + [dispatch, sortParams] |
| 95 | + ); |
| 96 | + |
| 97 | + const handlePageChange = useCallback( |
| 98 | + (pageNumber) => { |
| 99 | + dispatch(setAnalyzeSubmissionPage(pageNumber)); |
| 100 | + }, |
| 101 | + [dispatch, limit] |
| 102 | + ); |
| 103 | + |
| 104 | + const renderRow = (row: Submission) => ( |
| 105 | + <tr key={row.id}> |
| 106 | + <td>{row.id}</td> |
| 107 | + <td>{row.formName}</td> |
| 108 | + <td>{row.createdBy}</td> |
| 109 | + <td>{formatDate(row.created)}</td> |
| 110 | + <td>{row.applicationStatus}</td> |
| 111 | + <td> |
| 112 | + <CustomButton |
| 113 | + size="table-sm" |
| 114 | + variant="secondary" |
| 115 | + label={t("View")} |
| 116 | + onClick={() => dispatch(push(`${redirectUrl}application/${row.id}`))} |
| 117 | + dataTestId={`view-task-${row.id}`} |
| 118 | + ariaLabel={t("View details for task {{taskName}}", { |
| 119 | + taskName: row.formName ?? t("unnamed"), |
| 120 | + })} |
| 121 | + /> |
| 122 | + </td> |
| 123 | + </tr> |
| 124 | + ); |
| 125 | + |
| 126 | + const renderHeaderCell = useCallback( |
| 127 | + ( |
| 128 | + column: Column, |
| 129 | + index: number, |
| 130 | + columnsLength: number, |
| 131 | + currentResizingColumn: any, |
| 132 | + handleMouseDown: ( |
| 133 | + index: number, |
| 134 | + column: Column, |
| 135 | + e: React.MouseEvent |
| 136 | + ) => void |
| 137 | + ) => { |
| 138 | + const isLast = index === columnsLength - 1; |
| 139 | + const headerKey = column.sortKey || `col-${index}`; |
| 140 | + |
| 141 | + return ( |
| 142 | + <th |
| 143 | + key={`header-${headerKey}`} |
| 144 | + className="resizable-column" |
| 145 | + style={{ width: column.width }} |
| 146 | + data-testid={`column-header-${column.sortKey || "actions"}`} |
| 147 | + aria-label={column.name ? `${t(column.name)} ${t("column")}` : ""} |
| 148 | + > |
| 149 | + {!isLast && column.name ? ( |
| 150 | + <SortableHeader |
| 151 | + columnKey={column.sortKey} |
| 152 | + title={t(column.name)} |
| 153 | + currentSort={sortParams} |
| 154 | + handleSort={handleSort} |
| 155 | + className="w-100 d-flex justify-content-between align-items-center" |
| 156 | + dataTestId={`sort-header-${column.sortKey}`} |
| 157 | + ariaLabel={t("Sort by {{columnName}}", { |
| 158 | + columnName: t(column.name), |
| 159 | + })} |
| 160 | + /> |
| 161 | + ) : ( |
| 162 | + column.name && t(column.name) |
| 163 | + )} |
| 164 | + {column.resizable && ( |
| 165 | + <div |
| 166 | + className={`column-resizer ${ |
| 167 | + currentResizingColumn?.sortKey === column.sortKey |
| 168 | + ? "resizing" |
| 169 | + : "" |
| 170 | + }`} |
| 171 | + onMouseDown={(e) => handleMouseDown(index, column, e)} |
| 172 | + tabIndex={0} |
| 173 | + role="separator" |
| 174 | + aria-orientation="horizontal" |
| 175 | + data-testid={`column-resizer-${column.sortKey}`} |
| 176 | + aria-label={t("Resize {{columnName}} column", { |
| 177 | + columnName: t(column.name), |
| 178 | + })} |
| 179 | + /> |
| 180 | + )} |
| 181 | + </th> |
| 182 | + ); |
| 183 | + }, |
| 184 | + [t, sortParams, handleSort] |
| 185 | + ); |
| 186 | + |
| 187 | + const handleLimitChange = (newLimit: number) => { |
| 188 | + setAnalyzeSubmissionLimit(newLimit); |
| 189 | + setAnalyzeSubmissionPage(1); |
| 190 | + }; |
| 191 | + |
| 192 | + return ( |
| 193 | + <div className="container-wrapper" data-testid="table-container-wrapper"> |
| 194 | + <div className="table-outer-container"> |
| 195 | + <div |
| 196 | + className="table-scroll-wrapper resizable-scroll" |
| 197 | + ref={scrollWrapperRef} |
| 198 | + > |
| 199 | + <div className="resizable-table-container"> |
| 200 | + <ReusableResizableTable |
| 201 | + columns={columns} |
| 202 | + data={submissions} |
| 203 | + renderRow={renderRow} |
| 204 | + renderHeaderCell={renderHeaderCell} |
| 205 | + emptyMessage={t( |
| 206 | + "No submissions have been found. Try a different filter combination or contact your admin." |
| 207 | + )} |
| 208 | + onColumnResize={(newWidths) => |
| 209 | + //TBD |
| 210 | + console.log("Column resized:", newWidths) |
| 211 | + } |
| 212 | + tableClassName="resizable-table" |
| 213 | + headerClassName="resizable-header" |
| 214 | + containerClassName="resizable-table-container" |
| 215 | + scrollWrapperClassName="table-scroll-wrapper resizable-scroll" |
| 216 | + dataTestId="task-resizable-table" |
| 217 | + ariaLabel={t("submissions data table with resizable columns")} |
| 218 | + /> |
| 219 | + </div> |
| 220 | + </div> |
| 221 | + </div> |
| 222 | + |
| 223 | + {submissions.length > 0 && ( |
| 224 | + <table className="custom-tables" data-testid="table-footer-container"> |
| 225 | + <tfoot> |
| 226 | + <TableFooter |
| 227 | + limit={limit} |
| 228 | + activePage={page} |
| 229 | + totalCount={totalCount} |
| 230 | + handlePageChange={handlePageChange} |
| 231 | + onLimitChange={handleLimitChange} |
| 232 | + pageOptions={[ |
| 233 | + { text: "5", value: 5 }, |
| 234 | + { text: "25", value: 25 }, |
| 235 | + { text: "50", value: 50 }, |
| 236 | + { text: "100", value: 100 }, |
| 237 | + { text: "All", value: totalCount }, |
| 238 | + ]} |
| 239 | + dataTestId="submission-table-footer" |
| 240 | + ariaLabel={t("Table pagination controls")} |
| 241 | + pageSizeDataTestId="submission-page-size-selector" |
| 242 | + pageSizeAriaLabel={t("Select number of submissions per page")} |
| 243 | + paginationDataTestId="submission-pagination-controls" |
| 244 | + paginationAriaLabel={t("Navigate between submission pages")} |
| 245 | + /> |
| 246 | + </tfoot> |
| 247 | + </table> |
| 248 | + )} |
| 249 | + </div> |
| 250 | + ); |
| 251 | +}; |
| 252 | + |
| 253 | +export default TaskSubmissionList; |
0 commit comments