Refactor comment UI code

And run prettier
This commit is contained in:
Michael Shamoon
2022-08-07 17:33:13 -07:00
parent 6d956ac13b
commit 5c1e09cc48
7 changed files with 85 additions and 75 deletions

View File

@@ -0,0 +1,73 @@
import { Component, Input, OnInit } from '@angular/core'
import { DocumentCommentsService } from 'src/app/services/rest/document-comments.service'
import { PaperlessDocumentComment } from 'src/app/data/paperless-document-comment'
import { FormControl, FormGroup } from '@angular/forms'
import { first } from 'rxjs/operators'
import { ToastService } from 'src/app/services/toast.service'
@Component({
selector: 'app-document-comments',
templateUrl: './document-comments.component.html',
styleUrls: ['./document-comments.component.scss'],
})
export class DocumentCommentsComponent implements OnInit {
commentForm: FormGroup = new FormGroup({
newcomment: new FormControl(''),
})
networkActive = false
comments: PaperlessDocumentComment[] = []
@Input()
documentId: number
constructor(
private commentsService: DocumentCommentsService,
private toastService: ToastService
) {}
ngOnInit(): void {
this.commentsService
.getComments(this.documentId)
.pipe(first())
.subscribe((comments) => (this.comments = comments))
}
commentId(index, comment: PaperlessDocumentComment) {
return comment.id
}
addComment() {
this.networkActive = true
this.commentsService
.addComment(this.documentId, this.commentForm.get('newcomment').value)
.subscribe({
next: (result) => {
this.comments = result
this.commentForm.get('newcomment').reset()
this.networkActive = false
},
error: (e) => {
this.networkActive = false
this.toastService.showError(
$localize`Error saving comment: ${e.toString()}`
)
},
})
}
deleteComment(commentId: number) {
this.commentsService.deleteComment(this.documentId, commentId).subscribe({
next: (result) => {
this.comments = result
this.networkActive = false
},
error: (e) => {
this.networkActive = false
this.toastService.showError(
$localize`Error deleting comment: ${e.toString()}`
)
},
})
}
}