Repository navigation
Expand file tree
/
Copy pathtestCreate.js
More file actions
636 lines (536 loc) · 18.2 KB
/
Copy pathtestCreate.js
File metadata and controls
636 lines (536 loc) · 18.2 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// create.js
import { createBounty, getCategories, getTags } from "./api.js";
import { getConnectedWallet, shortenAddress } from "./wallet.js";
import { createBountyOnChain } from "./contractService.js";
import { initProfile } from "./initProfile.js";
// ==================== STATE MANAGEMENT ====================
let currentStep = 1;
let bountyData = {
title: "",
description: "",
category: "",
tags: [],
startDate: "",
deadline: "",
originLink: "",
network: "",
reward: 0,
token: "USDC",
winnersAllowed: 1,
payoutType: "single",
percentages: [],
creator: "",
};
// Multi-winner state
let isMultipleWinner = false;
let selectedPayoutType = "equal"; // "equal" or "percentage"
let winnerCount = 2;
let percentageArray = [];
// ==================== DOM ELEMENTS ====================
const pages = {
1: document.getElementById("page1"),
2: document.getElementById("page2"),
3: document.getElementById("page3"),
4: document.getElementById("page4"),
};
const stepIndicators = {
1: document.getElementById("step1"),
2: document.getElementById("step2"),
3: document.getElementById("step3"),
4: document.getElementById("step4"),
};
const nextBtn = document.getElementById("nextBtn");
const backBtn = document.getElementById("backBtn");
// ==================== INITIALIZATION ====================
document.addEventListener("DOMContentLoaded", async () => {
console.log("Create page loaded");
// Check if wallet is connected
const wallet = getConnectedWallet();
if (!wallet) {
showToast("Please connect your wallet first", "warning");
setTimeout(() => {
window.location.href = "index.html";
}, 2000);
return;
}
bountyData.creator = wallet;
// Initialize profile circle
await initProfile();
// Set minimum dates for date inputs
setMinDates();
// Initialize UI
updateStepIndicators();
setupEventListeners();
setupTokenDisplay();
});
// ==================== SETUP FUNCTIONS ====================
function setMinDates() {
const today = new Date().toISOString().split("T")[0];
const startDateInput = document.getElementById("startDate");
const endDateInput = document.getElementById("endDate");
if (startDateInput) {
startDateInput.min = today;
startDateInput.value = today;
}
if (endDateInput) {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 7);
endDateInput.min = tomorrow.toISOString().split("T")[0];
endDateInput.value = tomorrow.toISOString().split("T")[0];
}
}
function setupEventListeners() {
// Navigation buttons
nextBtn.addEventListener("click", handleNext);
backBtn.addEventListener("click", handleBack);
// Form inputs
document.getElementById("networks")?.addEventListener("change", (e) => {
bountyData.network = e.target.value;
});
document.getElementById("categories")?.addEventListener("change", (e) => {
bountyData.category = e.target.value;
});
document.getElementById("bountyTitle")?.addEventListener("input", (e) => {
bountyData.title = e.target.value;
updateReviewField("title", e.target.value);
});
document
.getElementById("bountyDescription")
?.addEventListener("input", (e) => {
bountyData.description = e.target.value;
updateReviewField("description", e.target.value);
});
document.getElementById("tags")?.addEventListener("change", (e) => {
bountyData.tags = [e.target.value];
updateReviewField("tags", e.target.value);
});
document.getElementById("startDate")?.addEventListener("change", (e) => {
bountyData.startDate = e.target.value;
});
document.getElementById("endDate")?.addEventListener("change", (e) => {
bountyData.deadline = e.target.value;
updateReviewField("deadline", formatDate(e.target.value));
});
document.getElementById("submissionLink")?.addEventListener("input", (e) => {
bountyData.originLink = e.target.value;
updateReviewField("task-detail-link", e.target.value);
});
// Token selection
document.getElementById("network1")?.addEventListener("change", (e) => {
bountyData.token = e.target.value;
});
// Reward input
document
.querySelector('#page3 input[type="number"]')
?.addEventListener("input", (e) => {
bountyData.reward = parseFloat(e.target.value) || 0;
calculateFees();
});
// Multiple winners toggle
setupMultiWinnerControls();
}
function setupMultiWinnerControls() {
const toggle = document.getElementById("toggle");
const dropdown = document.getElementById("dropdown");
const equalBtn = document.getElementById("equalBtn");
const percentBtn = document.getElementById("percentBtn");
const equalModal = document.getElementById("equalModal");
const percentModal = document.getElementById("percentModal");
const infom = document.getElementById("infom");
const infomenu = document.getElementById("infomenu");
// Toggle dropdown visibility
toggle.addEventListener("change", (e) => {
isMultipleWinner = e.target.checked;
dropdown.classList.toggle("hidden", !isMultipleWinner);
if (isMultipleWinner) {
bountyData.winnersAllowed = 2;
bountyData.payoutType = selectedPayoutType;
} else {
bountyData.winnersAllowed = 1;
bountyData.payoutType = "single";
bountyData.percentages = [];
}
});
// Equal split button
equalBtn.addEventListener("click", () => {
selectedPayoutType = "equal";
equalBtn.classList.add("bg-pink-500");
percentBtn.classList.remove("bg-pink-500");
equalModal.classList.remove("hidden");
});
// Percentage split button
percentBtn.addEventListener("click", () => {
selectedPayoutType = "percentage";
percentBtn.classList.add("bg-pink-500");
equalBtn.classList.remove("bg-pink-500");
percentModal.classList.remove("hidden");
});
// Close equal modal
document.getElementById("closeEqual").addEventListener("click", () => {
const input = document.getElementById("equalWinnersInput");
winnerCount = parseInt(input.value) || 2;
if (winnerCount < 2) winnerCount = 2;
if (winnerCount > 5) winnerCount = 5;
bountyData.winnersAllowed = winnerCount;
bountyData.payoutType = "equal";
bountyData.percentages = [];
equalModal.classList.add("hidden");
showToast(`${winnerCount} winners selected for equal split`, "success");
});
// Close percent modal
document.getElementById("closePercent").addEventListener("click", () => {
const input = document.getElementById("percentWinnersInput");
const count = parseInt(input.value) || 2;
if (count < 2) {
showToast("Minimum 2 winners required", "error");
return;
}
if (count > 5) {
showToast("Maximum 5 winners allowed", "error");
return;
}
winnerCount = count;
bountyData.winnersAllowed = winnerCount;
bountyData.payoutType = "percentage";
// Use preset or create default percentages
if (percentageArray.length === 0) {
// Create equal percentages as default
const equalPercent = Math.floor(100 / winnerCount);
const remainder = 100 - equalPercent * winnerCount;
percentageArray = Array(winnerCount).fill(equalPercent);
percentageArray[winnerCount - 1] += remainder;
}
bountyData.percentages = percentageArray;
percentModal.classList.add("hidden");
showToast(
`${winnerCount} winners selected with percentage split`,
"success",
);
});
// Preset buttons
document.querySelectorAll(".preset-btn").forEach((btn) => {
btn.addEventListener("click", () => {
const value = btn.getAttribute("data-value");
percentageArray = JSON.parse(value);
winnerCount = percentageArray.length;
document.getElementById("percentWinnersInput").value = winnerCount;
showToast(`Preset ${value} selected`, "success");
});
});
// Info button
infom.addEventListener("click", (e) => {
e.stopPropagation();
infomenu.classList.toggle("hidden");
});
// Close info menu when clicking outside
document.addEventListener("click", () => {
infomenu.classList.add("hidden");
});
}
function setupTokenDisplay() {
const tokenSelect = document.getElementById("network1");
const tokenSymbols = {
INJ: "INJ",
wETH: "wINJ",
USDT: "USDT",
USDC: "USDC",
};
tokenSelect.addEventListener("change", function () {
const symbol = tokenSymbols[this.value] || "wINJ";
document.querySelectorAll(".tokenType").forEach((el) => {
el.textContent = symbol;
});
updateReviewField("token", symbol);
});
}
function calculateFees() {
// Get reward value, ensure it's a valid number
const reward = parseFloat(bountyData.reward) || 0;
if (reward <= 0) {
// Clear fee and total if reward is invalid
if (feeInput) feeInput.value = "0.00";
if (totalInput) totalInput.value = "0.00";
return;
}
// Calculate fee (5%) and total
const fee = reward * 0.05;
const total = reward + fee;
const symbol = bountyData.token || "WINJ";
console.log(`Calculating: Reward=${reward}, Fee=${fee}, Total=${total}`);
// Update fee input (if it exists)
if (feeInput) {
feeInput.value = fee.toFixed(4);
} else {
// Fallback to old method
const inputs = document.querySelectorAll('#page3 input[type="number"]');
if (inputs.length >= 2) {
inputs[1].value = fee.toFixed(4);
}
}
// Update total input (if it exists)
if (totalInput) {
totalInput.value = total.toFixed(4);
} else {
// Fallback to old method
const inputs = document.querySelectorAll('#page3 input[type="number"]');
if (inputs.length >= 3) {
inputs[2].value = total.toFixed(4);
}
}
// Update review fields
updateReviewField("reward", `${reward.toFixed(2)} ${symbol}`);
updateReviewField("total", `${total.toFixed(2)} ${symbol}`);
}
// ==================== NAVIGATION ====================
function handleNext() {
if (currentStep < 4) {
// Validate current step before proceeding
if (!validateStep(currentStep)) {
return;
}
// Hide current page, show next
pages[currentStep].classList.add("hidden");
currentStep++;
pages[currentStep].classList.remove("hidden");
updateStepIndicators();
updateReviewPage();
// Update button text on last step
if (currentStep === 4) {
nextBtn.textContent = "Create Bounty";
}
} else {
// On last step, submit the bounty
submitBounty();
}
}
function handleBack() {
if (currentStep > 1) {
pages[currentStep].classList.add("hidden");
currentStep--;
pages[currentStep].classList.remove("hidden");
updateStepIndicators();
// Update button text
nextBtn.textContent = currentStep === 4 ? "Create Bounty" : "Next";
}
}
function validateStep(step) {
switch (step) {
case 1:
if (!bountyData.network) {
showToast("Please select a network", "warning");
return false;
}
if (!bountyData.category) {
showToast("Please select a category", "warning");
return false;
}
break;
case 2:
if (!bountyData.title || bountyData.title.length < 5) {
showToast("Title must be at least 5 characters", "warning");
return false;
}
if (!bountyData.description || bountyData.description.length < 20) {
showToast("Description must be at least 20 characters", "warning");
return false;
}
if (!bountyData.startDate || !bountyData.deadline) {
showToast("Please select start and end dates", "warning");
return false;
}
break;
case 3:
if (bountyData.reward <= 0) {
showToast("Please enter a valid reward amount", "warning");
return false;
}
break;
}
return true;
}
function updateStepIndicators() {
for (let i = 1; i <= 4; i++) {
if (i <= currentStep) {
stepIndicators[i].classList.remove("bg-gray-800");
stepIndicators[i].classList.add("bg-pink-500");
} else {
stepIndicators[i].classList.remove("bg-pink-500");
stepIndicators[i].classList.add("bg-gray-800");
}
}
}
// ==================== REVIEW PAGE ====================
function updateReviewPage() {
if (currentStep !== 4) return;
updateReviewField("category", bountyData.category);
updateReviewField("title", bountyData.title);
updateReviewField(
"description",
bountyData.description.substring(0, 100) + "...",
);
updateReviewField("tags", bountyData.tags.join(", "));
updateReviewField("task-detail-link", bountyData.originLink);
updateReviewField("deadline", formatDate(bountyData.deadline));
updateReviewField("reward", `${bountyData.reward} ${bountyData.token}`);
const total = bountyData.reward + bountyData.reward * 0.05;
updateReviewField("total", `${total.toFixed(2)} ${bountyData.token}`);
let winnerText = "No";
if (bountyData.winnersAllowed > 1) {
winnerText = `Yes (${bountyData.winnersAllowed} winners, ${bountyData.payoutType} split)`;
if (bountyData.percentages.length > 0) {
winnerText += ` - ${bountyData.percentages.join("% / ")}%`;
}
}
updateReviewField("multipleWinners", winnerText);
}
function updateReviewField(field, value) {
const reviewElements = document.querySelectorAll("#page4 h4");
reviewElements.forEach((el) => {
if (el.textContent.toLowerCase().includes(field.toLowerCase())) {
const nextEl = el.nextElementSibling;
if (nextEl && nextEl.tagName === "H4") {
nextEl.textContent = value;
}
}
});
}
function formatDate(dateString) {
if (!dateString) return "Not set";
const date = new Date(dateString);
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "short",
day: "numeric",
});
}
// ==================== SUBMISSION ===================
async function submitBounty() {
try {
// Show loading state
nextBtn.disabled = true;
nextBtn.textContent = "Creating on Blockchain...";
// Prepare final bounty data
const finalBountyData = {
title: bountyData.title,
description: bountyData.description,
category: bountyData.category,
tags: bountyData.tags,
startDate: new Date(bountyData.startDate).toISOString(),
deadline: new Date(bountyData.deadline).toISOString(),
originLink: bountyData.originLink,
network: bountyData.network,
reward: bountyData.reward,
token: bountyData.token,
winnersAllowed: bountyData.winnersAllowed,
payoutType: bountyData.payoutType,
percentages: bountyData.percentages,
creator: bountyData.creator,
};
console.log("Creating bounty on blockchain...", finalBountyData);
// Show transaction pending message
showToast("📝 Please confirm transaction in your wallet...", "info");
// Call blockchain to create bounty
console.log(`creator ${bountyData.creator}`);
const chainResult = await createBountyOnChain(
finalBountyData,
bountyData.creator,
);
if (chainResult.success) {
showToast(
`✅ Transaction confirmed! TX: ${chainResult.txHash.slice(0, 10)}...`,
"success",
);
// Now save to backend
nextBtn.textContent = "Saving to Database...";
// Then save to backend with blockchain info
const backendData = {
...bountyData,
blockchainId: chainResult.bountyId
? Number(chainResult.bountyId)
: null, // Convert BigInt to Number,
txHash: chainResult.txHash,
blockNumber: chainResult.blockNumber
? Number(chainResult.blockNumber)
: null, // Convert BigInt to Number,
isOnChain: true,
};
const result = await createBounty(backendData);
if (result && result._id) {
showToast("✅ Bounty created successfully!", "success");
// Store the created bounty ID
sessionStorage.setItem("createdBountyId", result._id);
// Redirect to dashboard after 3 seconds
setTimeout(() => {
window.location.href = "./user_dashboard.html";
}, 3000);
}
} else {
throw new Error("Blockchain transaction failed");
}
} catch (error) {
console.error("Error creating bounty:", error);
// Handle user rejection
if (error.message.includes("rejected") || error.code === 4001) {
showToast("❌ Transaction rejected by user", "error");
} else {
showToast(error.message || "Failed to create bounty", "error");
}
// Reset button
nextBtn.disabled = false;
nextBtn.textContent = "Create Bounty";
}
}
// ==================== TOAST NOTIFICATION ====================
function showToast(message, type = "info") {
// Remove existing toast
const existingToast = document.getElementById("toast");
if (existingToast) {
existingToast.remove();
}
// Create toast element
const toast = document.createElement("div");
toast.id = "toast";
const colors = {
success: "bg-green-600",
error: "bg-red-600",
warning: "bg-yellow-600",
info: "bg-blue-600",
};
toast.className = `fixed bottom-4 right-4 px-6 py-3 rounded-lg text-white z-50 transition-opacity duration-300 ${colors[type] || colors.info}`;
toast.textContent = message;
document.body.appendChild(toast);
// Auto hide after 3 seconds
setTimeout(() => {
toast.style.opacity = "0";
setTimeout(() => toast.remove(), 300);
}, 3000);
}
// ==================== MENU TOGGLES (from header) ====================
document.querySelector(".plusbtn")?.addEventListener("click", (e) => {
e.stopPropagation();
document.querySelector(".plusmenu")?.classList.toggle("hidden");
});
document.querySelector(".profile")?.addEventListener("click", (e) => {
e.stopPropagation();
document.querySelector(".profilemenu")?.classList.toggle("hidden");
});
// Close menus when clicking outside
document.addEventListener("click", () => {
document.querySelector(".plusmenu")?.classList.add("hidden");
document.querySelector(".profilemenu")?.classList.add("hidden");
});
// ==================== CHECK WALLET BALANCE ====================
async function checkWalletBalance(tokenSymbol, amount) {
try {
// Implement balance check here
// You'll need to fetch the user's balance for the selected token
console.log(`Checking ${tokenSymbol} balance...`);
return true; // Placeholder
} catch (error) {
console.error("Error checking balance:", error);
return false;
}
}
// ==================== EXPORT FOR DEBUGGING ====================
// For debugging in console
window.bountyData = bountyData;