-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (134 loc) · 4.51 KB
/
Copy pathapp.js
File metadata and controls
139 lines (134 loc) · 4.51 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
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
App = {
contracts: {},
init: async () => {
await App.loadWeb3();
await App.loadAccount();
await App.loadContract();
await App.render();
await App.renderTasks();
},
loadWeb3: async () => {
if (window.ethereum) {
App.web3Provider = window.ethereum;
await window.ethereum.request({ method: "eth_requestAccounts" });
} else if (web3) {
web3 = new Web3(window.web3.currentProvider);
} else {
console.log(
"No ethereum browser is installed. Try it installing MetaMask "
);
}
},
loadAccount: async () => {
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
App.account = accounts[0];
},
loadContract: async () => {
try {
const res = await fetch("TasksContract.json");
const tasksContractJSON = await res.json();
App.contracts.TasksContract = TruffleContract(tasksContractJSON);
App.contracts.TasksContract.setProvider(App.web3Provider);
App.tasksContract = await App.contracts.TasksContract.deployed();
} catch (error) {
console.error(error);
}
},
render: async () => {
document.getElementById("account").innerText = App.account;
},
renderTasks: async () => {
const tasksCounter = await App.tasksContract.tasksCounter();
const taskCounterNumber = tasksCounter.toNumber();
let html = "";
for (let i = 1; i <= taskCounterNumber; i++) {
const task = await App.tasksContract.tasks(i);
const taskId = task[0].toNumber();
const taskTitle = task[1];
const taskDescription = task[2];
const taskDone = task[3];
const taskCreatedAt = task[4];
const taskPrice = task[5];
// Creating a task Card
let taskElement = `<div class="wrapper">
<svg class="header" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none">
<polygon fill="white" points="0,0 100,100 0,100"/>
</svg>
<div class="card-container">
<div class="card-body">
<div class="side side-back">
<div class="container-fluid">
<div class="row">
<div class="product-image col-5">
<img src="panda.png" alt="Car" />
</div>
</div>
</div>
</div>
<div class="side side-front">
<div class="container-fluid">
<div class="row">
<div class="product-image col-12 col-lg-5">
<img src="./images/panda.png" alt="Car" />
</div>
<div class="content col-12 col-lg-7">
<h2>Rent ${taskTitle}</h2>
<div class="price-rating">
<div class="price">
<h3>Price/day:</h3>
<p>${taskPrice} ETH</p>
</div>
<div class="rating">
<h3>Id: <span class="rating-text">${taskId}</span></h3>
<div id="rateYo"></div>
</div>
</div>
<div class="details">
<p>
${taskDescription}
</p><br>
<p class="text-muted">
Car was created ${new Date(taskCreatedAt * 1000).toLocaleString()}
</p>
<span class="text-muted">Rented ${taskDone}</span>
</div>
<div class="form-check form-switch">
<input class="form-check-input"
data-id="${taskId}"
type="checkbox"
onchange="App.toggleDone(this)"
${taskDone === true && "checked"}
>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>`;
html += taskElement;
}
document.querySelector("#tasksList").innerHTML = html;
},
createTask: async (title, description, price) => {
try {
const result = await App.tasksContract.createTask(title, description, price, {
from: App.account,
});
console.log(result.logs[0].args);
window.location.reload();
} catch (error) {
console.error(error);
}
},
toggleDone: async (element) => {
const taskId = element.dataset.id;
await App.tasksContract.toggleDone(taskId, {
from: App.account, value: "999999999999999999"
});
window.location.reload();
},
};