Skip to content

Commit

Permalink
Add initFavoriteButtons helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ellmetha committed Oct 14, 2023
1 parent 8767e4d commit d05fbd1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/apps/blogging/templates/blogging/article_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ <h1>{{ article.title }}</h1>
</button>
&nbsp;&nbsp;
{% endif %}
<button class="btn btn-sm btn-outline-primary">
<button class="favorite-button btn btn-sm {% if favorited %}btn-primary{% else %}btn-outline-primary{% endif %}" data-article-slug="{{ article.slug }}">
<i class="ion-heart"></i>
&nbsp; Favorite Post <span class="counter">(29)</span>
&nbsp; <span class="text">Favorite Article</span> (<span class="counter">{{ article.favorited_by.size }}</span>)
</button>
{% if request.user? && request.user.profile == article.author %}
<a href="{% url 'blogging:article_update' slug: article.slug %}" class="btn btn-sm btn-outline-secondary">
Expand Down Expand Up @@ -71,9 +71,9 @@ <h1>{{ article.title }}</h1>
</button>
&nbsp;&nbsp;
{% endif %}
<button class="btn btn-sm btn-outline-primary">
<button class="favorite-button btn btn-sm {% if favorited %}btn-primary{% else %}btn-outline-primary{% endif %}" data-article-slug="{{ article.slug }}">
<i class="ion-heart"></i>
&nbsp; Favorite Article <span class="counter">(29)</span>
&nbsp; <span class="text">Favorite Article</span> (<span class="counter">{{ article.favorited_by.size }}</span>)
</button>
{% if request.user? && request.user.profile == article.author %}
<a href="{% url 'blogging:article_update' slug: article.slug %}" class="btn btn-sm btn-outline-secondary">
Expand Down
2 changes: 2 additions & 0 deletions src/assets/js/controllers/blogging/ArticleDetailController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import initFavoriteButtons from '../../core/initFavoriteButtons';
import initFollowButtons from '../../core/initFollowButtons';

export default {
async init() {
const articlePageDiv = document.getElementById('article_page');
await initFollowButtons(articlePageDiv.dataset.authorUsername);
await initFavoriteButtons();
},
};
35 changes: 35 additions & 0 deletions src/assets/js/core/initFavoriteButtons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import axios from 'axios';

import getCSRFToken from './getCSRFToken';

export default async function initFavoriteButtons() {
const favoriteButtons = document.querySelectorAll('.favorite-button');

if (favoriteButtons.length > 0) {
const csrfToken = getCSRFToken();
favoriteButtons.forEach((favoriteButton) => {
const slug = favoriteButton.dataset.articleSlug;
favoriteButton.addEventListener('click', async () => {
const response = await axios.post(
`/article/${slug}/favorite`,
{},
{ headers: { 'X-CSRF-Token': csrfToken } },
);

favoriteButtons.forEach((b) => {
if (b.dataset.articleSlug === slug) {
b.classList.toggle('btn-outline-primary');
b.classList.toggle('btn-primary');
const counter = b.querySelector('.counter');

if (response.data.favorited) {
counter.textContent = parseInt(counter.textContent, 10) + 1;
} else {
counter.textContent = parseInt(counter.textContent, 10) - 1;
}
}
});
});
});
}
}

0 comments on commit d05fbd1

Please sign in to comment.