All files / src/components Topbar.tsx

73.91% Statements 34/46
50% Branches 7/14
28.57% Functions 4/14
91.89% Lines 34/37

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                        4x                   4x               4x               4x                         4x                   4x           8x                   4x                           4x                           4x               4x                 4x       4x                         8x 8x 8x 8x 8x 8x 8x 8x   8x 4x     8x   8x   8x   8x   8x   8x   8x   8x   8x 4x           8x                                                                                                
import React, { useState, MouseEvent, ChangeEvent, useEffect, useCallback, useContext, useRef } from 'react';
import { ReactComponent as Close } from '../assets/close.svg';
import ProfileIcon from '../assets/account.svg';
import { Profile } from './Profile';
import { Dropdown } from './Dropdown';
import styled from 'styled-components/macro';
import ClickAwayListener from 'react-click-away-listener';
import { SelectMenu } from './SelectMenu';
import { studentsContext } from '../contexts/StudentsProvider';
import { CASContext } from '../contexts/CASProvider';
import { render } from 'react-dom';
 
const Topbar = styled.div`
  background-color: #e3e5ed;
  height: 80px;
  padding: 5px;
  font-size: 24px;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
`;
 
const LogoWrapper = styled.div`
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex: 2;
  margin-left: 10px;
`;
 
const Logo = styled.img`
  width: 70px;
  height: 70px;
  @media only screen and (max-width: 1533px) {
    flex: auto;
  }
`;
 
const Text = styled.div`
  margin-left: 10px;
  font-size: 1.4rem;
  user-select: none;
  @media only screen and (max-width: 1533px) {
    display: none;
  }
  @media only screen and (max-width: 1828px) {
    margin-right: 10px;
    text-align: center;
  }
`;
 
const FlexboxColumn = styled.div`
  display: flex;
  flex-direction: column;
  flex: 12;
`;
 
type InputWrapperProps = {
  isStudent: boolean;
};
 
const InputWrapper = styled.div<InputWrapperProps>`
  width: 100%;
  display: flex;
  margin-top: 15px;
  max-height: 40px;
  background-color: #f2f4f7;
  border-radius: ${({ isStudent }) => (isStudent ? ` 6px 6px 6px 6px` : ` 0px 6px 6px 0px`)};
  padding-left: 6px;
  &:hover {
    background-color: #ffffff;
  }
  &:hover > input {
    background-color: #ffffff;
  }
`;
 
const Input = styled.input`
  font-family: 'Roboto', sans-serif;
  font-size: 18px;
  background-color: #f1f2f5;
  height: 40px;
  max-height: 40px;
  width: 100%;
  border: none;
  margin-left: 5px;
  &:focus {
    outline: none;
  }
`;
 
const CloseIcon = styled(Close)`
  align-self: center;
  width: 30px;
  height: 30px;
  margin-right: 5px;
  @media only screen and (max-width: 670px) {
    width: 25px;
  }
  cursor: pointer;
  :hover {
    fill: grey;
  }
`;
 
const IconWrapper = styled.div`
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 335px;
  margin-right: 10px;
`;
 
const Icon = styled.img`
  width: 40px;
  margin: 5px;
  cursor: pointer;
  @media only screen and (max-width: 670px) {
    width: 35px;
  }
`;
 
export const Flexbox = styled.div`
  display: flex;
`;
 
export const SelectedStudent = styled.div`
  width: 100%;
  display: flex;
  justify-content: center;
  margin-right: 10px;
  margin-left: 10px;
`;
 
interface TopbarProps {
  handleTransfer: (e: MouseEvent) => void;
}
 
export default function ({ handleTransfer }: TopbarProps) {
  const { selectedStudent } = useContext(studentsContext)!;
  const { role } = useContext(CASContext)!;
  const [clearInput, setClearInput] = useState(false);
  const [isPolish, setIsPolish] = useState(false);
  const [anchorEl, setAnchorEl] = useState<HTMLImageElement | null>(null);
  const [open, setOpen] = useState(false);
  const [input, setInput] = useState('');
  const [selectedOption, setSelectedOption] = useState(role === 'STUDENT' ? 'przedmioty' : 'studenci');
 
  useEffect(() => {
    role && setSelectedOption(role === 'STUDENT' ? 'przedmioty' : 'studenci');
  }, [role]);
 
  const changeSelectedOption = (option: string) => setSelectedOption(option);
 
  const onLangChange = () => setIsPolish(!isPolish);
 
  const handleProfile = (event: MouseEvent<HTMLImageElement>) => setAnchorEl(event.currentTarget);
 
  const handleCloseProfile = () => setAnchorEl(null);
 
  const handleClearInput = useCallback(() => setClearInput((clearInput) => !clearInput), []);
 
  const handleChange = (event: ChangeEvent<HTMLInputElement>) => setInput(event.target.value);
 
  const handleShowDropdown = () => setOpen(true);
 
  const handleCloseDropdown = () => setOpen(false);
 
  useEffect(() => {
    Iif (clearInput) {
      setInput('');
      handleClearInput();
    }
  }, [clearInput, handleClearInput]);
 
  return (
    <Topbar>
      <LogoWrapper>
        <Logo alt="logo" src="https://plannaplan.pl/img/logo.svg" />
        <Text> plan na plan </Text>
      </LogoWrapper>
      <FlexboxColumn>
        <ClickAwayListener onClickAway={handleCloseDropdown}>
          <Flexbox>
            {role !== 'STUDENT' && (
              <SelectMenu
                changeSelectedOption={changeSelectedOption}
                selectedOption={selectedOption}
                changeDropdownOpen={setOpen}
              />
            )}
            <InputWrapper isStudent={role === 'STUDENT'}>
              <Input
                placeholder={`Wyszukaj ${selectedOption === 'studenci' ? 'studentów...' : 'przedmioty...'}`}
                onChange={handleChange}
                value={input}
                onFocus={() => {
                  handleShowDropdown();
                }}
              />
              <CloseIcon onClick={handleClearInput} />
            </InputWrapper>
          </Flexbox>
          <Dropdown
            open={open}
            input={input}
            handleCloseDropdown={handleCloseDropdown}
            selectedOption={selectedOption}
          />
        </ClickAwayListener>
      </FlexboxColumn>
      <IconWrapper>
        <SelectedStudent>{selectedStudent?.email.replace(/@st.amu.edu.pl/, '')}</SelectedStudent>
 
        {/* <Text>Maciej Głowacki</Text> */}
        {/* <Icon alt="transfer" src={Transfer} onClick={handleTransfer} /> */}
        {/* <Icon alt="change_language" src={isPolish ? EnglishIcon : PolishIcon} onClick={onLangChange} /> */}
        <Icon alt="profile" src={ProfileIcon} onClick={handleProfile} />
        <Profile anchorEl={anchorEl} handleClose={handleCloseProfile} />
      </IconWrapper>
    </Topbar>
  );
}