-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathprofile.mjs
More file actions
101 lines (87 loc) · 3.5 KB
/
profile.mjs
File metadata and controls
101 lines (87 loc) · 3.5 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
import { apiService } from "../index.mjs";
/**
* Create a profile component
* @param {string} template - The ID of the template to clone
* @param {Object} profileData - The profile data to display
* @returns {DocumentFragment} - The profile UI
*/
function createProfile(template, { profileData, whoToFollow, isLoggedIn }) {
if (!template || !profileData) return;
const profileElement = document
.getElementById(template)
.content.cloneNode(true);
const usernameEl = profileElement.querySelector("[data-username]");
const bloomCountEl = profileElement.querySelector("[data-bloom-count]");
const followingCountEl = profileElement.querySelector(
"[data-following-count]",
);
const followerCountEl = profileElement.querySelector("[data-follower-count]");
const followButtonEl = profileElement.querySelector("[data-action='follow']");
const unfollowButtonEl = profileElement.querySelector(
"[data-action='unfollow']",
);
const whoToFollowContainer = profileElement.querySelector(
".profile__who-to-follow",
);
// Populate with data
usernameEl.querySelector("h2").textContent = profileData.username || "";
usernameEl.setAttribute("href", `/profile/${profileData.username}`);
bloomCountEl.textContent = profileData.total_blooms || 0;
followerCountEl.textContent = profileData.followers?.length || 0;
followingCountEl.textContent = profileData.follows?.length || 0;
followButtonEl.dataset.username = profileData.username || "";
followButtonEl.hidden = profileData.is_self || profileData.is_following;
followButtonEl.addEventListener("click", handleFollow);
unfollowButtonEl.dataset.username = profileData.username || "";
unfollowButtonEl.hidden = profileData.is_self || !profileData.is_following;
unfollowButtonEl.addEventListener("click", handleUnfollow);
if (!isLoggedIn) {
followButtonEl.style.display = "none";
unfollowButtonEl.style.display = "none";
}
if (whoToFollow.length > 0) {
const whoToFollowList = whoToFollowContainer.querySelector(
"[data-who-to-follow]",
);
const whoToFollowTemplate = document.querySelector("#who-to-follow-chip");
for (const userToFollow of whoToFollow) {
const wtfElement = whoToFollowTemplate.content.cloneNode(true);
const usernameLink = wtfElement.querySelector("a[data-username]");
usernameLink.innerText = userToFollow.username;
usernameLink.setAttribute("href", `/profile/${userToFollow.username}`);
const followButton = wtfElement.querySelector("button");
followButton.dataset.username = userToFollow.username;
followButton.addEventListener("click", handleFollow);
if (!isLoggedIn) {
followButton.style.display = "none";
}
whoToFollowList.appendChild(wtfElement);
}
} else {
whoToFollowContainer.innerText = "";
}
return profileElement;
}
async function handleFollow(event) {
const button = event.target;
const username = button.dataset.username;
if (!username) return;
await apiService.followUser(username);
await apiService.getWhoToFollow();
}
async function handleUnfollow(event) {
const button = event.target;
const username = button.dataset.username;
if (!username) return;
const originalText = button.textContent;
try {
button.disabled = true;
button.textContent = "Unfollowing...";
await apiService.unfollowUser(username);
await apiService.getWhoToFollow();
} finally {
button.textContent = originalText;
button.disabled = false;
}
}
export { createProfile, handleFollow, handleUnfollow };