Skip to content

Commit 95b0d8b

Browse files
authored
Feat/#242-S : 투표 블록 소켓 이벤트 연동 및 투표 템플릿화 리팩토링 (#255)
* refactor : debounce를 lib로 분리 후 custom hook에서 활용 * refactor : 투표 블록 리팩토링 - 투표 등록 전 후 view를 다르게 보여주지 않고 input을 항상 열어둠 - input onChange 디바운싱 적용 - isUnRegisterd -> isStartedVote 변수명 변경 * chore : 주석 제거 * refactor : 버튼 아이콘만 있을 때 재사용 가능하도록 수정 * feat : 투표 블록 템플릿으로 구현 mode에 따라 투표 UI가 바뀌도록 함 * feat : mom에 투표 블록 임시로 연결 * chore: 버튼 padding 줄이기 * refactor : 투표 create, update, end 소켓 로직 수정 * refactor : 코드리뷰 반영
1 parent 4a4e908 commit 95b0d8b

File tree

12 files changed

+342
-159
lines changed

12 files changed

+342
-159
lines changed

client/src/components/Mom/index.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import VoteBlockTemplate from 'common/Templates/VoteBlock';
12
import { useEffect, useRef, useState } from 'react';
3+
import { VOTE_MODE } from 'src/constants/block';
24
import SOCKET_MESSAGE from 'src/constants/socket-message';
35
import { useCRDT } from 'src/hooks/useCRDT';
46
import useDebounce from 'src/hooks/useDebounce';
57
import useSelectedMom from 'src/hooks/useSelectedMom';
68
import useSocketContext from 'src/hooks/useSocketContext';
9+
import { Option, VoteMode } from 'src/types/block';
710
import { v4 as uuid } from 'uuid';
811

912
import Block from './Block';
@@ -15,6 +18,8 @@ function Mom() {
1518
const { selectedMom } = useSelectedMom();
1619
const { momSocket: socket } = useSocketContext();
1720

21+
const [voteMode, setVoteMode] = useState<VoteMode | null>(null);
22+
1823
const {
1924
syncCRDT,
2025
spreadCRDT,
@@ -68,6 +73,9 @@ function Mom() {
6873
}
6974
};
7075

76+
const initialOption: Option[] = [{ id: 1, text: '', count: 0 }];
77+
const [options, setOptions] = useState<Option[]>(initialOption);
78+
7179
useEffect(() => {
7280
if (!selectedMom) return;
7381

@@ -112,6 +120,11 @@ function Mom() {
112120
ee.emit(`${SOCKET_MESSAGE.BLOCK.UPDATE_TEXT}-${id}`, crdt);
113121
});
114122

123+
socket.on(SOCKET_MESSAGE.MOM.CREATE_VOTE, (options) => {
124+
setVoteMode(VOTE_MODE.REGISTERED as VoteMode);
125+
setOptions(options);
126+
});
127+
115128
return () => {
116129
[
117130
SOCKET_MESSAGE.MOM.INIT,
@@ -122,6 +135,7 @@ function Mom() {
122135
SOCKET_MESSAGE.BLOCK.INIT,
123136
SOCKET_MESSAGE.BLOCK.INSERT_TEXT,
124137
SOCKET_MESSAGE.BLOCK.DELETE_TEXT,
138+
SOCKET_MESSAGE.MOM.CREATE_VOTE,
125139
].forEach((event) => socket.off(event));
126140
};
127141
}, [selectedMom]);
@@ -140,11 +154,22 @@ function Mom() {
140154
</h1>
141155
<span>{new Date(selectedMom.createdAt).toLocaleString()}</span>
142156
</div>
157+
143158
<div className={style['mom-body']}>
144159
{blocks.map((id, index) => (
145160
<Block key={id} id={id} index={index} onKeyDown={onKeyDown} />
146161
))}
147162
</div>
163+
{/* TODO: 임시로 놓은 투표 블록임 */}
164+
<button onClick={() => setVoteMode('create')}>투표 등록</button>
165+
{voteMode && (
166+
<VoteBlockTemplate
167+
mode={voteMode}
168+
setVoteMode={setVoteMode}
169+
options={options}
170+
setOptions={setOptions}
171+
/>
172+
)}
148173
</div>
149174
</div>
150175
) : (

client/src/components/VoteBlock/index.tsx

Lines changed: 0 additions & 107 deletions
This file was deleted.

client/src/components/common/Button/index.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import classNames from 'classnames/bind';
2+
import { memo } from 'react';
23

34
import style from './style.module.scss';
45

56
const cx = classNames.bind(style);
67

78
interface ButtonProps {
8-
text: string;
9+
text?: string;
910
href?: string;
1011
icon?: JSX.Element;
1112
className?: string;
1213
isDisabled?: boolean;
14+
ariaLabel?: string;
1315
color?: string;
1416
onClick?: () => void;
1517
}
@@ -20,6 +22,7 @@ function Button({
2022
icon,
2123
className,
2224
isDisabled = false,
25+
ariaLabel,
2326
color,
2427
onClick,
2528
}: ButtonProps) {
@@ -34,15 +37,19 @@ function Button({
3437
</a>
3538
) : (
3639
<button
37-
className={cx(style.button, className, { disable: isDisabled })}
40+
className={cx(style.button, className, {
41+
disable: isDisabled,
42+
foursquare: !text,
43+
})}
3844
disabled={isDisabled}
3945
onClick={onClickBtn}
4046
style={{ backgroundColor: color }}
47+
aria-label={ariaLabel}
4148
>
4249
{icon}
43-
<span>{text}</span>
50+
{text && <span>{text}</span>}
4451
</button>
4552
);
4653
}
4754

48-
export default Button;
55+
export default memo(Button);
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
@import 'styles/color.module.scss';
1+
@import 'styles/color.module';
22

33
.button {
4-
width: fit-content;
54
display: flex;
65
align-items: center;
7-
background-color: $primary-100;
6+
width: fit-content;
7+
padding: 10px 40px;
88
border: none;
99
border-radius: 40px;
10-
padding: 10px 40px;
10+
background-color: $primary-100;
1111
text-decoration: none;
1212

1313
span {
14-
color: $white;
1514
padding: 10px;
15+
color: $white;
1616
font-weight: 700;
1717
}
1818
}
1919

2020
.disable {
2121
background-color: $gray-300;
2222
}
23+
24+
.foursquare {
25+
padding: 5px;
26+
border-radius: 10px;
27+
}

0 commit comments

Comments
 (0)