diff --git a/.gitignore b/.gitignore
index 2f2efbb..dfb779b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,4 +16,6 @@
.DS_Store
Thumbs.db
build
-dist
\ No newline at end of file
+dist
+
+client/config.js
\ No newline at end of file
diff --git a/client/router/auth-defender.js b/app/auth/auth-defender.js
similarity index 66%
rename from client/router/auth-defender.js
rename to app/auth/auth-defender.js
index 8df0d18..e63a967 100644
--- a/client/router/auth-defender.js
+++ b/app/auth/auth-defender.js
@@ -1,4 +1,4 @@
-import {Authentication} from "../auth/authentication";
+import {Authentication} from "./authentication";
export class AuthDefender {
static canActivate() {
return Authentication.instance.auth;
diff --git a/client/auth/authentication.js b/app/auth/authentication.js
similarity index 82%
rename from client/auth/authentication.js
rename to app/auth/authentication.js
index f72bc53..8063b54 100644
--- a/client/auth/authentication.js
+++ b/app/auth/authentication.js
@@ -6,21 +6,11 @@ export class Authentication {
throw new Error('use instance');
}
- var unparsedAuth = localStorage.getItem('auth');
- if (unparsedAuth) {
- this._auth = JSON.parse(unparsedAuth);
- }
this._callbacks = [];
return Authentication.inst;
}
get auth() {
- if (this._auth) {
- var unparsedAuth = localStorage.getItem('auth');
- if (unparsedAuth) {
- this._auth = JSON.parse(unparsedAuth);
- }
- }
return JSON.parse(localStorage.getItem('auth'));
}
diff --git a/client/components/article.comp.js b/app/components/article.comp.js
similarity index 56%
rename from client/components/article.comp.js
rename to app/components/article.comp.js
index 1e5b5e1..7ba793e 100644
--- a/client/components/article.comp.js
+++ b/app/components/article.comp.js
@@ -1,4 +1,5 @@
import {RouterHandler} from "../router/router-handler";
+import {Authentication} from "../auth/authentication";
"use strict";
export class ArticleComponent extends HTMLElement {
@@ -10,8 +11,6 @@ export class ArticleComponent extends HTMLElement {
heart: 0
};
this.updateHearts = this.updateHearts.bind(this);
-
- this.$tagList = null;
}
static get observedAttributes() {
@@ -29,7 +28,6 @@ export class ArticleComponent extends HTMLElement {
connectedCallback() {
this.innerHTML = this.render();
- // this.$tagList = this.querySelector('ul.tag-list');
const button = this.querySelector('#ion-heart');
button.addEventListener('click', this.updateHearts);
@@ -47,10 +45,59 @@ export class ArticleComponent extends HTMLElement {
}
- updateHearts() {
- var span = this.querySelector('#ion-heart > span');
- this.model.favoritesCount = this.model.favoritesCount + 1;
- span.innerHTML = this.model.favoritesCount;
+ updateHearts(e) {
+ e.preventDefault();
+ var auth = Authentication.instance.auth;
+ if (!auth) {
+ RouterHandler.getInstance.router.navigate('#/login');
+ }
+ let headers = {
+ 'Accept': 'application/json, text/plain, */*',
+ 'Content-Type': 'application/json',
+ 'Authorization': 'Token ' + auth.token
+ };
+ console.log(this.model);
+ if (this.model.favorited) {
+ this.unfavoritArticle(headers);
+ } else {
+ this.favoriteArticle(headers);
+ }
+ // POST /api/articles/:slug/favorite
+
+ // DELETE /api/articles/:slug/favorite
+ }
+
+
+ favoriteArticle(headers) {
+ fetch('https://conduit.productionready.io/api/articles/' + this.model.slug + '/favorite', {
+ headers: headers,
+ method: 'POST'
+ }).then(function (response) {
+ return response.json();
+ }).then(r => {
+ console.log(r);
+ this.model.favorited = r.article.favorited;
+ var span = this.querySelector('#ion-heart > span');
+ span.parentNode.classList.add('active');
+ this.model.favoritesCount = this.model.favoritesCount + 1;
+ span.innerHTML = this.model.favoritesCount;
+ });
+ }
+
+ unfavoritArticle(headers) {
+ fetch('https://conduit.productionready.io/api/articles/' + this.model.slug + '/favorite', {
+ headers: headers,
+ method: 'DELETE'
+ }).then(function (response) {
+ return response.json();
+ }).then(r => {
+ console.log(r);
+ this.model.favorited = r.article.favorited;
+ var span = this.querySelector('#ion-heart > span');
+ this.model.favoritesCount = this.model.favoritesCount - 1;
+ span.parentNode.classList.remove('active');
+ span.innerHTML = this.model.favoritesCount;
+ });
}
@@ -65,7 +112,7 @@ export class ArticleComponent extends HTMLElement {
${this.model.author.username}
${this.model.createdAt}
-