forked from dephizee/the-cryptoflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
310 lines (275 loc) · 10 KB
/
index.html
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<html>
<head>
<meta charset="utf-8">
<title>CrytoFlow</title>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="./css/icon.min.css">
<link rel="stylesheet" type="text/css" href="./css/m_main.css">
<div class="col-offset-8 col-4 spinner">
<img src="./images/spinner.gif">
</div>
<script src="https://unpkg.com/@aeternity/aepp-sdk/dist/aepp-sdk.browser-script.js"></script>
<script type="text/javascript">
const contractSource = `
contract Task_flow =
record task_info =
{
data : map(int, task),
length : int
}
record task =
{
name : string,
comment : string,
date : string,
duration : int}
record state =
{
all_tasks: map(address, task_info)
}
entrypoint init() =
{
all_tasks = {}
}
stateful entrypoint init_user() =
if( !Map.member(Call.caller, state.all_tasks) )
let task_i = { data = {} , length = 0 }
put(state{all_tasks[Call.caller] = task_i })
stateful entrypoint add_task(name': string, comment': string, date': string, duration': int)=
let task = { name = name', comment = comment', date = date', duration = duration'}
let index = state.all_tasks[Call.caller].length
let new_i = index + 1
put(state{all_tasks[Call.caller].data[index] = task})
put(state{all_tasks[Call.caller].length = new_i })
stateful entrypoint getLength() : int =
Map.size(state.all_tasks)
stateful entrypoint getTask(addr : address) : map(int, task) =
if( Map.member(addr, state.all_tasks ) )
state.all_tasks[addr].data
else
{}
stateful entrypoint getAllTasksForAddress() : map(int, task) =
if( Map.member(Call.caller, state.all_tasks ) )
state.all_tasks[Call.caller].data
else
{}
stateful entrypoint getAllTask() : map(address, task_info ) =
state.all_tasks
stateful entrypoint deleteTask(index : int) =
put(state{ all_tasks[Call.caller].data @ b = Map.delete(index, b) })
`;
const contractAddress = 'ct_zqhchWZEYwjJhpZbYVfVbfBzXeDNVeuinuXG3QBvobe1wRyjC';
</script>
<body>
<div class="col-8 col-offset-4 panel">
<h2 style="text-align: center;">
Task
</h2>
<input type="text" name="task" placeholder="Task">
<input type="text" name="comment" placeholder="Comment">
<input type="date" name="start_date" placeholder="Start Date">
<input type="number" name="duration" placeholder="Duration">
<input type="submit" id="add_task" value="Add Task">
<input type="submit" id="remove_panel" value="Cancel">
</div>
<div class="row">
<div class="col-12 main_div">
<div class="col-12 header">
<div class="col-12 header_top">
<div class="col-6 logo">
crypto flow
</div>
<div class="col-6 menu_list">
<i class="icono-list menu_list_icon" ></i>
<h5 class="" style="display: none"></h5>
<ul class="col-12 menu_ul">
<!-- <a class="spinner" href="#"><li><i class="icono-spinner" style=" margin: 20px; color: #ff0028;;"></i></li></a> -->
</ul>
</div>
</div>
<div class="col-12">
<div class=" col-offset-4 col-8 header_main">
<h4 class="welcome_text">
Welcome to Cryptoflow
</h4>
<div class=" col-offset-8 col-4 g_started">
Get Started
</div>
</div>
</div>
</div>
<div class="col-12 mid_div" >
<div class=" col-offset-4 col-8 sub_header_main">
<h2 class="">
What CryptoFlow offers
</h2 >
</div>
<div class=" col-offset-4 col-8">
CrytoFlow is a platform that let users keep and a secure workflow of their daily activity and manage them efficently
</div>
</div>
<div class="col-12 mid_div_2 display_panel" style="display: none;">
<div class="col-offset- col-3 item" >
<div class="col-offset-6 col-6 icon_container">
<i class="icono-plus" style=" margin: 20px; color: #ff0028;;"></i>
</div>
<div class="col-offset-6 col-6 show_panel" style="color: #fff;" >
Add Task
</div>
</div>
</div>
<div class="col-12 footer">
</div>
</div>
</div>
</body>
</html>
<script type="text/javascript">
// assigning needeed variables and objects
var tmp_div = `<div class="col-offset- col-3 item task" >
<div class="col-12 icon_container t_name">
task name (start_date)
</div>
<div class="col-12 t_comment" >
Comment
</div>
<div class="col-offset-6 col-6 g_started t_delete" style="color: #fff;" >
Delete Task
</div>
</div>`;
var panel = document.querySelector("div.panel");
var display_panel = document.querySelector("div.display_panel");
var show_panel = document.querySelector("div.show_panel");
var spinner = document.querySelector(".spinner");
var remove_panel = document.querySelector("#remove_panel");
var add_task = document.querySelector("#add_task");
// assigning event listeners appropraitely
add_task.onclick = async (e)=>{
if(validate()){
spinner.style.marginTop = "50%";
panel.style.marginTop = "-500px";
var callset = await callNonStaticFunc('add_task', getNewTaskData(), 0);
var tasks = await callFunc('getAllTasksForAddress', []);
if (tasks==null) {
alert("Unable to load");
}else{
populateTask(tasks);
}
}
spinner.style.marginTop = "-500px";
}
show_panel.addEventListener('click', function() {
panel.style.marginTop = "30px";
})
remove_panel.addEventListener('click', function() {
panel.style.marginTop = "-500px";
})
var validate = ()=>{
// little error checking and validation
var v_node = document.querySelectorAll(".panel input[name]");
for (var i = 0; i < v_node.length; i++) {
if(v_node[i].value.length < 1){
v_node[i].focus();
alert("Empty field");
return false;
break;
}
}
return true;
}
function getNewTaskData(){
//Fetch all data from form
var arr = [];
var v_node = document.querySelectorAll(".panel input[name]");
for (var i = 0; i < v_node.length; i++) {
arr[i] = v_node[i].value;
v_node[i].value = "";
}
console.log(arr.toString() ) ;
return arr;
}
var renderTask = (data) =>{
// reassign variable for rendering
var data_id = data[0];
data = data[1];
// then render to browser
var t_div = new DOMParser().parseFromString(tmp_div, 'text/html');
t_div = t_div.querySelector(".item");
t_div.querySelector(".t_name").innerHTML = data.name +" ("+ data.date +")<br>"+data.duration+" day(s) ";
t_div.querySelector(".t_comment").innerHTML = data.comment;
t_div.querySelector(".t_delete").id = data_id;
t_div.querySelector(".t_delete").onclick = async (e)=>{
spinner.style.marginTop = "50%";
var callset = await callNonStaticFunc('deleteTask', [e.target.id], 0);
var tasks = await callFunc('getAllTasksForAddress', []);
if (tasks==null) {
alert("Unable to load");
}else{
populateTask(tasks);
}
spinner.style.marginTop = "-300px";
}
display_panel.appendChild(t_div);
}
var populateTask = (data)=>{
unpopulateTask();
// populate the browser with data
for (var i = 0; i < data.length; i++) {
renderTask(data[i]);
}
}
var unpopulateTask = ()=>{
// removes all task currently being rendered
var data = document.querySelectorAll(".task");
for (var i = 0; i < data.length; i++) {
display_panel.removeChild(data[i]);
}
}
//
//
//
//
//
var client = null;
async function callFunc(func, args){
// got this from the tutorial static calls
const contract = await client.getContractInstance(contractSource, {contractAddress});
const calledGet = await contract.call(func, args, {callStatic: true}).catch(e => console.error(e));
const decodedGet = await calledGet.decode().catch(e => console.error(e));
return decodedGet;
}
async function callNonStaticFunc(func, args, value){
// got this from the tutorial contract calls
const contract = await client.getContractInstance(contractSource, {contractAddress});
const calledSet = await contract.call(func, args, {amount: value}).catch(e => {
console.error(e);
});
return calledSet;
}
var init = async ()=>{
//intialize the client Obj
client = await Ae.Aepp();
// the init_user function was created after i realized, nested intialization wasn't allowed
// so the init function checks and initializes new addresses
var init_user = await callNonStaticFunc('init_user', [], 0);
// getAllTasksForAddress func returns all functions under the a user
var tasks = await callFunc('getAllTasksForAddress', []);
//little error checking
if (tasks==null) {
display_panel.style.display = "none";
alert("Unable to load, Reload please");
}else{
display_panel.style.display = "block";
if(tasks.length > 0){
populateTask(tasks);
}else{
alert("No Task Found");
}
}
spinner.style.marginTop = "-300px";
}
//starts the whole Ae App() js excution
init();
</script>