Skip to content

Commit 879e9f7

Browse files
prevent infinite render loop by checking if hashtag has changed
1 parent c0add7d commit 879e9f7

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

front-end/views/hashtag.mjs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {renderOne, renderEach, destroy} from "../lib/render.mjs";
1+
import { renderOne, renderEach, destroy } from "../lib/render.mjs";
22
import {
33
state,
44
apiService,
@@ -14,36 +14,43 @@ import {createHeading} from "../components/heading.mjs";
1414

1515
// Hashtag view: show all tweets containing this tag
1616

17-
function hashtagView(hashtag) {
17+
async function hashtagView(hashtag) {
1818
destroy();
1919

20-
apiService.getBloomsByHashtag(hashtag);
20+
const hashtagWithHash = hashtag.startsWith("#") ? hashtag : `#${hashtag}`;
21+
if (hashtagWithHash !== state.currentHashtag) {
22+
await apiService.getBloomsByHashtag(hashtagWithHash);
23+
}
2124

22-
renderOne(
23-
state.isLoggedIn,
24-
getLogoutContainer(),
25-
"logout-template",
26-
createLogout
27-
);
28-
document
29-
.querySelector("[data-action='logout']")
30-
?.addEventListener("click", handleLogout);
31-
renderOne(
32-
state.isLoggedIn,
33-
getLoginContainer(),
34-
"login-template",
35-
createLogin
36-
);
37-
document
38-
.querySelector("[data-action='login']")
39-
?.addEventListener("click", handleLogin);
25+
if (state.isLoggedIn) {
26+
renderOne(
27+
true,
28+
getLogoutContainer(),
29+
"logout-template",
30+
createLogout
31+
);
32+
document
33+
.querySelector("[data-action='logout']")
34+
?.addEventListener("click", handleLogout);
35+
} else {
36+
renderOne(
37+
false,
38+
getLoginContainer(),
39+
"login-template",
40+
createLogin
41+
);
42+
document
43+
.querySelector("[data-action='login']")
44+
?.addEventListener("click", handleLogin);
45+
}
4046

4147
renderOne(
4248
state.currentHashtag,
4349
getHeadingContainer(),
4450
"heading-template",
4551
createHeading
4652
);
53+
4754
renderEach(
4855
state.hashtagBlooms || [],
4956
getTimelineContainer(),
@@ -52,4 +59,4 @@ function hashtagView(hashtag) {
5259
);
5360
}
5461

55-
export {hashtagView};
62+
export {hashtagView};

0 commit comments

Comments
 (0)