Advertisement
de-su

Untitled

Mar 18th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @Component({
  2.   selector: 'app-comment',
  3.   templateUrl: './comment.component.html',
  4.   changeDetection: ChangeDetectionStrategy.OnPush,
  5.   styleUrls: ['./comment.component.css']
  6. })
  7.  
  8. export class CommentComponent implements OnInit {
  9.   @Input() comments: Comment[];
  10.   @Input() users: User[];
  11.   comment: Comment;
  12.  
  13.   constructor(private commentService: CommentService, private alertify: AlertifyService, private router: Router ) {
  14.    }
  15.  
  16.   ngOnInit() {
  17.     this.comment = {
  18.       id: 0,
  19.       userId: 0,
  20.       ticketId: 0,
  21.       content: ''
  22.     };
  23.     console.log(this.comments);
  24.     console.log(this.users);
  25.   }
  26.  
  27.   selectUser(id: number): User {
  28.     return this.users.find(user => user.id === id);
  29.   }
  30.  
  31.   addComment() {
  32.     this.commentService.addComment(this.comment).subscribe((data: Comment) => {
  33.  
  34.       this.alertify.success('Comment has been added');
  35.       this.comments.push(data);
  36.       //console.log(data);
  37.       //this.router.navigate['tickets/' + data.ticketId]
  38.       //location.reload();
  39.  
  40.     }, error => {
  41.       this.alertify.error(error);
  42.     });
  43.   }
  44.  
  45. }
  46.  
  47.  
  48. <form class="mb-2" #addCommentForm="ngForm" (ngSubmit)="addComment()">
  49.     <textarea class="form-control form-control-sm" type="text" placeholder="Type some comment!" name="content" [(ngModel)]="comment.content"></textarea>
  50.     <button type="submit" class="btn btn-success btn-sm offset-sm-10 col-sm-2">Add</button>
  51. </form>
  52.  
  53. <div *ngFor="let comment of comments | reverse" class="card mb-2">
  54.   <div class="card-body pb-1">
  55.     <h6 class="card-subtitle mb-2"><a [routerLink]="['/members/', comment.userId]">{{selectUser(comment.userId).username}}</a></h6>
  56.     <h6 class="card-subtitle mb-2 text-muted">{{comment.dateCreated | date: 'd MMMM yyyy HH:mm'}}</h6>
  57.     <p class="card-text mb-1">{{comment.content}}</p>
  58.     <a href="#" class="card-link mb-1 text-danger">Delete</a>
  59.   </div>
  60. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement