Skip to content

Commit ea489d1

Browse files
authored
Merge pull request #42 from NullFull/feat/add-debounce
`ask()`함수에 `debounce()` 함수 추가
2 parents 69f697c + e4f64ed commit ea489d1

File tree

6 files changed

+93
-22
lines changed

6 files changed

+93
-22
lines changed

pages/api/requests.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import {makeResponse} from '@/utils/api'
55
export default async (req, res) => {
66
const { to, content } = req.body
77

8-
makeResponse(res, {
9-
message: '지금은 질문할 수 없습니다'
10-
})
11-
12-
// await Promise.all(
13-
// to.map(item => requestStore.create(item, content))
14-
// )
15-
//
168
// makeResponse(res, {
17-
//
9+
// message: '지금은 질문할 수 없습니다'
1810
// })
11+
12+
await Promise.all(
13+
to.map(item => requestStore.create(item, content))
14+
)
15+
16+
makeResponse(res, {
17+
18+
})
1919
}

src/components/Ask/Modal.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.modal {
2+
display: flex; /* Hidden by default */
3+
position: fixed; /* Stay in place */
4+
z-index: 999; /* Sit on top */
5+
justify-content: center;
6+
align-items: center;
7+
top: 0;
8+
left: 0;
9+
right: 0;
10+
bottom: 0;
11+
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
12+
}
13+
14+
/* Modal Content/Box */
15+
.modalContent {
16+
background-color: #fefefe;
17+
margin: 15% auto; /* 15% from the top and centered */
18+
padding: 20px;
19+
border: 1px solid #888;
20+
width: 80%; /* Could be more or less, depending on screen size */
21+
max-width: 400px;
22+
text-align: center;
23+
align-items: center;
24+
justify-content: center;
25+
}

src/components/Ask/Modal.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import './Modal.css'
2+
3+
const Modal = () => {
4+
return (
5+
<div className="modal">
6+
<div className="modalContent">
7+
질문을 보내는 중입니다.<br/>
8+
창을 닫지 말아주세요.
9+
</div>
10+
</div>
11+
)
12+
}
13+
14+
export default Modal

src/components/Ask/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
font-size: 1.1rem;
2929
padding: 12px 16px 12px 16px;
3030
color: #fff;
31-
background: #463e39;
31+
background: #BA44AE;
3232
}
3333
.askButton:disabled {
3434
opacity: 0.5;

src/components/Ask/index.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import FindByRegion from '@/components/Ask/FindByRegion'
88
import FindByParty from '@/components/Ask/FindByParty'
99
import Candidates from '@/components/Ask/Candidates'
1010
import CandidatesContext from '@/components/Ask/CandidatesContext'
11+
import Modal from '@/components/Ask/Modal'
1112
import client from '@/utils/client'
13+
import debounce from '@/utils/debounce'
1214
import './index.css'
1315

1416

@@ -101,19 +103,30 @@ const useCandidates = () => {
101103

102104
const Ask = () => {
103105
const { candidates, status, fetchCandidates, actions } = useCandidates()
106+
const hasSelectedCandidates = candidates.filter(c => c.checked).length > 0
104107

105-
const ask = async () => {
106-
if (candidates.filter(c => c.checked).length < 1) {
107-
alert('먼저 문의를 보낼 후보를 선택해주세요')
108-
return
109-
}
108+
const [pending, setPending] = React.useState(false)
110109

111-
const content = '후보님의 생각이 궁금합니다.'
112-
await client().sendRequest(content, candidates.filter(c => c.checked).map(c => c.id))
110+
const content = '후보님의 생각이 궁금합니다.'
113111

114-
alert('질문이 등록 되었습니다.\n연락처가 존재하는 후보에게는 질문이 메일로 전달됩니다.')
115-
}
112+
React.useEffect(() => {
113+
const ask = async () => {
114+
try {
115+
await client().sendRequest(content, candidates.filter(c => c.checked).map(c => c.id))
116+
alert('질문이 등록 되었습니다.\n연락처가 존재하는 후보에게는 질문이 메일로 전달됩니다.')
117+
setPending(false)
118+
}
119+
catch(e)
120+
{
121+
alert('오류가 발생했습니다.')
122+
}
123+
}
116124

125+
if(pending) {
126+
debounce(ask())
127+
}
128+
}, [pending, candidates])
129+
117130
return (
118131
<div className="ask">
119132
{/* <ul className="notice">
@@ -191,9 +204,14 @@ const Ask = () => {
191204
</CandidatesContext.Provider>
192205
</div>
193206

194-
{/* <div>
195-
<button className="askButton" disabled={true}>지금은 질문을 보낼 수 없습니다.</button>
196-
</div> */}
207+
{hasSelectedCandidates && (
208+
<div>
209+
<button className="askButton" onClick={() => setPending(true)} >
210+
질문 보내기
211+
</button>
212+
</div>
213+
)}
214+
{ pending && <Modal /> }
197215
</div>
198216
)
199217
}

src/utils/debounce.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function debounce(func, timeout = 300) {
2+
let timer;
3+
return (...args) => {
4+
if (!timer) {
5+
func.apply(this, args);
6+
}
7+
clearTimeout(timer);
8+
timer = setTimeout(() => {
9+
timer = undefined;
10+
}, timeout);
11+
};
12+
}
13+
14+
export default debounce

0 commit comments

Comments
 (0)