Skip to content

Commit

Permalink
Merge pull request #90 from softeerbootcamp4th/TASK-42
Browse files Browse the repository at this point in the history
[Feature][Task-42] 소켓 연결, 기대평 디자인 수정, setCookie 허용
  • Loading branch information
racgoo authored Aug 20, 2024
2 parents b9c6893 + 4898c7a commit 0829157
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 10 deletions.
11 changes: 11 additions & 0 deletions packages/admin/public/casper/khaki.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions packages/admin/public/casper/orange.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/admin/public/casper/white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/admin/public/casper/yellow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions packages/admin/src/components/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { BlockedChat, ChatProps, Message, Notice } from '@softeer/common/compone
import { FunctionComponent, useCallback } from 'react';

export default function Chat({ type, team, sender, content }: ChatProps) {
const handleHide = () => {
console.log('hide!');
};
const Render: FunctionComponent = useCallback(() => {
switch (type) {
case 'n':
Expand All @@ -11,8 +14,15 @@ export default function Chat({ type, team, sender, content }: ChatProps) {
case 'm':
default:
return (
<Message sender={sender} team={team} isMyMessage={false}>
{content}
<Message
sender={sender}
team={team}
isMyMessage={false}
hideAction={() => {
handleHide();
}}
>
<div className="text-cream-100 flex mi-w-fit flex-1">{content}</div>
</Message>
);
}
Expand Down
21 changes: 20 additions & 1 deletion packages/admin/src/components/chat/RealTimeChatting.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import { ChatList } from '@softeer/common/components';
import { useRef } from 'react';
import { UseSocketReturnType } from 'src/hooks/socket/index.ts';
import { useAlert } from 'src/store/provider/AlertProvider.tsx';
import { Button } from '../ui/button.tsx';
import { Input } from '../ui/input.tsx';
import Chat from './Chat.tsx';

/** 실시간 기대평 섹션 */

function RealTimeChatting({ chatSocket: { messages } }: Pick<UseSocketReturnType, 'chatSocket'>) {
const { openAlert, addAlertCallback } = useAlert();
const noticeInputRef = useRef<HTMLInputElement>(null);
const handleSend: React.FormEventHandler<HTMLFormElement> = (event) => {
event.preventDefault();
addAlertCallback(() => {
// 삭제하는 로직 들어가야함
if (noticeInputRef.current) {
noticeInputRef.current.value = '';
}
});
openAlert(<p>정말로 전송하겠습니까?<br />전송 이후엔 수정이 불가능합니다.</p>, 'confirm');
};
return (
<section className="container flex max-w-[1200px] snap-start flex-col items-center pb-[115px] pt-[50px]">
<h6 className="text-heading-10 mb-[25px] font-medium">기대평을 남겨보세요!</h6>
<form className="flex w-full flex-row mb-4 gap-2" onSubmit={handleSend}>
<Input ref={noticeInputRef} className="flex flex-1" placeholder="공지사항을 입력하세요." />
<Button type="submit">공지사항 전송</Button>
</form>
<div className="h-[1000px] w-full overflow-y-auto rounded-[10px] bg-neutral-800 py-10">
<ChatList>
{messages.map((message) => (
Expand Down
5 changes: 4 additions & 1 deletion packages/admin/src/hooks/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ export type UseSocketReturnType = ReturnType<typeof useSocket>;
export default function useSocket() {
const accessToken = Cookie.getCookie<string>(ACCESS_TOKEN_KEY) ?? '';
const chatSocket = useChatSocket();
console.log(chatSocket)
const { onReceiveMessage, ...chatSocketProps } = chatSocket;
useEffect(() => {
socketManager.connectSocketClient({ token: accessToken, onReceiveMessage });
if(accessToken !== ""){
socketManager.connectSocketClient({ token: accessToken, onReceiveMessage });
}
}, [socketManager, accessToken]);

return { chatSocket: chatSocketProps };
Expand Down
1 change: 1 addition & 0 deletions packages/admin/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineConfig({
src: path.resolve(__dirname, './src'),
},
},
publicDir: './public',
define: {
global: 'window', // web socket
},
Expand Down
24 changes: 21 additions & 3 deletions packages/common/src/components/chat/Message.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { PropsWithChildren } from 'react';
import { FormEventHandler, PropsWithChildren } from 'react';
import { MessageChatProps } from 'src/components/chat/index.ts';
import { SocketCategory } from 'src/types/category.ts';
import { cn } from 'src/utils/index.ts';

interface MessageProps extends Pick<MessageChatProps, 'sender' | 'team'> {
isMyMessage?: boolean;
hideAction?: () => void;
}

const TYPES: Record<SocketCategory, { casper: string; textColor: string }> = {
Expand All @@ -19,17 +20,34 @@ export default function Message({
team,
isMyMessage = false,
children,
hideAction,
}: PropsWithChildren<MessageProps>) {
const { casper, textColor } = TYPES[team.toLowerCase() as SocketCategory];

const handlehide: FormEventHandler<HTMLFormElement> = (event) => {
event.preventDefault();
if (hideAction) {
hideAction();
}
};
return (
<div className="flex w-full items-center gap-12">
<div className="flex min-w-[180px] max-w-[180px] items-center gap-3">
<img src={casper} className="h-8 w-11" alt="캐스퍼" />
<p className={cn(textColor, 'text-body-3 truncate font-medium')}>익명 {sender} </p>
{isMyMessage && <p className={cn(textColor, 'text-body-3 font-medium')}>(나)</p>}
</div>
<p className={`text-body-3 truncate ${isMyMessage && 'font-medium'}`}>{children}</p>
<div className="flex flex-row justify-between flex-1">
<p className={`text-body-3 truncate ${isMyMessage && 'font-medium'}`}>{children}</p>
{

hideAction ?
<form onSubmit={handlehide}>
<button type="submit" className="text-cream-100">Hide</button>
</form>
:
<div />
}
</div>
</div>
);
}
1 change: 1 addition & 0 deletions packages/common/src/components/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ export { default as BlockedChat } from './BlockedChat.tsx';
export { default as ChatList } from './ChatList.tsx';
export { default as Message } from './Message.tsx';
export { default as Notice } from './Notice.tsx';

6 changes: 3 additions & 3 deletions packages/common/src/utils/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class FetchWrapper {

async get<T>(url: string): Promise<T> {
return this.request<T>(url, {
// credentials: 'include',
credentials: 'include',
});
}

Expand All @@ -60,7 +60,7 @@ export default class FetchWrapper {
headers: {
...generateDefaultHeaders(),
},
// credentials: 'include',
credentials: 'include',
});
}

Expand All @@ -71,7 +71,7 @@ export default class FetchWrapper {
headers: {
...generateDefaultHeaders(),
},
// credentials: 'include',
credentials: 'include',
});
}

Expand Down

0 comments on commit 0829157

Please sign in to comment.