Skip to content

Commit

Permalink
Feat: ORV2-1500 Company Search Results (first pass) (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikataot authored Jan 12, 2024
1 parent bb7fa6c commit edf0482
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { useQuery } from "@tanstack/react-query";
import { memo, useCallback, useContext, useMemo, useState } from "react";

import {
MRT_ColumnDef,
MRT_PaginationState,
MRT_Row,
MaterialReactTable,
useMaterialReactTable,
} from "material-react-table";

import OnRouteBCContext from "../../../../common/authentication/OnRouteBCContext";
import { Optional } from "../../../../common/types/common";
import { USER_AUTH_GROUP } from "../../../../common/authentication/types";
import { getDataBySearch } from "../api/idirSearch";
import { SearchFields } from "../types/types";
import {
defaultTableInitialStateOptions,
defaultTableOptions,
defaultTableStateOptions,
} from "../../../../common/helpers/tableHelper";
import "./IDIRCompanySearchResults.scss";
import { CompanyProfile } from "../../../manageProfile/types/manageProfile";
import { CompanySearchResultColumnDef } from "../table/CompanySearchResultColumnDef";

/**
* Function to decide whether to show row actions icon or not.
* @param userAuthGroup The auth group the user belongs to.
* @returns boolean
*/
const shouldShowRowActions = (userAuthGroup: Optional<string>): boolean => {
if (!userAuthGroup) return false;
// Check if the user has PPC role to confirm
const allowableAuthGroups = [
USER_AUTH_GROUP.PPC_CLERK,
USER_AUTH_GROUP.ENFORCEMENT_OFFICER,
USER_AUTH_GROUP.SYSTEM_ADMINISTRATOR,
] as string[];
return allowableAuthGroups.includes(userAuthGroup);
};

/*
*
* The search results component uses Material React Table (MRT)
* For detailed documentation, see here:
* https://www.material-react-table.com/docs/getting-started/usage
*
*
*/
export const IDIRCompanySearchResults = memo(
({
searchParams,
}: {
/**
* The search parameters entered by the user.
*/
searchParams: SearchFields;
}) => {
const { searchValue, searchByFilter, searchEntity } = searchParams;
const { idirUserDetails } = useContext(OnRouteBCContext);
const [isActiveRecordsOnly, setIsActiveRecordsOnly] =
useState<boolean>(false);
const [pagination, setPagination] = useState<MRT_PaginationState>({
pageIndex: 0,
pageSize: 10,
});
// TODO: if data is [] AND current_user is PPC_ADMIN then (eventually)
// display the UX to allow the creation of a new Company Profile
const canCreateCompany = false;
const searchResultsQuery = useQuery(
[
"search-entity",
searchValue,
searchByFilter,
searchEntity,
pagination.pageIndex,
pagination.pageSize,
],
() =>
getDataBySearch(
{
searchByFilter,
searchEntity,
searchValue,
},
{ page: pagination.pageIndex, take: pagination.pageSize },
),
{
retry: 1, // retry once.
enabled: true,
refetchOnWindowFocus: false,
keepPreviousData: true,
},
);

const { data, isLoading, isError } = searchResultsQuery;

// Column definitions for the table
const columns = useMemo<MRT_ColumnDef<CompanyProfile>[]>(
() => CompanySearchResultColumnDef,
[],
);

const table = useMaterialReactTable({
...defaultTableOptions,
data: data?.items ?? [],
columns: columns,
initialState: {
...defaultTableInitialStateOptions,
sorting: [],
},
state: {
...defaultTableStateOptions,
isLoading,
showAlertBanner: isError,
showProgressBars: isLoading,
pagination,
},
autoResetPageIndex: false,
manualPagination: true,
rowCount: data?.meta?.totalItems ?? 0,
pageCount: data?.meta?.pageCount ?? 0,
onPaginationChange: setPagination,
enablePagination: true,
enableTopToolbar: true,
enableBottomToolbar: true,
enableRowSelection: false,
enableGlobalFilter: false,
renderToolbarInternalActions: () => (
<div className="toolbar-internal"></div>
),
muiToolbarAlertBannerProps: isError
? {
color: "error",
children: "Error loading data",
}
: undefined,
});

return (
<div className="table-container idir-search-results">
<MaterialReactTable table={table} />
</div>
);
},
);

IDIRCompanySearchResults.displayName = "SearchResults";
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@use "../../../../common/components/table/Table";

.idir-search-results {
&#{&} {
margin-top: 0;
padding-bottom: 12.5rem;
position: relative;

th:last-of-type, td:last-of-type {
padding: 0;
min-width: 0;
max-width: 3rem;
}
}

& &__row-actions {
padding: 0;
max-width: 3rem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ import { hasPermitExpired } from "../../../permits/helpers/permitState";
import { isPermitInactive } from "../../../permits/types/PermitStatus";
import { Permit } from "../../../permits/types/permit";
import { getDataBySearch } from "../api/idirSearch";
import { PermitSearchResultColumnDef } from "../table/Columns";
import { PermitSearchResultColumnDef } from "../table/PermitSearchResultColumnDef";
import { SearchFields } from "../types/types";
import { IDIRPermitSearchRowActions } from "./IDIRPermitSearchRowActions";
import {
defaultTableInitialStateOptions,
defaultTableOptions,
defaultTableStateOptions,
} from "../../../../common/helpers/tableHelper";
import "./IDIRSearchResults.scss";
import "./IDIRPermitSearchResults.scss";

/**
* Function to decide whether to show row actions icon or not.
Expand All @@ -51,7 +51,7 @@ const shouldShowRowActions = (userAuthGroup: Optional<string>): boolean => {
*
*
*/
export const IDIRSearchResults = memo(
export const IDIRPermitSearchResults = memo(
({
searchParams,
}: {
Expand Down Expand Up @@ -203,4 +203,4 @@ export const IDIRSearchResults = memo(
},
);

IDIRSearchResults.displayName = "SearchResults";
IDIRPermitSearchResults.displayName = "SearchResults";
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { Box } from "@mui/material";
import { memo } from "react";
import { useSearchParams } from "react-router-dom";
import { Banner } from "../../../../common/components/dashboard/Banner";
import { IDIRSearchResults } from "../components/IDIRSearchResults";
import {
SEARCH_BY_FILTERS,
SEARCH_ENTITIES,
SearchByFilter,
SearchEntity,
SearchFields,
} from "../types/types";
import { IDIRCompanySearchResults } from "../components/IDIRCompanySearchResults";
import { IDIRPermitSearchResults } from "../components/IDIRPermitSearchResults";

/**
* Returns a banner text based on the search criteria.
Expand Down Expand Up @@ -52,7 +54,8 @@ export const IDIRSearchResultsDashboard = memo(() => {
id={`layout-tabpanel-search-results`}
aria-labelledby={`layout-tab-search-results`}
>
<IDIRSearchResults searchParams={searchFields} />
{searchFields?.searchEntity === SEARCH_ENTITIES.COMPANY && <IDIRCompanySearchResults searchParams={searchFields} />}
{searchFields?.searchEntity === SEARCH_ENTITIES.PERMIT && <IDIRPermitSearchResults searchParams={searchFields} />}
</div>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { MRT_ColumnDef } from "material-react-table";
import {
dateTimeStringSortingFn,
formatCellValuetoDatetime
} from "../../../../common/helpers/tableHelper";
import { CompanyProfile } from "../../../manageProfile/types/manageProfile";

/*
*
* The Columns Options are from Material React Table.
* For a list of options, see here:
* https://www.material-react-table.com/docs/api/column-options
*
*/
export const CompanySearchResultColumnDef: MRT_ColumnDef<CompanyProfile>[] = [
{
accessorKey: "legalName",
header: "Company Name",
enableSorting: true,
sortingFn: "alphanumeric",
},
{
accessorKey: "alternateName",
header: "Doing Business As (DBA)",
enableSorting: true,
sortingFn: "alphanumeric",
},
{
accessorKey: "migratedClientHash",
header: "Client Number",
enableSorting: true,
sortingFn: "alphanumeric",
},
{
accessorKey: "mailingAddress",
header: "Company Address",
enableSorting: true,
sortingFn: "alphanumeric",
},
{
accessorKey: "primaryContact",
header: "Primary Contact",
enableSorting: true,
sortingFn: "alphanumeric",
},
];

0 comments on commit edf0482

Please sign in to comment.