-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
친구 추가 로직 구현 #116
친구 추가 로직 구현 #116
Changes from all commits
b992b8c
9f2d3be
bc0ef88
220cace
3dcc671
548659f
8509998
4275fa7
5335e09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,18 @@ import Signin from './page/Signin'; | |
import Signup from './page/Signup'; | ||
import { useEffect } from 'react'; | ||
import axios from 'axios'; | ||
import { useSetRecoilState } from 'recoil'; | ||
import { useRecoilState, useSetRecoilState } from 'recoil'; | ||
import { userState } from './store/atom/user'; | ||
import { friendsState } from './store/atom/friends'; | ||
|
||
type friendList = { | ||
nickname: string; | ||
characterName: string; | ||
}; | ||
|
||
const Router = () => { | ||
const setUser = useSetRecoilState(userState); | ||
const setFriends = useSetRecoilState(friendsState); | ||
|
||
useEffect(() => { | ||
const checkAuth = async () => { | ||
|
@@ -20,6 +27,24 @@ const Router = () => { | |
nickname: data.nickname.trim(), | ||
hair: data.characterName.trim(), | ||
}); | ||
|
||
(async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try catch 문 사용하면 더 좋을 것 같아요!!!!😀 |
||
const response = await axios.get('/api/friendship'); | ||
|
||
const friendList: friendList[] = response.data; | ||
if (!friendList.length) return; | ||
|
||
const initialList: any = {}; | ||
friendList.forEach(({ nickname }) => { | ||
initialList[nickname] = { | ||
isOnline: false, | ||
name: nickname, | ||
isCalling: false, | ||
}; | ||
}); | ||
|
||
setFriends(initialList); | ||
})(); | ||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,7 @@ export const userName = (state: boolean) => css` | |
`; | ||
|
||
export const findFriend = css` | ||
position: relative; | ||
display: flex; | ||
justify-content: space-evenly; | ||
align-items: center; | ||
|
@@ -83,6 +84,36 @@ export const findFriend = css` | |
background-color: rgba(255, 255, 255, 0.7); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 지금 rgba 많이 사용하는데 이거 같이 theme으로 정리할까요?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넴 좋은 것 같아요~~ |
||
margin-top: 10px; | ||
|
||
ul { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
|
||
display: flex; | ||
flex-flow: column nowrap; | ||
gap: 8px; | ||
|
||
width: 180px; | ||
transform: translate(50px, -100%); | ||
background-color: rgba(245, 245, 245, 0.9); | ||
border-radius: 10px; | ||
padding: 5px; | ||
margin-top: -5px; | ||
|
||
li { | ||
border-radius: 5px; | ||
font-weight: 700; | ||
padding: 5px; | ||
|
||
cursor: pointer; | ||
|
||
:hover { | ||
color: ${theme.green}; | ||
background-color: rgba(255, 255, 255, 0.9); | ||
} | ||
} | ||
} | ||
|
||
::before { | ||
content: url(${addFriendIcon}); | ||
} | ||
|
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; | ||
|
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 디바운스 구현을 이런 식으로 할 수 있네요.. 배워갑니당! |
||
}; | ||
|
||
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 타입 any로 많이 해줬는데 반성이 됩ㄴ디ㅏ....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저희가....anyscript를....사용했던가요.......? 🙃🙃🙃
라고 하지만 저도....