|
@@ -130,13 +130,43 @@
|
|
|
|
|
|
|
|
|
function favoriteBtn(indexNumber) {
|
|
|
+ const favoriteNum = event.target.querySelector('.count').textContent;
|
|
|
+ let counts = parseInt(favoriteNum);
|
|
|
+ let newCounts = counts + 1;
|
|
|
+
|
|
|
const favBtn = document.querySelectorAll('.favorite-btn');
|
|
|
if ("${ssIsLogin}" === "true") {
|
|
|
favBtn.forEach(value => {
|
|
|
if (value.classList.contains(indexNumber)) {
|
|
|
- value.classList.toggle('active');
|
|
|
+ if(event.target.classList.contains('active')){
|
|
|
+ event.target.querySelector('.count').textContent = counts - 1;
|
|
|
+ fetch('/article/' + indexNumber + '/favorite', { method: 'DELETE' })
|
|
|
+ .then(response => {
|
|
|
+ if(response.status === 200) {
|
|
|
+ return value.classList.remove('active');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ event.target.querySelector('.count').textContent = newCounts;
|
|
|
+ fetch('/article/'+ indexNumber + '/favorite', {
|
|
|
+ body: JSON.stringify({
|
|
|
+ articleId: indexNumber,
|
|
|
+ userId: "${ssId}",
|
|
|
+ created: new Date()
|
|
|
+ }),
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json"
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then(response => {
|
|
|
+ if(response.status === 201){
|
|
|
+ return value.classList.add('active');
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
- })
|
|
|
+ });
|
|
|
} else {
|
|
|
location.href = "/user/signin"
|
|
|
}
|
|
@@ -184,42 +214,45 @@
|
|
|
|
|
|
const domStrArticleMeta =
|
|
|
`
|
|
|
- <div class="article-meta">
|
|
|
- <div class="metadata">
|
|
|
- <a href="#" class="profile-link">
|
|
|
- <img src="/resources/images/avatar.png" alt="avatar">
|
|
|
- </a>
|
|
|
- <div class="article-info">
|
|
|
- <a href="#" class="name"></a>
|
|
|
- <span class="date"></span>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- <div>
|
|
|
- <button class="favorite-btn \${article.id}" onclick="favoriteBtn(\${article.id})">
|
|
|
- <i class="fas fa-heart"></i>
|
|
|
- <span class="count">564</span>
|
|
|
- </button>
|
|
|
- </div>
|
|
|
+ <div class="article-meta">
|
|
|
+ <div class="metadata">
|
|
|
+ <a href="#" class="profile-link">
|
|
|
+ <img src="/resources/images/avatar.png" alt="avatar">
|
|
|
+ </a>
|
|
|
+ <div class="article-info">
|
|
|
+ <a href="#" class="name"></a>
|
|
|
+ <span class="date"></span>
|
|
|
</div>
|
|
|
- `;
|
|
|
+ </div>
|
|
|
+ <div>
|
|
|
+ <button class="favorite-btn \${article.id}" onclick="favoriteBtn(\${article.id})">
|
|
|
+ <i class="fas fa-heart"></i>
|
|
|
+ <span class="count">\${article.favoriteNum}</span>
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ `;
|
|
|
const divArticleMeta = domParser.parseFromString(domStrArticleMeta, 'text/html').body.firstChild;
|
|
|
divArticleMeta.querySelector('.name').textContent = article.writerName;
|
|
|
divArticleMeta.querySelector('.date').textContent = new Date(article.created).toLocaleString();
|
|
|
-
|
|
|
+ console.log(article.favorite)
|
|
|
+ if(article.favorite) {
|
|
|
+ divArticleMeta.querySelector('.favorite-btn').classList.add('active');
|
|
|
+ }
|
|
|
|
|
|
const domStrPreviewLink =
|
|
|
`
|
|
|
- <a href="/article/\${article.id}" class="preview-link">
|
|
|
- <h1 class="preview-title"></h1>
|
|
|
- <p></p>
|
|
|
- <div class="tag-data">
|
|
|
- <span>Read more...</span>
|
|
|
- <ul class = "tag-list">
|
|
|
-
|
|
|
- </ul>
|
|
|
- </div>
|
|
|
- </a>
|
|
|
- `;
|
|
|
+ <a href="/article/\${article.id}" class="preview-link">
|
|
|
+ <h1 class="preview-title"></h1>
|
|
|
+ <p></p>
|
|
|
+ <div class="tag-data">
|
|
|
+ <span>Read more...</span>
|
|
|
+ <ul class = "tag-list">
|
|
|
+
|
|
|
+ </ul>
|
|
|
+ </div>
|
|
|
+ </a>
|
|
|
+ `;
|
|
|
const aPreviewLink = domParser.parseFromString(domStrPreviewLink, 'text/html').body.firstChild;
|
|
|
aPreviewLink.querySelector('.preview-title').textContent = article.title;
|
|
|
aPreviewLink.querySelector('p').textContent = article.subtitle;
|
|
@@ -359,6 +392,7 @@
|
|
|
// hide loadings
|
|
|
setLoading('off');
|
|
|
|
|
|
+
|
|
|
// loadingTag.style.display = 'none';
|
|
|
// loadingArticle.style.display = 'none';
|
|
|
|