1- import { apiService } from "../index.mjs" ;
1+ import { apiService } from "../index.mjs" ;
22
33/**
44 * Create a profile component
55 * @param {string } template - The ID of the template to clone
66 * @param {Object } profileData - The profile data to display
77 * @returns {DocumentFragment } - The profile UI
88 */
9- function createProfile ( template , { profileData, whoToFollow, isLoggedIn} ) {
9+ function createProfile ( template , { profileData, whoToFollow, isLoggedIn } ) {
1010 if ( ! template || ! profileData ) return ;
1111 const profileElement = document
1212 . getElementById ( template )
@@ -19,22 +19,36 @@ function createProfile(template, {profileData, whoToFollow, isLoggedIn}) {
1919 ) ;
2020 const followerCountEl = profileElement . querySelector ( "[data-follower-count]" ) ;
2121 const followButtonEl = profileElement . querySelector ( "[data-action='follow']" ) ;
22- const whoToFollowContainer = profileElement . querySelector ( ".profile__who-to-follow" ) ;
22+ const whoToFollowContainer = profileElement . querySelector (
23+ ".profile__who-to-follow"
24+ ) ;
2325 // Populate with data
2426 usernameEl . querySelector ( "h2" ) . textContent = profileData . username || "" ;
2527 usernameEl . setAttribute ( "href" , `/profile/${ profileData . username } ` ) ;
2628 bloomCountEl . textContent = profileData . total_blooms || 0 ;
2729 followerCountEl . textContent = profileData . followers ?. length || 0 ;
2830 followingCountEl . textContent = profileData . follows ?. length || 0 ;
2931 followButtonEl . setAttribute ( "data-username" , profileData . username || "" ) ;
30- followButtonEl . hidden = profileData . is_self || profileData . is_following ;
31- followButtonEl . addEventListener ( "click" , handleFollow ) ;
32+ // followButtonEl.hidden = profileData.is_self || profileData.is_following;
33+ // followButtonEl.addEventListener("click", handleFollow);
34+ followButtonEl . hidden = profileData . is_self ;
35+
36+ // Set button text and action based on follow status
37+ if ( profileData . is_following ) {
38+ followButtonEl . textContent = "Unfollow" ;
39+ followButtonEl . addEventListener ( "click" , handleUnfollow ) ;
40+ } else {
41+ followButtonEl . textContent = "Follow" ;
42+ followButtonEl . addEventListener ( "click" , handleFollow ) ;
43+ }
3244 if ( ! isLoggedIn ) {
3345 followButtonEl . style . display = "none" ;
3446 }
3547
3648 if ( whoToFollow . length > 0 ) {
37- const whoToFollowList = whoToFollowContainer . querySelector ( "[data-who-to-follow]" ) ;
49+ const whoToFollowList = whoToFollowContainer . querySelector (
50+ "[data-who-to-follow]"
51+ ) ;
3852 const whoToFollowTemplate = document . querySelector ( "#who-to-follow-chip" ) ;
3953 for ( const userToFollow of whoToFollow ) {
4054 const wtfElement = whoToFollowTemplate . content . cloneNode ( true ) ;
@@ -43,7 +57,15 @@ function createProfile(template, {profileData, whoToFollow, isLoggedIn}) {
4357 usernameLink . setAttribute ( "href" , `/profile/${ userToFollow . username } ` ) ;
4458 const followButton = wtfElement . querySelector ( "button" ) ;
4559 followButton . setAttribute ( "data-username" , userToFollow . username ) ;
46- followButton . addEventListener ( "click" , handleFollow ) ;
60+ // followButton.addEventListener("click", handleFollow);
61+
62+ if ( userToFollow . is_following ) {
63+ followButton . textContent = "Unfollow" ;
64+ followButton . addEventListener ( "click" , handleUnfollow ) ;
65+ } else {
66+ followButton . textContent = "Follow" ;
67+ followButton . addEventListener ( "click" , handleFollow ) ;
68+ }
4769 if ( ! isLoggedIn ) {
4870 followButton . style . display = "none" ;
4971 }
@@ -66,4 +88,12 @@ async function handleFollow(event) {
6688 await apiService . getWhoToFollow ( ) ;
6789}
6890
69- export { createProfile , handleFollow } ;
91+ async function handleUnfollow ( event ) {
92+ const button = event . target ;
93+ const username = button . getAttribute ( "data-username" ) ;
94+ if ( ! username ) return ;
95+
96+ await apiService . unfollowUser ( username ) ;
97+ await apiService . getWhoToFollow ( ) ;
98+ }
99+ export { createProfile , handleFollow , handleUnfollow } ;
0 commit comments