-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from ktmihs/develop
친구 추가 로직 구현
- Loading branch information
Showing
9 changed files
with
176 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export type friendType = { | ||
id: string; | ||
isCalling: boolean; | ||
isOnline: boolean; | ||
name: string; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import axios from 'axios'; | ||
import { FormEvent, MouseEvent, useState } from 'react'; | ||
import { findFriend } from './friends.styled'; | ||
|
||
const testSearchWord = (word: string) => | ||
['hj', 'ktmihs22', 'jongbin', 'kt', 'ktm', 'ktmi', 'ktmih', 'ktmihs'].filter( | ||
name => name.indexOf(word) !== -1 | ||
); | ||
|
||
const Search = () => { | ||
const [searchWord, setSearchWord] = useState<string>(''); | ||
const [nicknameList, setNicknameList] = useState<string[]>([]); | ||
|
||
const addToFriend = async (e: MouseEvent) => { | ||
const target = e.target as HTMLUListElement; | ||
|
||
const selectedWord = target.innerText; | ||
|
||
setSearchWord(''); | ||
setNicknameList([]); | ||
|
||
const response = confirm(`${selectedWord}를 친구추가 하시겠습니까?`); | ||
if (response) { | ||
try { | ||
await axios.put(`/api/friendship/${selectedWord}`); | ||
|
||
alert('팔로우 되었습니다.'); | ||
} catch { | ||
alert('팔로우 실패'); | ||
} | ||
} | ||
}; | ||
|
||
const [timer, setTimer] = useState<NodeJS.Timeout>(); | ||
|
||
const handleSearchNickname = (e: FormEvent) => { | ||
const target = e.target as HTMLInputElement; | ||
|
||
const value = target.value; | ||
setSearchWord(value); | ||
|
||
if (timer) clearTimeout(timer); | ||
const newTimer = setTimeout(() => { | ||
// 서버로 리스트 받아오는 요청 보내기 | ||
const findList = testSearchWord(value); | ||
setNicknameList(findList); | ||
}, 500); | ||
|
||
setTimer(newTimer); | ||
}; | ||
|
||
return ( | ||
<section css={findFriend}> | ||
{nicknameList.length ? ( | ||
<ul onClick={addToFriend}> | ||
{nicknameList.map(name => ( | ||
<li key={name}>{name}</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<></> | ||
)} | ||
<input | ||
type="text" | ||
value={searchWord} | ||
placeholder="추가할 친구 이름" | ||
onInput={handleSearchNickname} | ||
/> | ||
</section> | ||
); | ||
}; | ||
|
||
export default Search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters