Skip to content

Commit

Permalink
[feat] boostcampwm-2022#66 친구 검색 자동 완성
Browse files Browse the repository at this point in the history
- 자동 완성 api 연결
  • Loading branch information
ktmihs committed Dec 4, 2022
1 parent c45d134 commit 1a5677d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
5 changes: 2 additions & 3 deletions frontend/src/component/Sidebar/Friends/friends.styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ export const findFriend = css`
font-weight: 700;
padding: 5px;
cursor: pointer;
:hover {
&:not(.none):hover {
cursor: pointer;
color: ${theme.green};
background-color: rgba(255, 255, 255, 0.9);
}
Expand Down
50 changes: 37 additions & 13 deletions frontend/src/component/Sidebar/Friends/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ import axios from 'axios';
import { FormEvent, MouseEvent, useState } from 'react';
import { useRecoilState } from 'recoil';
import { friendsState } from '../../../store/atom/friends';
import { friendType } from './friends';
import { findFriend } from './friends.styled';

const testSearchWord = (word: string) =>
['hj', 'ktmihs22', 'jongbin', 'kt', 'ktm', 'ktmi', 'ktmih', 'ktmihs'].filter(
name => name.indexOf(word) !== -1
);
type nicknameType = {
userId: string;
isCalling: boolean;
isOnline: boolean;
nickname: string;
};

const Search = () => {
const [friends, setFriends] = useRecoilState(friendsState);
const [searchWord, setSearchWord] = useState<string>('');
const [nicknameList, setNicknameList] = useState<string[]>([]);
const [nicknameList, setNicknameList] = useState<nicknameType[]>([]);

const addToFriend = async (e: MouseEvent) => {
const target = e.target as HTMLUListElement;

if (target.classList.contains('none')) return;
const selectedWord = target.innerText;

setSearchWord('');
Expand Down Expand Up @@ -52,12 +56,28 @@ const Search = () => {
const value = target.value;
setSearchWord(value);

if (!value) {
setNicknameList([]);
return;
}
if (timer) clearTimeout(timer);
const newTimer = setTimeout(() => {
// 서버로 리스트 받아오는 요청 보내기
const findList = testSearchWord(value);
setNicknameList(findList);
}, 500);

const newTimer = setTimeout(async () => {
const { data: findList } = await axios(`/api/friendship/${value}`);

if (findList.length) {
setNicknameList(findList);
} else if (value) {
setNicknameList([
{
userId: 'none',
nickname: '일치하는 유저가 없습니다.',
isCalling: false,
isOnline: false,
},
]);
}
}, 100);

setTimer(newTimer);
};
Expand All @@ -66,9 +86,13 @@ const Search = () => {
<section css={findFriend}>
{nicknameList.length ? (
<ul onClick={addToFriend}>
{nicknameList.map(name => (
<li key={name}>{name}</li>
))}
{nicknameList.map(
({ nickname, userId }: { nickname: string; userId: string }) => (
<li key={nickname} className={userId === 'none' ? 'none' : ''}>
{nickname}
</li>
)
)}
</ul>
) : (
<></>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/component/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const component: componentType = {
};

const Sidebar = () => {
const [isOpen, setOpen] = useState(false);
const [isOpen, setOpen] = useState(true);
const [isMicOn, setMic] = useState(false);
const [isCamOn, setCam] = useState(true);

Expand Down

0 comments on commit 1a5677d

Please sign in to comment.