All files / app/features/communities/events AttendanceMenu.tsx

90.32% Statements 28/31
90.47% Branches 19/21
71.42% Functions 5/7
90% Lines 27/30

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 1341x 1x 1x 1x 1x 1x 1x 1x 1x 1x   7x             9x                         13x 13x   13x 13x   13x   13x 2x     2x     13x     13x 2x 2x       13x 13x                                                                                                                           2x                              
import { ListItemText } from "@mui/material";
import makeStyles from "@mui/styles/makeStyles";
import Button from "components/Button";
import { CheckIcon, ExpandLessIcon, ExpandMoreIcon } from "components/Icons";
import Menu, { MenuItem } from "components/Menu";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import { AttendanceState } from "proto/events_pb";
import { useState } from "react";
import { theme } from "theme";
 
const useStyles = makeStyles((theme) => ({
  menuListItem: {
    display: "flex",
    gap: theme.spacing(2),
  },
}));
 
export default function AttendanceMenu({
  loading,
  onChangeAttendanceState,
  attendanceState,
  id,
  disabled = false,
}: {
  loading: boolean;
  onChangeAttendanceState: (attendanceState: AttendanceState) => void;
  attendanceState: AttendanceState;
  id: string;
  disabled: boolean;
}) {
  const { t } = useTranslation([COMMUNITIES]);
  const classes = useStyles();
 
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
  const open = Boolean(anchorEl);
  const isAttending =
    attendanceState === AttendanceState.ATTENDANCE_STATE_GOING;
 
  const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
    Iif (!isAttending) {
      onChangeAttendanceState(AttendanceState.ATTENDANCE_STATE_GOING);
    } else {
      setAnchorEl(event.currentTarget);
    }
  };
  const handleClose = () => {
    setAnchorEl(null);
  };
  const handleChangeAttendanceState = (attendanceState: AttendanceState) => {
    onChangeAttendanceState(attendanceState);
    setAnchorEl(null);
  };
 
  /* @todo: this id can be unique and not passed from outside when we have React 18 useId */
  const buttonId = `${id}-button`;
  const menuId = `${id}-menu`;
 
  return (
    <>
      <Button
        id={buttonId}
        aria-controls={open ? menuId : undefined}
        aria-haspopup="true"
        aria-expanded={open ? "true" : undefined}
        onClick={handleClick}
        loading={loading}
        variant={isAttending ? "outlined" : "contained"}
        disabled={disabled}
        sx={{
          color: isAttending
            ? theme.palette.common.black
            : theme.palette.common.white,
          borderColor: theme.palette.grey[300],
 
          "&:hover": {
            borderColor: theme.palette.grey[300],
            backgroundColor: "#3135390A",
          },
        }}
      >
        {isAttending
          ? t("communities:going_to_event")
          : t("communities:join_event")}
 
        {isAttending && (open ? <ExpandLessIcon /> : <ExpandMoreIcon />)}
      </Button>
 
      <Menu
        id={menuId}
        anchorEl={anchorEl}
        open={open}
        onClose={handleClose}
        MenuListProps={{
          "aria-labelledby": buttonId,
        }}
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "right",
        }}
        transformOrigin={{
          vertical: "top",
          horizontal: "right",
        }}
      >
        <MenuItem
          onClick={() => {
            handleChangeAttendanceState(AttendanceState.ATTENDANCE_STATE_GOING);
          }}
          classes={{ root: classes.menuListItem }}
        >
          <ListItemText primary={t("communities:going_to_event")} />
          {attendanceState === AttendanceState.ATTENDANCE_STATE_GOING && (
            <CheckIcon />
          )}
        </MenuItem>
        <MenuItem
          onClick={() => {
            handleChangeAttendanceState(
              AttendanceState.ATTENDANCE_STATE_NOT_GOING
            );
          }}
          classes={{ root: classes.menuListItem }}
        >
          <ListItemText primary={t("communities:not_going_to_event")} />
          {attendanceState === AttendanceState.ATTENDANCE_STATE_NOT_GOING && (
            <CheckIcon />
          )}
        </MenuItem>
      </Menu>
    </>
  );
}