-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomePage.js
More file actions
130 lines (116 loc) · 3.49 KB
/
homePage.js
File metadata and controls
130 lines (116 loc) · 3.49 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
import AuthService from "./AuthUser.js";
import Utilities from "./utilities.js";
const authService = new AuthService();
class Home {
#campaignsContainer = document.getElementById("campaignsContainer");
#rewardsSection = document.getElementById("rewardsSection");
constructor() {
this.lastCampaigns = [];
this.bestRewards = [];
this.initHomePages();
}
async setLastCampaigns() {
const response = await fetch(
`http://localhost:3000/campaigns?_embed=rewards&isApproved=true&_sort=date&_order=desc&_limit=5`
);
const data = await response.json();
this.lastCampaigns = data;
}
get LastCampaigns() {
return this.lastCampaigns;
}
async setBestRewards() {
const response = await fetch(
`http://localhost:3000/rewards?_expand=campaign&_limit=5&_sort=amount&_order=desc`
);
const data = await response.json();
this.bestRewards = data;
}
get BestRewards() {
return this.bestRewards;
}
renderSingleReward(r) {
return `<div className="reward">
${r.title} — ${r.amount}
</div>`;
}
renderRewardsCards(rewards) {
if (rewards.length > 0) {
return `<div className="rewards">
<strong>Pledges: </strong>${rewards.length}
</div>`;
} else {
return `<div className="rewards">No pledges listed.</div>`;
}
}
renderCampaignCard(campaign) {
return `
<div class="campaign-card">
<a href="./campaigns/view/?id=${campaign.id}" class="reset-anchor">
<img src="${campaign.image}" alt="${campaign.title}">
<div class="campaign-content">
<h3>${campaign.title}</h3>
<p>${campaign.description}</p>
<div class="campaign-meta">
<strong>Goal:</strong> ${campaign.goal} <br>
<strong>Achieved:</strong> ${Utilities.sumRewards(
campaign.rewards
)} <br>
<strong>Deadline:</strong> ${campaign.deadline}
</div>
${this.renderRewardsCards(campaign.rewards)}
</div>
</a>
</div>
`;
}
renderCampaigns(data) {
console.log("data=====>", data);
const container = data.map((ele) => this.renderCampaignCard(ele)).join("");
console.log("container", container);
this.#campaignsContainer.insertAdjacentHTML("afterbegin", container);
this.#campaignsContainer.insertAdjacentHTML(
"beforeend",
`<a href='./campaigns/' class="reset-anchor">
<div class="campaign-card show-more">
<spn>+ Show More</span>
</div>
</a>`
);
}
renderRewardCard(reward) {
return `
<div class="reward-card">
<div style="display: flex;justify-content: space-between;align-items: center;">
<h3>${reward.title}</h3>
<p>$${reward.amount}</p>
</div>
<small>Campaign: ${reward.campaign.title}</small>
</div>
`;
}
renderRewardCards(data) {
const rewards = data.map((ele) => this.renderRewardCard(ele)).join("");
return `<div class="rewards-cards">${rewards}</div>`;
}
renderRewards(data) {
this.#rewardsSection.insertAdjacentHTML(
"afterbegin",
this.renderRewardCards(data)
);
}
async initHomePages() {
await this.setLastCampaigns();
await this.setBestRewards();
this.renderCampaigns(this.LastCampaigns);
this.renderRewards(this.BestRewards);
}
}
(async function () {
authService.getStorage();
await authService.renderHeder();
console.log("isLogged", authService.isLoggedIn);
console.log("user authorization");
// render Home Page Content
new Home();
})();