Skip to content
Snippets Groups Projects
Commit 1fb4e922 authored by Tom Käsler's avatar Tom Käsler
Browse files

Merge branch 'refactor-comment-list' into 'master'

Refactor comment list

Closes #253

See merge request arsnova/arsnova-lite!207
parents 3cea62b2 7bc4d26e
No related merge requests found
<div fxLayout="row" id="search-container" *ngIf="!isLoading"> <div fxLayout="row" id="search-container">
<mat-label fxLayoutAlign="center center"> <mat-label fxLayoutAlign="center center">
<mat-icon class="search-icon">search</mat-icon> <mat-icon class="search-icon">search</mat-icon>
</mat-label> </mat-label>
...@@ -29,45 +29,47 @@ ...@@ -29,45 +29,47 @@
</div> </div>
<mat-menu #sortMenu="matMenu" xPosition="before"> <mat-menu #sortMenu="matMenu" xPosition="before">
<button mat-icon-button (focus)="hideCommentsList=false" (click)="sort(votedesc)"> <button mat-icon-button (focus)="hideCommentsList=false" (click)="sortComments(votedesc)">
<mat-icon>arrow_upwards</mat-icon> <mat-icon>arrow_upwards</mat-icon>
</button> </button>
<button mat-icon-button (focus)="hideCommentsList=false" (click)="sort(voteasc)"> <button mat-icon-button (focus)="hideCommentsList=false" (click)="sortComments(voteasc)">
<mat-icon>arrow_downwards</mat-icon> <mat-icon>arrow_downwards</mat-icon>
</button> </button>
<button mat-icon-button (focus)="hideCommentsList=false" (click)="sort(currentSort === timedesc ? timeasc : timedesc)"> <button mat-icon-button (focus)="hideCommentsList=false" (click)="sortComments(currentSort === timedesc ? timeasc : timedesc)">
<mat-icon>access_time</mat-icon> <mat-icon>access_time</mat-icon>
</button> </button>
</mat-menu> </mat-menu>
<mat-menu #filterMenu="matMenu" xPosition="before"> <mat-menu #filterMenu="matMenu" xPosition="before">
<div> <div>
<button mat-icon-button (focus)="hideCommentsList=true" (click)="filter(correct)"> <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterComments(correct)">
<mat-icon [ngClass]="{correct: 'correct-icon'}[currentFilter]">check_circle</mat-icon> <mat-icon [ngClass]="{correct: 'correct-icon'}[currentFilter]">check_circle</mat-icon>
</button> </button>
<button mat-icon-button (focus)="hideCommentsList=true" (click)="filter(favorite)"> <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterComments(favorite)">
<mat-icon [ngClass]="{favorite: 'favorite-icon'}[currentFilter]">favorite</mat-icon> <mat-icon [ngClass]="{favorite: 'favorite-icon'}[currentFilter]">favorite</mat-icon>
</button> </button>
<button mat-icon-button (focus)="hideCommentsList=true" (click)="filter(read)"> <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterComments(read)">
<mat-icon [ngClass]="{read: 'read-icon'}[currentFilter]">visibility</mat-icon> <mat-icon [ngClass]="{read: 'read-icon'}[currentFilter]">visibility</mat-icon>
</button> </button>
<button mat-icon-button (focus)="hideCommentsList=true" (click)="filter(unread)"> <button mat-icon-button (focus)="hideCommentsList=true" (click)="filterComments(unread)">
<mat-icon [ngClass]="{unread: 'unread-icon'}[currentFilter]">visibility_off</mat-icon> <mat-icon [ngClass]="{unread: 'unread-icon'}[currentFilter]">visibility_off</mat-icon>
</button> </button>
<button mat-icon-button (focus)="hideCommentsList=false" (click)="sort(currentSort)"> <button mat-icon-button (focus)="hideCommentsList=false" (click)="sortComments(currentSort)">
<mat-icon>close</mat-icon> <mat-icon>close</mat-icon>
</button> </button>
</div> </div>
</mat-menu> </mat-menu>
</div> </div>
<app-comment *ngFor="let current of showComments()" [comment]="current" [parseVote]="getVote(current)"></app-comment> <div *ngIf="!isLoading">
<app-comment *ngFor="let current of hideCommentsList ? filteredComments : comments" [comment]="current" [parseVote]="getVote(current)"></app-comment>
</div>
<div *ngIf="comments.length < 1" fxLayout="row" fxLayoutAlign="center center" class="no-comments"> <div *ngIf="comments.length < 1" fxLayout="row" fxLayoutAlign="center center" class="no-comments">
<h4>{{ 'comment-page.no-comments' | translate }}</h4> <h4>{{ 'comment-page.no-comments' | translate }}</h4>
......
...@@ -10,7 +10,6 @@ import { WsCommentServiceService } from '../../../services/websockets/ws-comment ...@@ -10,7 +10,6 @@ import { WsCommentServiceService } from '../../../services/websockets/ws-comment
import { User } from '../../../models/user'; import { User } from '../../../models/user';
import { Vote } from '../../../models/vote'; import { Vote } from '../../../models/vote';
import { UserRole } from '../../../models/user-roles.enum'; import { UserRole } from '../../../models/user-roles.enum';
import { AuthenticationService } from '../../../services/http/authentication.service';
import { Room } from '../../../models/room'; import { Room } from '../../../models/room';
import { RoomService } from '../../../services/http/room.service'; import { RoomService } from '../../../services/http/room.service';
import { VoteService } from '../../../services/http/vote.service'; import { VoteService } from '../../../services/http/vote.service';
...@@ -23,7 +22,7 @@ import { VoteService } from '../../../services/http/vote.service'; ...@@ -23,7 +22,7 @@ import { VoteService } from '../../../services/http/vote.service';
export class CommentListComponent implements OnInit { export class CommentListComponent implements OnInit {
@Input() user: User; @Input() user: User;
@Input() roomId: string; @Input() roomId: string;
@Input() comments: Comment[]; comments: Comment[];
room: Room; room: Room;
hideCommentsList = false; hideCommentsList = false;
filteredComments: Comment[]; filteredComments: Comment[];
...@@ -46,7 +45,6 @@ export class CommentListComponent implements OnInit { ...@@ -46,7 +45,6 @@ export class CommentListComponent implements OnInit {
private translateService: TranslateService, private translateService: TranslateService,
public dialog: MatDialog, public dialog: MatDialog,
protected langService: LanguageService, protected langService: LanguageService,
private authenticationService: AuthenticationService,
private wsCommentService: WsCommentServiceService, private wsCommentService: WsCommentServiceService,
protected roomService: RoomService, protected roomService: RoomService,
protected voteService: VoteService protected voteService: VoteService
...@@ -56,20 +54,27 @@ export class CommentListComponent implements OnInit { ...@@ -56,20 +54,27 @@ export class CommentListComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.roomId = localStorage.getItem(`roomId`); this.roomId = localStorage.getItem(`roomId`);
const userId = this.authenticationService.getUser().id; const userId = this.user.id;
this.voteService.getByRoomIdAndUserID(this.roomId, userId).subscribe(votes => { this.userRole = this.user.role;
for (const v of votes) {
this.commentVoteMap.set(v.commentId, v);
}
});
this.roomService.getRoom(this.roomId).subscribe( room => this.room = room); this.roomService.getRoom(this.roomId).subscribe( room => this.room = room);
this.hideCommentsList = false; this.hideCommentsList = false;
this.wsCommentService.getCommentStream(this.roomId).subscribe((message: Message) => { this.wsCommentService.getCommentStream(this.roomId).subscribe((message: Message) => {
this.parseIncomingMessage(message); this.parseIncomingMessage(message);
}); });
this.translateService.use(localStorage.getItem('currentLang')); this.translateService.use(localStorage.getItem('currentLang'));
this.userRole = this.authenticationService.getRole();
this.deviceType = localStorage.getItem('deviceType'); this.deviceType = localStorage.getItem('deviceType');
if (this.userRole === 0) {
this.voteService.getByRoomIdAndUserID(this.roomId, userId).subscribe(votes => {
for (const v of votes) {
this.commentVoteMap.set(v.commentId, v);
}
});
}
this.commentService.getComments(this.roomId)
.subscribe(comments => {
this.comments = comments;
this.getComments();
});
} }
searchComments(term: string): void { searchComments(term: string): void {
...@@ -81,29 +86,24 @@ export class CommentListComponent implements OnInit { ...@@ -81,29 +86,24 @@ export class CommentListComponent implements OnInit {
} }
} }
showComments(): Comment[] { getComments(): void {
this.isLoading = false; this.isLoading = false;
let commentThreshold = -10; let commentThreshold = -10;
if (this.room.extensions && this.room.extensions['comments']) { if (this.room.extensions && this.room.extensions['comments']) {
commentThreshold = this.room.extensions['comments'].commentThreshold; commentThreshold = this.room.extensions['comments'].commentThreshold;
if (this.hideCommentsList) { if (this.hideCommentsList) {
return this.filteredComments.filter( x => x.score >= commentThreshold ); this.filteredComments = this.filteredComments.filter( x => x.score >= commentThreshold );
} else {
this.sort(this.currentSort);
return this.comments.filter( x => x.score >= commentThreshold );
}
} else {
if (this.hideCommentsList) {
return this.filteredComments;
} else { } else {
this.sort(this.currentSort); this.comments = this.comments.filter( x => x.score >= commentThreshold );
return this.comments;
} }
} }
this.sortComments(this.currentSort);
} }
getVote(comment: Comment): Vote { getVote(comment: Comment): Vote {
return this.commentVoteMap.get(comment.id); if (this.userRole === 0) {
return this.commentVoteMap.get(comment.id);
}
} }
parseIncomingMessage(message: Message) { parseIncomingMessage(message: Message) {
...@@ -157,7 +157,8 @@ export class CommentListComponent implements OnInit { ...@@ -157,7 +157,8 @@ export class CommentListComponent implements OnInit {
} }
break; break;
} }
this.filter(this.currentFilter); this.filterComments(this.currentFilter);
this.sortComments(this.currentSort);
} }
openCreateDialog(): void { openCreateDialog(): void {
...@@ -180,64 +181,29 @@ export class CommentListComponent implements OnInit { ...@@ -180,64 +181,29 @@ export class CommentListComponent implements OnInit {
this.wsCommentService.add(comment); this.wsCommentService.add(comment);
} }
filterFavorite(): void { filterComments(type: string): void {
this.filteredComments = this.comments.filter(c => c.favorite);
}
filterRead(): void {
this.filteredComments = this.comments.filter(c => c.read);
}
filterUnread(): void {
this.filteredComments = this.comments.filter(c => !c.read);
}
filterCorrect(): void {
this.filteredComments = this.comments.filter(c => c.correct);
}
filter(type: string): void {
this.currentFilter = type; this.currentFilter = type;
switch (type) { this.filteredComments = this.comments.filter(c => {
case this.correct: switch (type) {
this.filterCorrect(); case this.correct:
break; return c.correct;
case this.favorite: case this.favorite:
this.filterFavorite(); return c.favorite;
break; case this.read:
case this.read: return c.read;
this.filterRead(); case this.unread:
break; return !c.read;
case this.unread: }
this.filterUnread();
}
}
sort(type: string): void {
this.currentSort = type;
if (type === this.voteasc) {
this.sortVote();
} else if (type === this.votedesc) {
this.sortVoteDesc();
} else {
this.sortTime(type);
}
}
sortVote(): void {
this.comments.sort((a, b) => {
return a.score - b.score;
});
}
sortVoteDesc(): void {
this.comments.sort((a, b) => {
return b.score - a.score;
}); });
} }
sortTime(type: string): void { sortComments(type: string): void {
this.comments.sort((a, b) => { this.comments.sort((a, b) => {
if (type === this.voteasc) {
return a.score - b.score;
} else if (type === this.votedesc) {
return b.score - a.score;
}
const dateA = new Date(a.timestamp), dateB = new Date(b.timestamp); const dateA = new Date(a.timestamp), dateB = new Date(b.timestamp);
if (type === this.timedesc) { if (type === this.timedesc) {
return +dateA - +dateB; return +dateA - +dateB;
...@@ -245,5 +211,6 @@ export class CommentListComponent implements OnInit { ...@@ -245,5 +211,6 @@ export class CommentListComponent implements OnInit {
return +dateB - +dateA; return +dateB - +dateA;
} }
}); });
this.currentSort = type;
} }
} }
<div *ngIf="isLoading" fxLayout="column" fxLayoutAlign="center" fxFill>
<div fxLayout="row" fxLayoutAlign="center">
<mat-progress-spinner color="primary" mode="indeterminate"></mat-progress-spinner>
</div>
</div>
<div fxLayout="column" fxLayoutAlign="center" fxLayoutGap="20px"> <div fxLayout="column" fxLayoutAlign="center" fxLayoutGap="20px">
<div fxLayout="row" fxLayoutAlign="center"> <div fxLayout="row" fxLayoutAlign="center">
<app-comment-list [user]="user" [roomId]="roomId" [comments]="comments"></app-comment-list> <app-comment-list [user]="user" [roomId]="roomId" comments></app-comment-list>
</div> </div>
</div> </div>
...@@ -3,8 +3,6 @@ import { ActivatedRoute } from '@angular/router'; ...@@ -3,8 +3,6 @@ import { ActivatedRoute } from '@angular/router';
import { User } from '../../../models/user'; import { User } from '../../../models/user';
import { NotificationService } from '../../../services/util/notification.service'; import { NotificationService } from '../../../services/util/notification.service';
import { AuthenticationService } from '../../../services/http/authentication.service'; import { AuthenticationService } from '../../../services/http/authentication.service';
import { CommentService } from '../../../services/http/comment.service';
import { Comment } from '../../../models/comment';
@Component({ @Component({
selector: 'app-comment-page', selector: 'app-comment-page',
...@@ -14,25 +12,14 @@ import { Comment } from '../../../models/comment'; ...@@ -14,25 +12,14 @@ import { Comment } from '../../../models/comment';
export class CommentPageComponent implements OnInit { export class CommentPageComponent implements OnInit {
roomId: string; roomId: string;
user: User; user: User;
isLoading = true;
comments: Comment[];
constructor(private route: ActivatedRoute, constructor(private route: ActivatedRoute,
private notification: NotificationService, private notification: NotificationService,
private authenticationService: AuthenticationService, private authenticationService: AuthenticationService) { }
private commentService: CommentService) { }
ngOnInit(): void { ngOnInit(): void {
this.roomId = localStorage.getItem('roomId'); this.roomId = localStorage.getItem('roomId');
this.user = this.authenticationService.getUser(); this.user = this.authenticationService.getUser();
this.getComments();
} }
getComments(): void {
this.commentService.getComments(this.roomId)
.subscribe(comments => {
this.comments = comments;
this.isLoading = false;
});
}
} }
...@@ -13,15 +13,15 @@ ...@@ -13,15 +13,15 @@
</div> </div>
<button mat-icon-button [disabled]="isStudent" (click)="setCorrect(comment)" <button mat-icon-button [disabled]="isStudent" (click)="setCorrect(comment)"
matTooltip="{{ 'comment-page.correct' | translate }}"> matTooltip="{{ 'comment-page.correct' | translate }}">
<mat-icon [ngClass]="{true : 'correct-icon', false: 'not-marked'}[comment.correct]">check_circle</mat-icon> <mat-icon [ngClass]="{'correct-icon' : comment.correct, 'not-marked' : !comment.correct}">check_circle</mat-icon>
</button> </button>
<button mat-icon-button [disabled]="isStudent" (click)="setFavorite(comment)" <button mat-icon-button [disabled]="isStudent" (click)="setFavorite(comment)"
matTooltip="{{ 'comment-page.favorite' | translate }}"> matTooltip="{{ 'comment-page.favorite' | translate }}">
<mat-icon [ngClass]="{true: 'favorite-icon', false: 'not-marked'}[comment.favorite]">favorite</mat-icon> <mat-icon [ngClass]="{'favorite-icon' : comment.favorite, 'not-marked' : !comment.favorite}">favorite</mat-icon>
</button> </button>
<button mat-icon-button [disabled]="isStudent" (click)="setRead(comment)" <button mat-icon-button [disabled]="isStudent" (click)="setRead(comment)"
matTooltip="{{ 'comment-page.read' | translate }}"> matTooltip="{{ 'comment-page.read' | translate }}">
<mat-icon class="icon" [ngClass]="{'read-icon': comment.read, 'not-marked' : !comment.read}">visibility <mat-icon [ngClass]="{'read-icon': comment.read, 'not-marked' : !comment.read}">visibility
</mat-icon> </mat-icon>
</button> </button>
</div> </div>
......
...@@ -41,7 +41,6 @@ export const rubberBand = [ ...@@ -41,7 +41,6 @@ export const rubberBand = [
export class CommentComponent implements OnInit { export class CommentComponent implements OnInit {
@Input() comment: Comment; @Input() comment: Comment;
isStudent = false; isStudent = false;
isLoading = true;
hasVoted = 0; hasVoted = 0;
language: string; language: string;
animationState: string; animationState: string;
...@@ -77,7 +76,9 @@ export class CommentComponent implements OnInit { ...@@ -77,7 +76,9 @@ export class CommentComponent implements OnInit {
@Input() @Input()
set parseVote(vote: Vote) { set parseVote(vote: Vote) {
this.hasVoted = vote.vote; if (this.isStudent) {
this.hasVoted = vote.vote;
}
} }
resetAnimationState(): void { resetAnimationState(): void {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment