From d70c2829124fef3257c8dfeeb4c9b5db0368ec0e Mon Sep 17 00:00:00 2001 From: myml Date: Fri, 28 Dec 2018 10:54:03 +0800 Subject: [PATCH] fix: update comment Change-Id: Idf2bbc9c1e2eb4a0c3b89c6f12cf39bb3fda319b --- .../components/edit/edit.component.html | 3 +++ .../components/edit/edit.component.scss | 5 ++++ .../components/edit/edit.component.ts | 27 +++++++++++++------ .../my-comments/services/comments.service.ts | 23 ++++++++++------ 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/web/src/app/modules/my-comments/components/edit/edit.component.html b/src/web/src/app/modules/my-comments/components/edit/edit.component.html index b338fbdd..5d3f661c 100644 --- a/src/web/src/app/modules/my-comments/components/edit/edit.component.html +++ b/src/web/src/app/modules/my-comments/components/edit/edit.component.html @@ -27,6 +27,9 @@
+ + Failed to submit +  
diff --git a/src/web/src/app/modules/my-comments/components/edit/edit.component.scss b/src/web/src/app/modules/my-comments/components/edit/edit.component.scss index f4a2eaf5..cc7e4aee 100644 --- a/src/web/src/app/modules/my-comments/components/edit/edit.component.scss +++ b/src/web/src/app/modules/my-comments/components/edit/edit.component.scss @@ -55,3 +55,8 @@ width: 400px; height: 300px; } + +.error { + color: red; + margin: 1rem; +} diff --git a/src/web/src/app/modules/my-comments/components/edit/edit.component.ts b/src/web/src/app/modules/my-comments/components/edit/edit.component.ts index 94dfb847..3c38f1d6 100644 --- a/src/web/src/app/modules/my-comments/components/edit/edit.component.ts +++ b/src/web/src/app/modules/my-comments/components/edit/edit.component.ts @@ -25,6 +25,7 @@ export class EditComponent implements OnInit { content: string; rate: number; version: string; + error: boolean; _comment: UserComment; @Output() close = new EventEmitter(); @@ -39,7 +40,10 @@ export class EditComponent implements OnInit { this.version = c.version; this.app$ = this.appService.getApp(c.appName, false, false); } - constructor(private commentService: CommentsService, private appService: AppService) {} + constructor( + private commentService: CommentsService, + private appService: AppService, + ) {} ngOnInit() { this.dialogRef.nativeElement.addEventListener('close', () => { @@ -50,13 +54,20 @@ export class EditComponent implements OnInit { this.close.emit(changed); } submit() { - this.commentService.delete(this._comment.id).subscribe(() => { - this.commentService - .create(this._comment.appName, this.content, this.rate * 2, this._comment.version) - .subscribe(() => { - this.closed(true); - }); - }); + this.commentService + .update(this._comment.id, { + appName: this._comment.appName, + content: this.content, + rate: this.rate * 2, + version: this._comment.version, + }) + .toPromise() + .then(() => { + this.closed(); + }) + .catch(() => { + this.error = true; + }); } delete() { this.commentService.delete(this._comment.id).subscribe(() => { diff --git a/src/web/src/app/modules/my-comments/services/comments.service.ts b/src/web/src/app/modules/my-comments/services/comments.service.ts index 57dc3e17..61e5aeea 100644 --- a/src/web/src/app/modules/my-comments/services/comments.service.ts +++ b/src/web/src/app/modules/my-comments/services/comments.service.ts @@ -37,18 +37,18 @@ export class CommentsService { ), ); } - create(appName: string, content: string, rate: number, version: string) { - const c = { - appName, - content, - rate, - version, - }; - return this.http.post(this.server + `/api/user/comment/app/${appName}`, c); + create(c: CommentRequest) { + return this.http.post( + this.server + `/api/user/comment/app/${c.appName}`, + c, + ); } delete(id: number) { return this.http.delete(this.server + `/api/user/my/comment/${id}`); } + update(id: number, c: CommentRequest) { + return this.http.patch(this.server + `/api/user/my/comment/${id}`, c); + } } interface Result { @@ -56,6 +56,13 @@ interface Result { totalCount: number; } +interface CommentRequest { + appName: string; + content: string; + rate: number; + version: string; +} + export interface UserComment { id: number; appName: string;