Browse Source

frontend comment service & state handler

pull/1195/head
Paul 3 years ago
parent
commit
0fd5a01ee1
5 changed files with 137 additions and 44 deletions
  1. 7
      frontend/services/comment.service.js
  2. 3
      frontend/services/document.service.js
  3. 115
      frontend/store/comments.js
  4. 44
      frontend/store/documents.js
  5. 12
      frontend/store/projects.js

7
frontend/services/comment.service.js

@ -5,12 +5,15 @@ class CommentService {
this.request = ApiService
}
getCommentList({ projectId, docId }) {
getCommentList(projectId, docId) {
return this.request.get(`/projects/${projectId}/docs/${docId}/comments`)
}
getProjectCommentList(projectId) {
return this.request.get(`/projects/${projectId}/comments`)
}
addComment(projectId, docId, payload) {
console.log(payload)
return this.request.post(`/projects/${projectId}/docs/${docId}/comments`, payload)
}

3
frontend/services/document.service.js

@ -34,6 +34,9 @@ class DocumentService {
if (format === 'csv') {
headers.Accept = 'text/csv; charset=utf-8'
headers['Content-Type'] = 'text/csv; charset=utf-8'
} else if (format === 'txt') {
headers.Accept = 'text/plain; charset=utf-8'
headers['Content-Type'] = 'text/plain; charset=utf-8'
} else {
headers.Accept = 'application/json'
headers['Content-Type'] = 'application/json'

115
frontend/store/comments.js

@ -0,0 +1,115 @@
import CommentService from '@/services/comment.service'
import UserService from '@/services/user.service'
export const state = () => ({
comments: [],
selectedComments: [],
totalComments: 0,
userId: -1,
loading: false
})
export const getters = {
isCommentSelected(state) {
return state.selectedComments.length > 0
}
}
export const mutations = {
setCommentList(state, payload) {
state.comments = payload
},
addComment(state, payload) {
state.comments.unshift(payload)
},
updateComment(state, payload) {
const item = state.comments.find(item => item.id === payload.id)
Object.assign(item, payload)
},
deleteComment(state, commentId) {
state.comments = state.comments.filter(item => item.id !== commentId)
},
updateSelectedComments(state, selected) {
state.selectedComments = selected
},
resetSelectedComments(state) {
state.selectedComments = []
},
setTotalComments(state, payload) {
state.totalComments = payload
},
setUserId(state, payload) {
state.userId = payload.id
},
setLoading(state, payload) {
state.loading = payload
}
}
export const actions = {
getCommentList({ commit, state }, payload) {
commit('setLoading', true)
return CommentService.getCommentList(payload.projectId, payload.docId)
.then((response) => {
commit('setCommentList', response.data)
})
.catch((error) => {
alert(error)
})
.finally(() => {
commit('setLoading', false)
})
},
getProjectCommentList({ commit, state }, payload) {
commit('setLoading', true)
return CommentService.getProjectCommentList(payload.projectId)
.then((response) => {
commit('setCommentList', response.data)
commit('setTotalComments', response.data.count)
})
.catch((error) => {
alert(error)
})
.finally(() => {
commit('setLoading', false)
})
},
addComment({ commit, state }, payload) {
CommentService.addComment(payload.projectId, payload.docId, payload)
.then((response) => {
commit('addComment', response.data)
})
.catch((error) => {
alert(error)
})
},
updateComment({ commit, state }, payload) {
CommentService.updateComment(payload.projectId, payload.docId, payload.commentId, payload)
.then((response) => {
commit('updateComment', response.data)
})
.catch((error) => {
alert(error)
})
},
deleteComment({ commit, state }, payload) {
for (const comment of state.selectedComments) {
CommentService.deleteComment(payload.projectId, comment.document, comment.id)
.then((response) => {
commit('deleteComment', comment.id)
})
.catch((error) => {
alert(error)
})
commit('resetSelectedComments')
}
},
getMyUserId({ commit, state }) {
UserService.getMe()
.then((response) => {
commit('setUserId', response.data)
})
.catch((error) => {
alert(error)
})
}
}

44
frontend/store/documents.js

@ -1,4 +1,3 @@
import CommentService from '@/services/comment.service'
import DocumentService from '@/services/document.service'
import AnnotationService from '@/services/annotation.service'
@ -74,16 +73,6 @@ export const mutations = {
const item = state.items[state.current].annotations.find(item => item.id === payload.id)
Object.assign(item, payload)
},
addComment(state, payload) {
state.items[state.current].comments.push(payload)
},
updateComment(state, payload) {
const item = state.items[state.current].comments.find(item => item.id === payload.id)
Object.assign(item, payload)
},
deleteComment(state, commentId) {
state.items[state.current].comments = state.items[state.current].comments.filter(item => item.id !== commentId)
},
updateSearchOptions(state, payload) {
state.searchOptions = Object.assign(state.searchOptions, payload)
},
@ -95,6 +84,9 @@ export const mutations = {
isChecked: '',
filterName: ''
}
},
setUserId(state, payload) {
state.userId = payload.id
}
}
@ -233,35 +225,5 @@ export const actions = {
.catch((error) => {
alert(error)
})
},
addComment({ commit, state }, payload) {
const documentId = state.items[state.current].id
CommentService.addComment(payload.projectId, documentId, payload)
.then((response) => {
commit('addComment', response.data)
})
.catch((error) => {
alert(error)
})
},
updateComment({ commit, state }, payload) {
const documentId = state.items[state.current].id
CommentService.updateComment(payload.projectId, documentId, payload.commentId, payload)
.then((response) => {
commit('updateComment', response.data)
})
.catch((error) => {
alert(error)
})
},
deleteComment({ commit, state }, payload) {
const documentId = state.items[state.current].id
CommentService.deleteComment(payload.projectId, documentId, payload.commentId)
.then((response) => {
commit('deleteComment', payload.commentId)
})
.catch((error) => {
alert(error)
})
}
}

12
frontend/store/projects.js

@ -180,6 +180,11 @@ export const getters = {
text: 'JSONL(Text label)',
suffix: 'jsonl'
}
const fastText = {
type: 'txt',
text: 'FastText',
suffix: 'txt'
}
if (state.current.project_type === 'DocumentClassification') {
json.examples = [
'{"id": 1, "text": "Terrible customer service.", "annotations": [{"id": 1, "label": 1, "user": 1}]}\n',
@ -192,9 +197,14 @@ export const getters = {
'2,"Really great transaction.",2,1\n',
'3,"Great price.",2,1'
]
fastText.examples = [
'__label__pet dog cat \n',
'__label__car VW BMW'
]
return [
csv,
json
json,
fastText
]
} else if (state.current.project_type === 'SequenceLabeling') {
json.examples = [

Loading…
Cancel
Save