-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhome.js
187 lines (135 loc) · 4.96 KB
/
home.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//var myContractAddress = '0x2Debf19564C7ca2c23a179A2b43DC9615cE23ff1'
//var myContractAddress = '0xdEE5bCBAa461cF6F432F54389d45BBd79e07160C';
const updateInterval = 1000; // 1 second
let eventId;
// **** Internal functions ****
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
// **** Access values on blockchain ****
async function updateEvents() {
/* TODO: list the events */
/* console.log('calling numEvents') */;
var num_events = await (proxyContract.methods.numEvents().call());
// console.log('num_events ', num_events);
eventId = num_events;
console.log("EVENT ID ", eventId);
var unclaimed = [];
var claimed = [];
var oneHour = 60 * 60;
console.log('numevents = %s', num_events);
//console.log('numseen = %s', numSeen);
for (var i = 1; i <= num_events; i++) {
var event = await (proxyContract.methods.getEvent(i).call());
console.log("event ", event);
// Check if event exists
let data = $("#calendar").fullCalendar('clientEvents', function(event){
console.log("Event id ", event.id)
if(event.id === i){
return true;
}else{
return false;
}
});
if(data.length == 0 && event.enabled) {
console.log('enabled');
var newEvent = new Object();
let w = parseInt(event.when) * 1000;
newEvent.id = i;
newEvent.title = 'Office hours';
newEvent.start = moment(w).format();
newEvent.end = moment(w + oneHour * 1000).format();
newEvent.allday = false;
if (!event.open) {
newEvent.backgroundColor = '#E33C18';
}
console.log("open: ", event.open);
$("#calendar").fullCalendar('renderEvent', newEvent, true);
} else if (event.enabled && !event.open && data.length > 0) {
console.log('data', data);
data[0].backgroundColor = '#E33C18';
$("#calendar").fullCalendar('updateEvent', data[0]);
}
// console.log('dataaaaa: ', data);
// console.log('event = %s', event);
// console.log("EVENT ID INCREASE ", eventId)
// /* if (event.enabled && event.open)
// claimed.push(i);
// else
// unclaimed.push(i);*/
}
$("#unclaimedEvents").text(unclaimed);
$("#claimedEvents").text(claimed);
}
// **** Global functions ****
async function addEvent2(startTime, endTime) {
// Wait for
var prev_num_events = await (proxyContract.methods.numEvents().call());
confirm("this is the timestamp being given: " + startTime.unix());
proxyContract.methods.addEvent( startTime.unix() ).send({from: user});
//console.log("Transaction sent");
while (true) {
var num_events = await (proxyContract.methods.numEvents().call());
if (prev_num_events != num_events) {
break
}
await sleep(1000);
}
numSeen += 1;
//console.log("Transaction completed!");
}
async function initCalender() {
$('#calendar').fullCalendar({
selectable: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
/* dayClick: function(date) {
alert('clicked ' + date.format());
}, */
select: async function(startDate, endDate) {
eventId += 1;
//alert('selected ' + startDate.format() + ' to ' + endDate.format());
var ok = confirm("Confirm new event from " + startDate.format("dddd, MMMM Do YYYY, h:mm:ss a") + ' to ' + endDate.format("dddd, MMMM Do YYYY, h:mm:ss a"));
var newEvent = new Object();
newEvent.id = eventId;
newEvent.title = 'Office hours';
newEvent.start = moment(startDate).format();
newEvent.end = moment(endDate).format();
newEvent.allday = false;
await addEvent2(startDate, endDate);
$("#calendar").fullCalendar('renderEvent', newEvent);
},
defaultView: 'month',
editable: true,
});
}
// **** Initialization ****
async function init() {
const myContractAddress = prompt("Contract adress");
var currurl = window.location;
window.web3 = new Web3(ethereum)
window.user = (await ethereum.request({ method: 'eth_requestAccounts'}))[0]
$('#user').text(user)
await initCalender();
const proxyABI = JSON.parse($('#proxyABI').text());
const mainABI = JSON.parse($('#mainABI').text());
window.proxyContract = new web3.eth.Contract(proxyABI, myContractAddress);
window.mainContract = new web3.eth.Contract(mainABI, myContractAddress);
window.numSeen = 0;
var uxurl = await (proxyContract.methods.owner_url().call());
eventId = 0;
//if (currurl != uxurl) {
// //alert("cururl: " + currurl + " uxurl: " + uxurl);
// window.location.replace(uxurl);
//}
//console.log('Starting update Events');
await updateEvents();
while (true) {
await updateEvents();
await sleep(updateInterval);
}
}
init()