Skip to content
Snippets Groups Projects
Commit 33dac6c6 authored by thomas.blanc.2@etu.univ-amu.fr's avatar thomas.blanc.2@etu.univ-amu.fr
Browse files

Added comment support

parent 515634db
Branches
No related tags found
No related merge requests found
...@@ -170,6 +170,21 @@ export const api = { ...@@ -170,6 +170,21 @@ export const api = {
return false return false
} }
}, },
async removeComment (id) {
const token = await localStorage.getItem('token')
const url = serverurl + 'comments' + '?id=' + id
try {
await fetch(url, {
method: 'DELETE',
headers: {
Authorization: 'Bearer ' + token
}
})
return true
} catch (error) {
return false
}
},
async removeUser (id) { async removeUser (id) {
const token = await localStorage.getItem('token') const token = await localStorage.getItem('token')
const url = serverurl + 'user' + '?id=' + id const url = serverurl + 'user' + '?id=' + id
...@@ -227,5 +242,51 @@ export const api = { ...@@ -227,5 +242,51 @@ export const api = {
} catch (error) { } catch (error) {
return null return null
} }
},
async getModelComments (id) {
const url = serverurl + 'comments' + '?id=' + id
const response = await fetch(url)
try {
return await response.json()
} catch (error) {
return null
}
},
async getCommentList (id) {
const token = await localStorage.getItem('token')
const url = serverurl + 'comments/list'
const response = await fetch(url, {
headers: {
Authorization: 'Bearer ' + token
}
})
try {
return await response.json()
} catch (error) {
return null
}
},
async addComment (id, content) {
const url = serverurl + 'comments'
const token = await localStorage.getItem('token')
try {
const response = await fetch(
url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token
},
body: JSON.stringify({
modelId: id,
content: content
})
}
)
return await response.json()
} catch (error) {
return null
}
} }
} }
<template> <template>
<div class="comments" v-if="comments"> <div class="comments">
<h1 class="title is-5">{{comments.length}} comments</h1> <h1 class="title is-5">{{comments.length}} comments</h1>
<article class="media" v-for="comment in comments" v-bind:key="comment.id"> <article class="media" v-for="comment in comments" v-bind:key="comment.id">
<div class="media-content"> <div class="media-content">
<div class="content"> <div class="content">
<p> <p>
<strong>{{comment.author}}</strong> - <small>{{new Date(comment.date).toLocaleDateString()}}</small> <strong>{{comment.author.username}}</strong> - <small>{{new Date(comment.added).toLocaleDateString()}}</small>
<br> <br>
{{comment.content}} {{comment.content}}
</p> </p>
</div> </div>
</div> </div>
<div class="media-right details" v-if="isAdmin">
<b-button class="actionButton" icon-left="delete" type="is-danger" @click="removePrompt(comment.id)" outlined/>
</div>
</article> </article>
<div v-if="isLoggedIn">
<hr>
<b-field label="Add a comment">
<b-input maxlength="1000" type="textarea" v-model="postContent"/>
</b-field>
<b-field>
<b-button @click="postComment">Post</b-button>
</b-field>
</div>
</div> </div>
</template> </template>
<script> <script>
import { api } from '@/api.js'
export default { export default {
name: 'Comments', name: 'Comments',
props: ['comments'] props: ['modelId'],
data () {
return {
isLoggedIn: false,
isLoading: true,
isAdmin: false,
comments: [],
postContent: ''
}
},
async mounted () {
this.comments = await api.getModelComments(this.modelId)
const userRole = await localStorage.getItem('user_role')
if (userRole) this.isLoggedIn = true
if (userRole === 'ROLE_ADMIN') this.isAdmin = true
},
methods: {
async postComment () {
await api.addComment(this.modelId, this.postContent)
this.comments = await api.getModelComments(this.modelId)
this.postContent = null
},
removePrompt (id) {
this.$buefy.dialog.confirm({
message: 'Delete comment ?',
cancelText: 'Abort',
confirmText: 'Delete',
type: 'is-danger',
hasIcon: true,
onConfirm: async () => {
await api.removeComment(id)
this.comments = await api.getModelComments(this.modelId)
this.$buefy.toast.open('Comment deleted')
}
})
}
}
} }
</script> </script>
......
...@@ -38,7 +38,7 @@ export default { ...@@ -38,7 +38,7 @@ export default {
} }
}, },
async mounted () { async mounted () {
this.models = await api.getModels() this.models = await api.getUserModels()
this.isLoading = false this.isLoading = false
}, },
methods: { methods: {
...@@ -49,24 +49,12 @@ export default { ...@@ -49,24 +49,12 @@ export default {
confirmText: 'Delete', confirmText: 'Delete',
type: 'is-danger', type: 'is-danger',
hasIcon: true, hasIcon: true,
onConfirm: () => this.remove(id, name) onConfirm: async () => {
}) await api.removeModel(id)
},
async remove (id, name) {
const token = await localStorage.getItem('token')
const url = this.$serverurl + 'models' + '?id=' + id
try {
await fetch(url, {
method: 'DELETE',
headers: {
Authorization: 'Bearer ' + token
}
})
this.$buefy.toast.open(name + ' deleted') this.$buefy.toast.open(name + ' deleted')
this.getModels() this.models = await api.getUserModels()
} catch (error) {
this.$buefy.toast.open('Delete error ' + error)
} }
})
} }
} }
} }
......
...@@ -116,6 +116,8 @@ export default { ...@@ -116,6 +116,8 @@ export default {
shortDescription: this.model.shortDescription, shortDescription: this.model.shortDescription,
longDescription: this.model.longDescription, longDescription: this.model.longDescription,
performance: this.model.performance, performance: this.model.performance,
performanceUnit: this.model.performanceUnit,
parameterCount: this.model.parameterCount,
tags: this.model.tags tags: this.model.tags
}) })
}) })
......
...@@ -59,7 +59,7 @@ export default { ...@@ -59,7 +59,7 @@ export default {
}, },
async mounted () { async mounted () {
this.account = await api.getUser() this.account = await api.getUser()
this.models = await api.getModels() this.models = await api.getUserModels()
await localStorage.setItem('user_role', this.account.role) await localStorage.setItem('user_role', this.account.role)
await localStorage.setItem('user_id', this.account.id) await localStorage.setItem('user_id', this.account.id)
}, },
......
...@@ -80,6 +80,25 @@ ...@@ -80,6 +80,25 @@
</b-tab-item> </b-tab-item>
<b-tab-item label="Comments"> <b-tab-item label="Comments">
<b-table :data="commentList" :loading="isLoading" striped hoverable>
<template slot-scope="props">
<b-table-column field="content" label="Content">
{{ props.row.content }}
</b-table-column>
<b-table-column field="username" label="Username" searchable sortable>
{{ props.row.author.username }}
</b-table-column>
<b-table-column field="added" label="Added" sortable>
{{ props.row.added }}
</b-table-column>
<b-table-column label="" centered>
<b-button class="actionButton" icon-left="delete" type="is-danger" @click="removePrompt(props.row.id, props.row.content, 'comment')" outlined/>
</b-table-column>
</template>
</b-table>
</b-tab-item> </b-tab-item>
</b-tabs> </b-tabs>
</div> </div>
...@@ -96,13 +115,15 @@ export default { ...@@ -96,13 +115,15 @@ export default {
isLoading: true, isLoading: true,
userList: [], userList: [],
modelList: [], modelList: [],
tagList: [] tagList: [],
commentList: []
} }
}, },
async mounted () { async mounted () {
this.modelList = await api.getModelList() this.modelList = await api.getModelList()
this.userList = await api.getUserList() this.userList = await api.getUserList()
this.tagList = await api.getTags() this.tagList = await api.getTags()
this.commentList = await api.getCommentList()
this.isLoading = false this.isLoading = false
}, },
methods: { methods: {
...@@ -119,11 +140,13 @@ export default { ...@@ -119,11 +140,13 @@ export default {
case 'model' : api.removeModel(id); break case 'model' : api.removeModel(id); break
case 'tag' : api.removeTag(id); break case 'tag' : api.removeTag(id); break
case 'category' : api.removeCategory(id); break case 'category' : api.removeCategory(id); break
case 'comment' : api.removeComment(id); break
} }
this.$buefy.toast.open(name + ' deleted') this.$buefy.toast.open(name + ' deleted')
this.modelList = await api.getModelList() this.modelList = await api.getModelList()
this.userList = await api.getUserList() this.userList = await api.getUserList()
this.tagList = await api.getTags() this.tagList = await api.getTags()
this.commentList = await api.getCommentList()
} }
}) })
}, },
......
...@@ -56,7 +56,7 @@ ...@@ -56,7 +56,7 @@
<b-tab-item label="Comments"> <b-tab-item label="Comments">
<div class="box"> <div class="box">
<Comments v-bind:comments="model.comments"/> <Comments v-if="!isLoading" v-bind:modelId="model.id"/>
</div> </div>
</b-tab-item> </b-tab-item>
</b-tabs> </b-tabs>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment