All files / src/apps/concepts/components ConceptsTableRow.tsx

23.08% Statements 9/39
0% Branches 0/38
0% Functions 0/21
23.08% Lines 9/39

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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315                                                                                                                  1x                                           1x                                                                 1x                             1x                             1x                             1x                           1x                                       1x                                                                         1x                                                                                                                                                                            
import React from "react";
import { APIConcept } from "../types";
import {
  TableRow,
  TableCell,
  Checkbox,
  Tooltip,
  IconButton
} from "@mui/material";
import { MoreVert as MoreVertIcon } from "@mui/icons-material";
import { Link } from "react-router-dom";
import { ConceptsActionMenu } from "./ConceptsActionMenu";
import { AlreadyAddedIcon } from "./AlreadyAddedIcon";
 
export function showEditMenuItem(
  concept: APIConcept,
  showingEditButtons: boolean,
  linkedSource: string | undefined,
  canModifyConcept: (concept: APIConcept) => boolean
) {
  // only allow edit of a concept we can modify and belongs to our linked source
  // the second condition prevents us editing non custom concepts in a collection
  return (
    showingEditButtons &&
    canModifyConcept(concept) &&
    linkedSource &&
    concept.url.includes(linkedSource)
  );
}
 
export function showRemoveFromDictionaryMenuItem(
  concept: APIConcept,
  showingEditButtons: boolean,
  linkedSource: string | undefined
) {
  // only allow manual removal of imported/ non custom concepts
  return (
    showingEditButtons && linkedSource && !concept.url.includes(linkedSource)
  );
}
/*
  Show the action icon only when the concept has 
  atleast one of the 3 options - edit, remove or add to Dictionary
  */
export function showActionIcon(
  concept: APIConcept,
  buttons: { [key: string]: boolean },
  linkedSource: string | undefined,
  canModifyConcept: (concept: APIConcept) => boolean
) {
  return (
    showEditMenuItem(concept, buttons.edit, linkedSource, canModifyConcept) ||
    showRemoveFromDictionaryMenuItem(concept, buttons.edit, linkedSource) ||
    buttons.addToDictionary
  );
}
 
const actionIcon = (
  index: number,
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void
) => {
  return (
    <Tooltip title="More actions" enterDelay={700}>
      <IconButton
        data-testid={"action-icon"}
        id={`${index}.menu-icon`}
        aria-controls={`${index}.menu`}
        aria-haspopup="true"
        onClick={event => toggleMenu(index, event)}
      >
        <MoreVertIcon />
      </IconButton>
    </Tooltip>
  );
};
 
const conceptNameCell = (
  toggleSelect: (event: React.MouseEvent<unknown>, uuid: string) => void,
  row: APIConcept,
  linkedDictionary?: string,
  dictionaryToAddTo?: string
) => {
  let link = row.version_url;
  const params = new URLSearchParams();
  if (linkedDictionary) {
    params.append("linkedDictionary", linkedDictionary);
  }
 
  if (dictionaryToAddTo) {
    params.append("dictionaryToAddTo", dictionaryToAddTo);
  }
 
  const queryString = params.toString();
  link = queryString ? row.version_url + "?" + queryString : row.version_url;
 
  return (
    <TableCell
      onClick={event => toggleSelect(event, row.uuid || "")}
      data-testclass="name"
      className={row.retired ? "retired" : ""}
      style={{ wordBreak: "break-all" }}
    >
      <Link onClick={e => e.stopPropagation()} to={link}>
        {row.display_name}
      </Link>
    </TableCell>
  );
};
 
const conceptClassCell = (
  toggleSelect: (event: React.MouseEvent<unknown>, uuid: string) => void,
  row: APIConcept
) => {
  return (
    <TableCell
      onClick={event => toggleSelect(event, row.uuid || "")}
      data-testclass="conceptClass"
      style={{ wordWrap: "break-word" }}
    >
      {row.concept_class}
    </TableCell>
  );
};
 
const conceptDataTypeCell = (
  toggleSelect: (event: React.MouseEvent<unknown>, uuid: string) => void,
  row: APIConcept
) => {
  return (
    <TableCell
      onClick={event => toggleSelect(event, row.uuid || "")}
      data-testclass="datatype"
      style={{ wordWrap: "break-word" }}
    >
      {row.datatype}
    </TableCell>
  );
};
 
const conceptSourceCell = (
  toggleSelect: (event: React.MouseEvent<unknown>, uuid: string) => void,
  row: APIConcept
) => {
  return (
    <TableCell
      onClick={event => toggleSelect(event, row.uuid || "")}
      data-testclass="source"
      style={{ wordWrap: "break-word" }}
    >
      {row.source}
    </TableCell>
  );
};
 
const conceptIDCell = (
  toggleSelect: (event: React.MouseEvent<unknown>, uuid: string) => void,
  row: APIConcept
) => {
  return (
    <TableCell
      onClick={event => toggleSelect(event, row.uuid || "")}
      style={{ wordBreak: "break-all" }}
    >
      {row.id}
    </TableCell>
  );
};
 
const checkBoxCell = (
  toggleSelect: (event: React.MouseEvent<unknown>, id: string) => void,
  row: APIConcept,
  isItemSelected: boolean,
  labelId: string
) => {
  return (
    <TableCell padding="checkbox">
      <Checkbox
        // ideally, we would have made this apply to the entire row, but there
        // seems to be a problem with an implicit click when the row popup closes
        onClick={event => toggleSelect(event, row.id)}
        checked={isItemSelected}
        inputProps={{ "aria-labelledby": labelId }}
        color="secondary"
      />
    </TableCell>
  );
};
 
const actionCell = (
  row: APIConcept,
  buttons: { [key: string]: boolean },
  canModifyConcept: (concept: APIConcept) => boolean,
  index: number,
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void,
  menu: { index: number; anchor: null | HTMLElement },
  removeConceptsFromDictionary: (conceptVersionUrls: string[]) => void,
  addConceptsToDictionary: Function,
 
  linkedSource: string | undefined,
  linkedDictionary: string | undefined
) => {
  return (
    <TableCell padding="checkbox">
      {!showActionIcon(row, buttons, linkedSource, canModifyConcept)
        ? null
        : actionIcon(index, toggleMenu)}
      <ConceptsActionMenu
        index={index}
        row={row}
        buttons={buttons}
        toggleMenu={toggleMenu}
        menu={menu}
        canModifyConcept={canModifyConcept}
        removeConceptsFromDictionary={removeConceptsFromDictionary}
        addConceptsToDictionary={addConceptsToDictionary}
        linkedSource={linkedSource}
        linkedDictionary={linkedDictionary}
      />
    </TableCell>
  );
};
 
const AlreadyCheckedCell = () => (
  <TableCell padding="checkbox">
    <AlreadyAddedIcon />
  </TableCell>
);
 
interface ConceptsTableRowProps {
  row: APIConcept;
  index: number;
  selected: string[];
  toggleSelect: (event: React.MouseEvent<unknown>, uuid: string) => void;
  linkedDictionary?: string;
  buttons: { [key: string]: boolean };
  linkedSource: string | undefined;
  canModifyConcept: (concept: APIConcept) => boolean;
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void;
  menu: { index: number; anchor: null | HTMLElement };
  removeConceptsFromDictionary: (conceptVersionUrls: string[]) => void;
  addConceptsToDictionary: Function;
  dictionaryToAddTo?: string;
  isItemSelected: boolean;
  labelId: string;
}
 
export function ConceptsTableRow(props: ConceptsTableRowProps) {
  const {
    row,
    index,
    selected,
    toggleSelect,
    linkedDictionary,
    buttons,
    linkedSource,
    canModifyConcept,
    toggleMenu,
    menu,
    removeConceptsFromDictionary,
    addConceptsToDictionary,
    dictionaryToAddTo,
    isItemSelected,
    labelId
  } = props;
  return (
    <TableRow
      hover
      data-testrowclass="conceptRow"
      role="checkbox"
      aria-checked={isItemSelected}
      tabIndex={-1}
      key={`${row.id}-${index}`}
      selected={isItemSelected}
      className={row?.added ? "added" : ""}
      style={
        row?.added
          ? { backgroundColor: "#bbc5fb" }
          : { backgroundColor: "none" }
      }
    >
      {selected.length <= 0
        ? null
        : checkBoxCell(toggleSelect, row, isItemSelected, labelId)}
      {conceptNameCell(toggleSelect, row, linkedDictionary, dictionaryToAddTo)}
      {conceptClassCell(toggleSelect, row)}
      {conceptDataTypeCell(toggleSelect, row)}
      {conceptSourceCell(toggleSelect, row)}
      {conceptIDCell(toggleSelect, row)}
      {row.added
        ? AlreadyCheckedCell()
        : actionCell(
            row,
            buttons,
            canModifyConcept,
            index,
            toggleMenu,
            menu,
            removeConceptsFromDictionary,
            addConceptsToDictionary,
            linkedSource,
            linkedDictionary
          )}
    </TableRow>
  );
}