auction.pug
3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
extends layout
block content
.timeline
h2= '상품명: ' + good.name
div= '등록자: ' + good.owner.nick
div= '시작가: ' + good.price + '원'
span#time(data-start=good.createdAt)
img#good-img(src='/img/' + good.img)
#bid
for bid in auction
div
span= bid.user.nick + '님이 '
strong= bid.bid
span= '원에 입찰하셨습니다.'
if bid.msg
span= '(' + bid.msg + ')'
form#bid-form
input(type='number' name='bid' placeholder='입찰가' required min=good.price style='font-family:Yeon Sung; font-size:16px; width:100px; text-align:center; margin:5px;')
input(type='msg' name='msg' placeholder='메시지(선택사항)' maxlength='100' style='font-family:Yeon Sung; font-size:16px; width:300px; text-align:center; margin:5px;')
button.btn(type='submit' style='font-family:Yeon Sung; font-size:18px; width:100px; text-align:center; margin:5px;') 입찰
if auctionError
.error-message= auctionError
script(src='https://cdnjs.cloudflare.com/ajax/libs/event-source-polyfill/0.0.9/eventsource.min.js')
script(src='/socket.io/socket.io.js')
script.
document.querySelector('#bid-form').addEventListener('submit', function (e) {
e.preventDefault();
var xhr = new XMLHttpRequest();
var errorMessage = document.querySelector('.error-message');
xhr.onload = function () {
if (xhr.status === 200) {
e.target.bid.value = '';
e.target.msg.value = '';
errorMessage.textContent = '';
} else {
console.error(xhr.responseText);
e.target.bid.value = '';
e.target.msg.value = '';
errorMessage.textContent = xhr.responseText
}
};
xhr.open('POST', '/good/#{good.id}/bid');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
bid: e.target.bid.value,
msg: e.target.msg.value,
}));
});
var es = new EventSource("/sse");
var time = document.querySelector('#time');
es.onmessage = function (e) {
var end = new Date(time.dataset.start);
var server = new Date(parseInt(e.data, 10));
end.setDate(end.getDate() + 1);
if (server >= end) {
return time.textContent = '00:00:00';
} else {
var t = end - server;
var seconds = ('0' + Math.floor((t / 1000) % 60)).slice(-2);
var minutes = ('0' + Math.floor((t / 1000 / 60) % 60)).slice(-2);
var hours = ('0' + Math.floor((t / (1000 * 60 * 60)) % 24)).slice(-2);
return time.textContent = hours + ':' + minutes + ':' + seconds;
}
};
var socket = io.connect('http://localhost:8010', {
path: '/socket.io'
});
socket.on('bid', function (data) {
var div = document.createElement('div');
var span = document.createElement('span');
span.textContent = data.nick + '님이 ';
var strong = document.createElement('strong');
strong.textContent = data.bid
span.textContent = '원에 입찰하셨습니다.';
div.appendChild(span);
div.appendChild(strong);
if (data.msg) {
span = document.createElement('span');
span.textContent = '(' + data.msg + ')';
div.appendChild(span);
}
document.querySelector('#bid').appendChild(div);
});