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

8.7% Statements 2/23
0% Branches 0/14
0% Functions 0/10
8.7% 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 191 192 193 194 195 196 197 198 199 200 201                                        1x                         1x                                                                                                                                                                                                                                                                                                                                              
import React from "react";
import { Link } from "react-router-dom";
import { Fab, Menu, MenuItem, Theme, Tooltip } from "@mui/material";
import {
  CONCEPT_CLASSES,
  PREFERRED_SOURCES_VIEW_ONLY,
  useAnchor
} from "../../../utils";
import { Add as AddIcon } from "@mui/icons-material";
import { createStyles, makeStyles } from "@mui/styles";
 
interface Props {
  canModifyDictionary: boolean;
  canModifySource: boolean;
  containerUrl: string;
  gimmeAUrl: Function;
  linkedSource?: string;
  preferredSource: string;
}
 
const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    link: {
      textDecoration: "none",
      color: "inherit",
      width: "100%"
    },
    largerTooltip: {
      fontSize: "larger"
    }
  })
);
 
const AddConceptsIcon: React.FC<Props> = ({
  canModifyDictionary,
  canModifySource,
  containerUrl,
  gimmeAUrl,
  linkedSource,
  preferredSource
}) => {
  const classes = useStyles();
 
  const [addNewAnchor, handleAddNewClick, handleAddNewClose] = useAnchor();
  const [customAnchor, handleCustomClick, handleCustomClose] = useAnchor();
  const [
    importExistingAnchor,
    handleImportExistingClick,
    handleImportExistingClose
  ] = useAnchor();
 
  const getAddConceptIcon = () => {
    return canModifyDictionary ? (
      <Tooltip title="Add concepts" data-testid="addConceptsIcon">
        <Fab onClick={handleAddNewClick} color="primary" className="fab">
          <AddIcon />
        </Fab>
      </Tooltip>
    ) : (
      <Tooltip title="Create custom concept" data-testid="addCustomConceptIcon">
        <Fab
          onClick={e => {
            handleCustomClick(e);
            handleAddNewClose();
          }}
          color="primary"
          className="fab"
        >
          <AddIcon />
        </Fab>
      </Tooltip>
    );
  };
 
  const getTitleBasedOnLinkedSource = () => {
    return linkedSource ? (
      ""
    ) : (
      <span className={classes.largerTooltip}>
        This dictionary doesn't have a linked source attached to it. You'll need
        to
        <Link
          to={`${containerUrl}edit/?createLinkedSource=true&next=${gimmeAUrl()}`}
        >
          create one
        </Link>{" "}
        to keep your custom concepts.
      </span>
    );
  };
 
  const getMenuForAddConceptIcon = () => {
    return canModifyDictionary ? (
      <Menu
        anchorEl={addNewAnchor}
        keepMounted
        open={Boolean(addNewAnchor)}
        onClose={handleAddNewClose}
      >
        <MenuItem
          onClick={e => {
            handleImportExistingClick(e);
            handleAddNewClose();
          }}
        >
          Import existing concept
        </MenuItem>
        <Tooltip title={getTitleBasedOnLinkedSource()}>
          <span>
            <MenuItem
              disabled={!linkedSource}
              onClick={e => {
                handleCustomClick(e);
                handleAddNewClose();
              }}
            >
              Create custom concept
            </MenuItem>
          </span>
        </Tooltip>
      </Menu>
    ) : null;
  };
 
  const getAddConceptsIconWithMenu = () => {
    return canModifyDictionary || canModifySource ? (
      <>
        {getAddConceptIcon()}
        {getMenuForAddConceptIcon()}
      </>
    ) : null;
  };
 
  return (
    <>
      {getAddConceptsIconWithMenu()}
 
      <Menu
        anchorEl={customAnchor}
        keepMounted
        open={Boolean(customAnchor)}
        onClose={handleCustomClose}
        data-testid="createCustomConceptMenu"
      >
        {CONCEPT_CLASSES.slice(0, 9).map((conceptClass, index) => (
          <MenuItem onClick={handleCustomClose} key={index}>
            <Link
              className={classes.link}
              to={
                canModifySource
                  ? `${containerUrl}concepts/new/?conceptClass=${conceptClass}`
                  : `${linkedSource}concepts/new/?conceptClass=${conceptClass}&linkedDictionary=${containerUrl}`
              }
            >
              {conceptClass} Concept
            </Link>
          </MenuItem>
        ))}
        <MenuItem onClick={handleCustomClose}>
          <Link
            className={classes.link}
            to={
              canModifySource
                ? `${containerUrl}concepts/new/`
                : `${linkedSource}concepts/new/?linkedDictionary=${containerUrl}`
            }
          >
            Other kind
          </Link>
        </MenuItem>
      </Menu>
      <Menu
        anchorEl={importExistingAnchor}
        keepMounted
        open={Boolean(importExistingAnchor)}
        onClose={handleImportExistingClose}
        data-testid="importExistingConceptMenu"
      >
        <MenuItem onClick={handleImportExistingClose}>
          <Link
            className={classes.link}
            to={`${PREFERRED_SOURCES_VIEW_ONLY[preferredSource]}concepts/?addToDictionary=${containerUrl}`}
          >
            Pick concepts
          </Link>
        </MenuItem>
        <MenuItem onClick={handleImportExistingClose}>
          <Link
            className={classes.link}
            to={`${containerUrl}add/?fromSource=${preferredSource}`}
          >
            Add bulk concepts
          </Link>
        </MenuItem>
      </Menu>
    </>
  );
};
 
export default AddConceptsIcon;