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

27.27% Statements 6/22
0% Branches 0/10
0% Functions 0/11
28.57% Lines 6/21

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                            1x                                         1x                                           1x                                         1x                                       1x                                         1x                                                                                                                                                        
import React from "react";
import { Menu, MenuItem } from "@mui/material";
import { APIConcept } from "../types";
import {
  showEditMenuItem,
  showRemoveFromDictionaryMenuItem
} from "./ConceptsTableRow";
import { Link } from "react-router-dom";
import {
  Add as AddIcon,
  DeleteSweepOutlined as DeleteIcon,
  EditOutlined as EditIcon
} from "@mui/icons-material";
 
const addConceptsToDictionaryMenu = (
  addConceptsToDictionary: Function,
  row: APIConcept,
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void,
  index: number
) => {
  return (
    <MenuItem
      onClick={() => {
        if (addConceptsToDictionary) addConceptsToDictionary([row]);
        toggleMenu(index);
      }}
    >
      <AddIcon/> Add to dictionary
    </MenuItem>
  );
};
 
const removeConceptsFromDictionaryMenu = (
  removeConceptsFromDictionary: (conceptVersionUrls: string[]) => void,
  row: APIConcept,
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void,
  index: number
) => {
  return (
    <MenuItem
      onClick={() => {
        if (removeConceptsFromDictionary)
          removeConceptsFromDictionary([row.version_url]);
        toggleMenu(index);
      }}
    >
      <DeleteIcon /> Remove
    </MenuItem>
  );
};
 
const editConceptMenu = (
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void,
  index: number,
  row: APIConcept,
  linkedDictionary: string | undefined
) => {
  return (
    <MenuItem onClick={() => toggleMenu(index)}>
      <Link
        className="buttonLink"
        to={`${row.version_url}edit/?linkedDictionary=${linkedDictionary}`}
      >
        <EditIcon /> Edit
      </Link>
    </MenuItem>
  );
};
 
const addMenu = (
  index: number,
  row: APIConcept,
  buttons: { [key: string]: boolean },
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void,
  addConceptsToDictionary: Function
) => {
  return !buttons.addToDictionary
    ? null
    : addConceptsToDictionaryMenu(
        addConceptsToDictionary,
        row,
        toggleMenu,
        index
      );
};
 
const removeMenu = (
  index: number,
  row: APIConcept,
  buttons: { [key: string]: boolean },
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void,
  linkedSource: string | undefined,
  removeConceptsFromDictionary: (conceptVersionUrls: string[]) => void
) => {
  return !showRemoveFromDictionaryMenuItem(row, buttons.edit, linkedSource)
    ? null
    : removeConceptsFromDictionaryMenu(
        removeConceptsFromDictionary,
        row,
        toggleMenu,
        index
      );
};
 
const editMenu = (
  index: number,
  row: APIConcept,
  buttons: { [key: string]: boolean },
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void,
  canModifyConcept: (concept: APIConcept) => boolean,
  linkedSource: string | undefined,
  linkedDictionary: string | undefined
) => {
  return !showEditMenuItem(row, buttons.edit, linkedSource, canModifyConcept)
    ? null
    : editConceptMenu(toggleMenu, index, row, linkedDictionary);
};
 
interface ConceptsActionMenuProps {
  index: number;
  row: APIConcept;
  buttons: { [key: string]: boolean };
  toggleMenu: (
    index: number,
    event?: React.MouseEvent<HTMLButtonElement, MouseEvent> | undefined
  ) => void;
  menu: { index: number; anchor: null | HTMLElement };
  canModifyConcept: (concept: APIConcept) => boolean;
  removeConceptsFromDictionary: (conceptVersionUrls: string[]) => void;
  addConceptsToDictionary: Function;
  linkedSource: string | undefined;
  linkedDictionary: string | undefined;
}
 
export function ConceptsActionMenu(props: ConceptsActionMenuProps) {
  const {
    index,
    row,
    buttons,
    toggleMenu,
    menu,
    canModifyConcept,
    removeConceptsFromDictionary,
    addConceptsToDictionary,
    linkedSource,
    linkedDictionary
  } = props;
  return (
    <Menu
      anchorEl={menu.anchor}
      id={`${index}.menu`}
      open={index === menu.index}
      onClose={() => toggleMenu(index)}
    >
      {editMenu(
        index,
        row,
        buttons,
        toggleMenu,
        canModifyConcept,
        linkedSource,
        linkedDictionary
      )}
      {removeMenu(
        index,
        row,
        buttons,
        toggleMenu,
        linkedSource,
        removeConceptsFromDictionary
      )}
      {addMenu(index, row, buttons, toggleMenu, addConceptsToDictionary)}
    </Menu>
  );
}
 
export default ConceptsActionMenu;