File size: 7,723 Bytes
f710fc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{% extends "layout.html" %}

{% block content %}
  <div class="user-profile__container">
    <div class="user-profile__rows">
      <div class="user-profile__left-column">
        <div class="text-center">
          <img src="{{ url_for('static', filename='users/uploaded_images/' + user.profile_pic) }}" alt="Profile Picture"
            class="user-profile__profile-picture img-thumbnail">
        </div>
        {% if current_user.is_authenticated and current_user.id != user.id %}
          <div class="text-center mt-3">
            {% if current_user.is_following(user) %}
              <form action="{{ url_for('unfollow', user_id=profile_user_id, page='profile') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <button type="submit" class="user-profile__follow-button btn btn-danger"><i class="fa-solid fa-user-minus"></i> Unfollow</button>
              </form>
            {% else %}
              <form action="{{ url_for('follow', user_id=profile_user_id, page='profile') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
                <button type="submit" class="user-profile__follow-button btn btn-primary"><i class="fa-solid fa-user-plus"></i> Follow</button>
              </form>
            {% endif %}
          </div>
        {% endif %}
      </div>

      <div class="user-profile__right-column">
        <h1 class="title">{{ user.username }}</h1>
        <p class="user-profile__email"><strong>Email:</strong> {{ user.email }}</p>
        {% if user.first_name or user.last_name %}
          <p class="user-profile__name"><strong>Name:</strong> {{ user.first_name }} {{ user.last_name }}</p>
        {% endif %}
        {% if user.profession %}
          <p class="user-profile__profession"><strong>Profession:</strong> {{ user.profession }}</p>
        {% endif %}
        {% if user.location %}
          <p class="user-profile__location"><strong>Location:</strong> {{ user.location }}</p>
        {% endif %}
      </div>
    </div>
  </div>

  <hr>

  <h3>Posts</h3>

  <div class="row">
    <div class="col-md-12">
      {% if posts %}
        {% for post in posts %}
          <div class="post mb-4">
            <div class="top">
              <a href="user/{{ post.author.id }}">
                <img class="post_profile_picture" src="{{ url_for('static', filename='users/uploaded_images/' + post.author.profile_pic) }}" alt="Profile Image">
                <p class="profile_name">{{ post.author.username }}</p>
              </a>
              <div class="dropdown">
                <button class="btn dropdown-toggle" type="button" id="dropdownMenuButton{{ post.id }}" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <i class="fas fa-ellipsis-vertical"></i>
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton{{ post.id }}">
                  <a class="dropdown-item" href="#" onclick="deletePost({{ post.id }})">Delete Post</a>
                </div>
              </div>
            </div>
            {% if post.filename %}
              <img class="post-image" src="{{ url_for('static', filename='users/uploaded_images/' + post.filename) }}" alt="Post Image">
            {% endif %}
            <p class="caption">{{ post.content }}</p>
            <div class="post-interactions">
              <p class="post-likes">
                <i class="fas fa-heart {% if not current_user.has_liked_post(post) %}heartIcon{% endif %}{% if current_user.has_liked_post(post) %}liked{% endif %}" onclick="likePost({{ post.id }})" id="heartIcon{{ post.id }}"></i>
                <span class="likes" id="likeCount{{ post.id }}">{{ post.likes_count }}</span>
              </p>
              <p class="post-comments" onclick="showComments({{ post.id }})">
                <i class="fas fa-comment CommentIcon"></i> {{ post.comments | length }}
              </p>
              <p class="post-info">Posted by {{ post.author.username }} on {{ post.created_at.strftime('%Y-%m-%d') }}</p>
            </div>
            <!-- Comments Modal -->
            <div class="modal fade" id="commentsModal{{ post.id }}" tabindex="-1" role="dialog" aria-labelledby="commentsModalLabel" aria-hidden="true">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h5 class="modal-title" id="commentsModalLabel">Comments</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <ul class="list-group">
                      {% for comment in post.comments %}
                        <li class="list-group-item">
                          <strong>{{ comment.author.username }}:</strong> {{ comment.content }}
                        </li>
                      {% endfor %}
                    </ul>
                  </div>
                  <div class="modal-footer">
                    <form method="POST" action="{{ url_for('add_comment', post_id=post.id) }}">
                      <div class="form-group">
                        <textarea name="content" class="form-control" placeholder="Write your comment here" required></textarea>
                      </div>
                      <button type="submit" class="btn btn-primary">Submit Comment</button>
                    </form>
                  </div>
                </div>
              </div>
            </div>
          </div>
        {% endfor %}
      {% else %}
        <div class="col-md-12">
          <p>No posts yet.</p>
        </div>
      {% endif %}
    </div>
  </div>
  <script>
    function likePost(postId) {
      fetch(`/like_post/${postId}`, { method: 'POST' })
        .then((response) => response.json())
        .then((data) => {
          const likeCountSpan = document.getElementById(`likeCount${postId}`);
          likeCountSpan.textContent = data.likes_count;
  
          const heartIcon = document.getElementById(`heartIcon${postId}`);
          if (data.liked) {
            heartIcon.classList.add('liked'); 
            heartIcon.classList.remove('heartIcon');
          } else {
            heartIcon.classList.remove('liked');
            heartIcon.classList.add('heartIcon');
          }
        });
    }
  
    function showComments(postId) {
      $(`#commentsModal${postId}`).modal('show');
    }
    function deletePost(postId) {
      fetch(`/delete_post/${postId}`, { method: 'POST' })
        .then((response) => {
          if (response.ok) {
            const postElement = document.querySelector(`.post[data-post-id="${postId}"]`);
            if (postElement) {
              postElement.remove();
            }
          } else {
            console.error('Error deleting post:', response);
          }
        })
        .catch((error) => {
          console.error('Error deleting post:', error);
        });
    }

    document.addEventListener('click', function (event) {
      const dropdownToggle = event.target.closest('.dropdown-toggle');
      if (dropdownToggle) {
        const dropdownMenu = dropdownToggle.nextElementSibling;
        if (dropdownMenu) {
          dropdownMenu.classList.toggle('show');
        }
      } else {
        const dropdownMenus = document.querySelectorAll('.dropdown-menu');
        dropdownMenus.forEach((menu) => {
          menu.classList.remove('show');
        });
      }
    });
  </script>
{% endblock %}