forked from augustocesarfmo/clone-instagram-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
142 lines (112 loc) · 3.44 KB
/
index.ts
File metadata and controls
142 lines (112 loc) · 3.44 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
import { v4 as randomUUID } from "uuid";
import { faker } from "@faker-js/faker";
class Post {
private _id: string = randomUUID();
private _userName: string;
private _avatarUrl: string;
private _imageUrl: string;
private _isLiked: boolean = false;
private _description: string;
private _createdAt: Date = new Date();
private _numberOfLikes: number = 0;
constructor(
userName: string,
avatarUrl: string,
imageUrl: string,
description: string
) {
this._userName = userName.toLowerCase();
this._avatarUrl = avatarUrl;
this._imageUrl = imageUrl;
this._description = description;
}
like() {
const postContainer = document.getElementById(this._id);
if (!postContainer) return;
this.updateLikeIcon(postContainer);
this.updateTextNumberOfLikes(postContainer);
this._isLiked = !this._isLiked;
}
private updateLikeIcon(postHTML: HTMLElement) {
const btnLike = postHTML.querySelector("#btn-like");
const icon = btnLike?.children[0];
if (!icon) return;
icon.classList.toggle("fa-heart");
icon.classList.toggle("liked");
icon.classList.toggle("fa-heart-o");
}
private updateTextNumberOfLikes(postHTML: HTMLElement) {
const postLikes = postHTML.querySelector(".post-likes");
const span = postLikes?.querySelector("span");
if (!span) return;
if (this._isLiked) {
this._numberOfLikes -= 1;
} else {
this._numberOfLikes += 1;
}
span.textContent = this._numberOfLikes.toString();
}
toHTML() {
const postContainer = document.createElement("div");
postContainer.className = "post-container";
postContainer.id = this._id;
const postHeader = `
<div class="post-header">
<div>
<img title="Avatar image"
src=${this._avatarUrl}>
</div>
<span>${this._userName}</span>
</div>
`;
const postImage = `
<div class="post-image">
<img title="Post image"
src=${this._imageUrl}>
</div>
`;
const postIcons = `
<div class="post-icons">
<div>
<div id="btn-like" class="btn">
<i class="fa fa-heart-o"></i>
</div>
<div class="btn">
<i class="fa fa-comment-o"></i>
</div>
<div class="btn">
<i class="fa fa-paper-plane-o"></i>
</div>
</div>
<div class="btn">
<i class="fa fa-bookmark-o"></i>
</div>
</div>
`;
const postLikes = `
<div class="post-likes">
<i class="fa fa-heart"></i>
<div><span>${this._numberOfLikes}</span> likes</div>
</div>
`;
postContainer.innerHTML = postHeader;
postContainer.innerHTML += postImage;
postContainer.innerHTML += postIcons;
postContainer.innerHTML += postLikes;
const btnLike = postContainer.querySelector("#btn-like");
btnLike?.addEventListener("click", () => this.like());
const ulPostsHTML = document.querySelector("ul");
if (!ulPostsHTML) return;
const liHTML = document.createElement("li");
liHTML.appendChild(postContainer);
ulPostsHTML.appendChild(liHTML);
}
}
for (let index = 0; index < 15; index++) {
const userName = faker.person.firstName();
const avatarUrl = faker.image.avatarGitHub();
const imageUrl = faker.image.urlLoremFlickr();
const description = faker.lorem.paragraph();
const post = new Post(userName, avatarUrl, imageUrl, description);
post.toHTML();
}