From 98c29f5694d93bd2ce70adc80a014e3136b4a750 Mon Sep 17 00:00:00 2001 From: YuukanOO Date: Fri, 22 Jan 2021 09:50:14 +0100 Subject: [PATCH] add link upvote/downvote and authenticated comp --- hn-vue/src/Layout.vue | 20 ++++++++++++---- hn-vue/src/api.js | 32 +++++++++++++++++++++++++ hn-vue/src/components/Authenticated.vue | 9 +++++++ hn-vue/src/components/LinkItem.vue | 32 ++++++++++++++++++++++++- hn-vue/src/pages/LinkDetail.vue | 5 +++- hn-vue/src/pages/Links.vue | 10 +++++++- hn-vue/src/store.js | 4 ++++ 7 files changed, 105 insertions(+), 7 deletions(-) create mode 100644 hn-vue/src/components/Authenticated.vue diff --git a/hn-vue/src/Layout.vue b/hn-vue/src/Layout.vue index b5792e2..2c531d2 100644 --- a/hn-vue/src/Layout.vue +++ b/hn-vue/src/Layout.vue @@ -3,9 +3,20 @@ @@ -14,8 +25,9 @@ diff --git a/hn-vue/src/api.js b/hn-vue/src/api.js index 00214ca..079c839 100644 --- a/hn-vue/src/api.js +++ b/hn-vue/src/api.js @@ -16,6 +16,14 @@ class Api { return this._get(`/api/links/${id}`); } + upvoteLink(id) { + return this._put(`/api/links/${id}/upvote`); + } + + downvoteLink(id) { + return this._put(`/api/links/${id}/downvote`); + } + getLinkComments(id) { return this._get(`/api/links/${id}/comments`); } @@ -80,6 +88,30 @@ class Api { return r.json(); }); } + + _put(path, data) { + return fetch(this._baseUrl + path, { + method: "PUT", + headers: { + Accept: "application/json", + "Content-Type": "application/json", + ...this._authorizationHeader(), + }, + body: JSON.stringify(data), + }).then((r) => { + if (!r.ok) { + return Promise.reject(new Error("Erreur dans la requête")); + } + + if (r.status === 204) { + return r; + } + + // if (!r.headers['Content-Length'] === '0') { + + return r.json(); + }); + } } export default new Api(process.env.VUE_APP_API_URL); diff --git a/hn-vue/src/components/Authenticated.vue b/hn-vue/src/components/Authenticated.vue new file mode 100644 index 0000000..e4ce145 --- /dev/null +++ b/hn-vue/src/components/Authenticated.vue @@ -0,0 +1,9 @@ + diff --git a/hn-vue/src/components/LinkItem.vue b/hn-vue/src/components/LinkItem.vue index 38aaac4..8319334 100644 --- a/hn-vue/src/components/LinkItem.vue +++ b/hn-vue/src/components/LinkItem.vue @@ -6,18 +6,48 @@ }}

Publié le {{ link.createdAt }} par {{ link.createdBy.username }}

-

👍 {{ link.upvotesCount }} / 👎 {{ link.downvotesCount }}

+ + + / + + diff --git a/hn-vue/src/pages/LinkDetail.vue b/hn-vue/src/pages/LinkDetail.vue index fb2aff2..ebaaa37 100644 --- a/hn-vue/src/pages/LinkDetail.vue +++ b/hn-vue/src/pages/LinkDetail.vue @@ -7,7 +7,7 @@ message="Chargement du lien en cours ..." />
- +

Aucun commentaire pour ce lien

    @@ -52,6 +52,9 @@ export default { // '$route': 'fetchLinkDetail' }, methods: { + async refreshLink() { + this.link = await api.getLinkById(this.id); + }, async fetchLinkDetail() { this.link = null; this.comments = null; diff --git a/hn-vue/src/pages/Links.vue b/hn-vue/src/pages/Links.vue index eea77d1..c0b2d4f 100644 --- a/hn-vue/src/pages/Links.vue +++ b/hn-vue/src/pages/Links.vue @@ -8,7 +8,7 @@ />
    • - +
@@ -41,6 +41,14 @@ export default { // this.loading = false; // } }, + methods: { + async refreshLink(id) { + const link = await api.getLinkById(id); + const index = this.links.findIndex((l) => l.id === id); + + this.links.splice(index, 1, link); + }, + }, }; diff --git a/hn-vue/src/store.js b/hn-vue/src/store.js index f1f9368..9d91a5c 100644 --- a/hn-vue/src/store.js +++ b/hn-vue/src/store.js @@ -40,6 +40,10 @@ export default function createStore() { state.token = token; state.username = username; }, + logout(state) { + state.token = null; + state.username = null; + }, }, actions: { async login({ commit }, data) {