Skip to content

Instantly share code, notes, and snippets.

@ryanmenzer
Last active August 10, 2017 18:50
Show Gist options
  • Select an option

  • Save ryanmenzer/9944e58c461019c1de1a259e1486784a to your computer and use it in GitHub Desktop.

Select an option

Save ryanmenzer/9944e58c461019c1de1a259e1486784a to your computer and use it in GitHub Desktop.
A shared Angular component (in progress) for displaying supervisors' comments
import {Component, Input, OnInit} from '@angular/core';
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import {User, Profile, Comment} from './models/';
import {ProfileService, AuthService, EmailService, UserService} from '../shared/services/';
@Component({
selector: 'comment',
templateUrl: './views/comment.html',
styleUrls: ['./styles/shared.scss'],
})
export class CommentComponent implements OnInit {
@Input() profile: Profile;
@Input() type: string;
user: User;
currentUser: any;
comments: any;
comment: any;
showCommentAdd: boolean = false;
showCommentEdit: boolean = false;
close: any = [];
saveInterval: any;
autosaveRunning: boolean = false;
showSaved: boolean = false;
constructor(
private _profileService: ProfileService,
private _emailService: EmailService,
private _userService: UserService,
private _router: Router,
private _route: ActivatedRoute) {
const admin = new RegExp('(\/coordinator\/)\\w+');
const review = new RegExp('(\/review\/)\\w+');
const location = new RegExp('(\/location\/)\\w+');
_router.events.subscribe((event:NavigationEnd) => {
if(event.url.match(admin)){
this.close = ['/coordinator'];
}
else if(event.url.match(review)){
this.close = ['/review'];
}
else if(event.url.match(location)) {
let url = event.url.match(/location\/(\d+)/);
this.close = ['/location', url[1] ];
}
});
// console.log("ARRAY= " + close);
this._userService.currentUser.subscribe(user => {
this.user = user;
});
}
ngOnInit() {
this.comments = this.profile.comments.filter(comment => {
comment.type == this.type
});
if(this.profile.status == 'mtl') { // || (this.profile.status == 'assigned' && (!this.comment || this.comment == ''))) {
this.showCommentAdd = true;
}
}
addComment() {
this.showCommentAdd = true;
}
/** Updates the profile MTL Comment. When submitted, changes profile status to coordinator
*/
saveComment(submit?:boolean){
if(submit) {
//Stop saving interval
clearInterval(this.saveInterval);
this.autosaveRunning = false;
//Variable to track if this mtlComment was an edit or a first submit
let wasEdit = false;
if(this.profile.status == "mtl") {
this.profile.status = "coordinator";
} else {
wasEdit = true;
}
// this.showMTLEdit = false;
this._profileService.saveComment(this.comment);
if(wasEdit) {
this._emailService.sendEmail(this.profile.id, "mtlCommentEdit", this.comment);
} else {
this._emailService.sendEmail(this.profile.id, "awaitingAssignment", this.comment);
}
} else {
//Only save if the comment has changed and it is not empty
if(this.comment && this.comment != "" && this.comment.id) {
// this.profile.comments.push(newComment);
this._profileService.saveComment(this.comment);
//Show "Autosaved" alert, then hide after a delay
this.showSaved = true;
setTimeout(()=>{
this.showSaved = false;
}, 10000);
}
}
}
ngOnDestroy() {
clearInterval(this.saveInterval);
}
/** Starts the autosave interval timer only if it has not yet been started
*/
startAutosave() {
if(!this.autosaveRunning) {
this.autosaveRunning = true;
this.saveInterval = setInterval(() => {
this.saveComment();
}, 20000);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment