All files / src/components Profile.tsx

79.03% Statements 49/62
64.28% Branches 9/14
84.21% Functions 16/19
80% Lines 48/60

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                                        28x 28x 28x   28x   28x 28x 8x 7x         28x 4x   28x 2x 2x 2x 2x     28x 2x 2x 2x 2x     28x 1x 1x       1x 1x 1x     1x   28x                                                             2x 2x                         2x 2x                                     15x 15x 15x   15x       15x       15x               15x 2x         15x 12x 12x 9x     15x 15x     15x                       3x                  
import React, {useEffect, useRef, useState} from 'react';
import {Menu, Dropdown} from 'antd';
import '../pages/spacePage/space.css';
import {
  AvatarImageEnum,
  avatarImageMDs,
} from '../utils/pixiUtils/metaData/ImageMetaData';
import {ProfileDropdownOnOff} from './Navigation';
import {isMobile} from '../utils/AgentCheck';
 
export interface ProfileProps {
  profileDropdownOnOff: ProfileDropdownOnOff;
  nickname: string;
  setNickname: (nickname: string) => void;
  avatar: AvatarImageEnum;
  setAvatar: (avatar: AvatarImageEnum) => void;
  setVisible: React.Dispatch<React.SetStateAction<boolean>> | null;
}
 
export function ProfileDropDown(props: ProfileProps): JSX.Element {
  const [newNickname, setNewNickname] = useState(props.nickname);
  const [newAvatar, setNewAvatar] = useState(props.avatar);
  const numberOfAvatars = avatarImageMDs.length;
 
  const inputRef = useRef<HTMLInputElement>(null);
 
  if (!isMobile()) {
    if (inputRef.current) {
      setTimeout(() => {
        inputRef.current?.focus();
      }, 50);
    }
  }
 
  const onNicknameInput = (e: React.ChangeEvent<HTMLInputElement>) => {
    setNewNickname(e.target.value);
  };
  const onLeftClick = () => {
    setNewAvatar(beforeAvatar => {
      let newAvatar = beforeAvatar - 1;
      if (newAvatar < 0) newAvatar = numberOfAvatars - 1;
      return newAvatar;
    });
  };
  const onRightClick = () => {
    setNewAvatar(beforeAvatar => {
      let newAvatar = beforeAvatar + 1;
      Iif (newAvatar >= numberOfAvatars) newAvatar = 0;
      return newAvatar;
    });
  };
  const onProfileChangeClick = () => {
    let nextNickname = newNickname;
    Iif (newNickname === '') {
      nextNickname = `익명의 ${avatarImageMDs[newAvatar].avatarInitialName}`;
      setNewNickname(nextNickname);
    }
    props.setAvatar(newAvatar);
    props.setNickname(nextNickname);
    Iif (!props.setVisible) {
      return;
    }
    props.setVisible(false);
  };
  return (
    <Menu className="navbar_profile">
      <Menu.Item key="1" disabled={true} className="profile_title">
        프로필 설정
      </Menu.Item>
      <Menu.Divider></Menu.Divider>
      <Menu.Item key="2" disabled={true}>
        <div className="name_title">이름</div>
        <form
          onSubmit={e => {
            e.preventDefault();
            onProfileChangeClick();
            props.profileDropdownOnOff.on = false;
          }}
        >
          <div className="profile_input">
            <input
              ref={inputRef}
              data-testid="profileDropdownInputTestId"
              maxLength={10}
              value={newNickname}
              onChange={onNicknameInput}
            />
          </div>
        </form>
        <div className="avatar_title">아바타</div>
        <div className="profile_avatar">
          <button
            className="profile_left_button"
            data-testid="profileDropdownLeftButtonTestId"
            onClick={e => {
              e.preventDefault();
              onLeftClick();
            }}
          >
            <img src="./assets/navigation/profile_left_button.png"></img>
          </button>
          <img
            className="avatar_preview"
            src={avatarImageMDs[newAvatar].avatarProfileSrc}
          ></img>
          <button
            className="profile_right_button"
            data-testid="profileDropdownRightButtonTestId"
            onClick={e => {
              e.preventDefault();
              onRightClick();
            }}
          >
            <img src="./assets/navigation/profile_right_button.png"></img>
          </button>
        </div>
      </Menu.Item>
      <Menu.Item
        key="3"
        className="profile_button"
        onClick={onProfileChangeClick}
      >
        <div className="change_button">변경</div>
      </Menu.Item>
    </Menu>
  );
}
 
function Profile(props: ProfileProps): JSX.Element {
  const [nickname, setNickname] = useState(props.nickname);
  const [avatar, setAvatar] = useState(props.avatar);
  const [visible, setVisible] = useState(props.profileDropdownOnOff.on);
 
  const newSetNickname = (nickname: string): void => {
    props.setNickname(nickname);
    setNickname(nickname);
  };
  const newSetAvatar = (avatar: AvatarImageEnum): void => {
    props.setAvatar(avatar);
    setAvatar(avatar);
  };
  const profileDropDownProps: ProfileProps = {
    profileDropdownOnOff: props.profileDropdownOnOff,
    nickname: nickname,
    setNickname: newSetNickname,
    avatar: avatar,
    setAvatar: newSetAvatar,
    setVisible: setVisible,
  };
  const onEscKeyDown = (event: KeyboardEvent) => {
    Iif (event.key === 'Escape') {
      props.profileDropdownOnOff.on = false;
      setVisible(false);
    }
  };
  useEffect(() => {
    window.addEventListener('keydown', onEscKeyDown);
    return () => {
      window.removeEventListener('keydown', onEscKeyDown);
    };
  }, []);
  useEffect(() => {
    props.profileDropdownOnOff.on = visible;
  }, [visible]);
 
  return (
    <Dropdown
      placement={'topLeft'}
      visible={visible}
      data-testid="dropDownTestId"
      onVisibleChange={setVisible}
      overlay={ProfileDropDown(profileDropDownProps)}
      trigger={['click']}
    >
      <a
        role="button"
        className="ant-dropdown-link"
        onClick={e => e.preventDefault()}
      >
        <span className="navbar_button">{nickname}</span>
      </a>
    </Dropdown>
  );
}
 
export default Profile;