All files / src/components/groupedGrandChild GroupedGrandChildDetailGroups.tsx

8.33% Statements 2/24
0% Branches 0/18
0% Functions 0/9
8.69% Lines 2/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190                                          66x                                           66x                                                                                                                                                                                                                                                                                                    
import React, { Dispatch, ReactElement, SetStateAction, useState } from "react";
import { Collapse, Empty, FormInstance, Space } from "antd";
import {
  CellComponentDisplayPage,
  FormProps,
  RecordProps,
  SortedRecordProps,
  TableMetaProps
} from "@props/RecordProps";
import { CaretRightOutlined, ArrowUpOutlined, ArrowDownOutlined } from "@ant-design/icons";
import GroupedGrandChildGroupTitle from "./GroupedGrandChildGroupTitle";
import GroupedGrandChildDetailGroup from "./GroupedGrandChildDetailGroup";
import GroupedGrandChildGroupExtra from "./GroupedGrandChildGroupExtra";
import GroupedGrandChildDetailFooter from "./GroupedGrandChildDetailFooter";
import { stopPropagationAndPreventDefault } from "@utils/ObjectUtils";
import { useTranslation } from "react-i18next";
import PageHeader from "../../form/PageHeader";
import { emptyMethod } from "@utils/Constants";
import update from "immutability-helper";
import { openInfoNotification } from "@utils/NotificationUtils";
 
const { Panel } = Collapse;
 
export interface GroupedGrandChildDetailGroupsProps {
  data: Array<RecordProps>;
  ownerId: number;
  ownerClass: string;
  reload: () => void;
  onExchange: (index: number, isDown: boolean) => void;
  sortedData: SortedRecordProps[][];
  setSortedData: Dispatch<SetStateAction<SortedRecordProps[][]>>;
  domainName: string;
  columns: Array<TableMetaProps>;
  page: CellComponentDisplayPage,
  zIndex: number;
  canDnd: boolean;
  summaryGroupTitleColumns: Array<TableMetaProps>;
  summaryGroupChildrenColumn: TableMetaProps;
  summaryGroupExtraColumns: Array<TableMetaProps>;
  form: FormInstance;
  column: TableMetaProps;
}
 
const GroupedGrandChildDetailGroups = (props: GroupedGrandChildDetailGroupsProps): ReactElement => {
  const {
    data,
    ownerId,
    ownerClass,
    reload,
    onExchange,
    sortedData,
    setSortedData,
    domainName,
    page,
    zIndex,
    columns,
    summaryGroupExtraColumns,
    summaryGroupTitleColumns,
    summaryGroupChildrenColumn,
    canDnd,
    form,
    column
  } = props;
 
  const { t } = useTranslation();
  const [updatedGroups, setUpdatedGroups] = useState<Array<number>>([]);
 
  const header = <PageHeader
    listForm={{ extInfo: { domainTitle: "" } } as FormProps}
    tableMode={"table-list"}
    domainName={domainName}
    data={[]}
    selectedData={[]}
    columns={columns}
    enumValues={{}}
    objectValues={{}}
    setData={emptyMethod}
    fetchDataCallback={() => {
      reload();
      openInfoNotification(t("Data refreshed"));
    }}
    zIndex={zIndex + 1}
    ownerClass={ownerClass}
    ownerId={ownerId}
    ownerColumn={column.backReferenceField}
    columnNameInOwnerClass={column.dataIndex}
    dataState={{}}
    dataDispatch={emptyMethod}
    disableSearch={true}
    disableCSV={true}
  />;
 
  if (data?.length === 0) {
    return (<>
      {header}
      <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
    </>);
  }
 
  return (<>
    {header}
    <Collapse
      bordered={false}
      defaultActiveKey={data.map((d, i) => i.toString())}
      expandIcon={({ isActive }) => <CaretRightOutlined rotate={isActive ? 90 : 0} />}
      className="site-collapse-custom-collapse">
      {data.map((d, idx) => {
        const header = <GroupedGrandChildGroupTitle
          record={d}
          columns={columns}
          displayColumns={summaryGroupTitleColumns}
          domainName={domainName}
          page={page} zIndex={zIndex} form={form}
        />;
        const extra = <GroupedGrandChildGroupExtra
          record={d}
          operationsProps={{
            ownerId,
            ownerClass,
          }}
          createChildProps={{
            columnNameInCurrentDomainClass: summaryGroupChildrenColumn.key,
            childrenDomainName: summaryGroupChildrenColumn.elementType ?? summaryGroupChildrenColumn.type,
          }}
          reload={() => {
            if (updatedGroups.indexOf(idx) === -1) {
              setUpdatedGroups(prevState => update(prevState, { $push: [idx] }));
            }
          }}
          columns={columns}
          displayColumns={summaryGroupExtraColumns}
          domainName={domainName}
          page={page} zIndex={zIndex}
          form={form}
          showOperations={true}
        />;
        const dndController = (canDnd) ? (
          <Space>
            <a
              href="/#"
              onClick={(event: React.MouseEvent<unknown>): void => {
                stopPropagationAndPreventDefault(event);
                onExchange(idx, false);
              }}
              style={idx === 0 ? { visibility: "hidden" } : {}}
              title={t("Move up")}
            ><ArrowUpOutlined /></a>
            <a href="/#"
              style={(idx === data.length - 1) ? { visibility: "hidden" } : {}}
              onClick={(event: React.MouseEvent<unknown>): void => {
                stopPropagationAndPreventDefault(event);
                onExchange(idx, true);
              }}
              title={t("Move down")}
            >
              <ArrowDownOutlined />
            </a>
          </Space>
        ) : (<></>);
        return (<Panel header={header}
          key={idx}
          className="site-collapse-custom-panel"
          extra={<Space>{extra}{dndController}</Space>}
        >
          {summaryGroupChildrenColumn != null && <GroupedGrandChildDetailGroup
            domainName={domainName}
            index={idx}
            sortedData={sortedData}
            setSortedData={setSortedData}
            record={d}
            displayColumn={summaryGroupChildrenColumn}
            page={page}
            zIndex={zIndex}
            updatedGroups={updatedGroups}
            setUpdatedGroups={setUpdatedGroups}
          />}
          {summaryGroupChildrenColumn &&
            <GroupedGrandChildDetailFooter
              domainName={domainName}
              record={d}
              displayColumn={summaryGroupChildrenColumn}
              zIndex={zIndex} />}
        </Panel>);
      })}
    </Collapse>
  </>);
};
 
export default GroupedGrandChildDetailGroups;