Broker System for Supercredit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.7KB

  1. import {Component, Input, OnInit, ViewChild, EventEmitter, Output, OnChanges, SimpleChanges, QueryList, ViewChildren} from '@angular/core';
  2. import {MessageBoxComponent} from '../../message-box/message-box.component';
  3. import {FileInfo, FileRestrictions, FileSelectComponent, SelectEvent} from '@progress/kendo-angular-upload';
  4. import {AuthService} from '../../service/auth.service';
  5. import {PeopleService} from '../../service/people.service';
  6. import {NgForm, NgModel} from '@angular/forms';
  7. import {PeopleModel} from '../../models/people.model';
  8. import {SessionService} from '../../service/session.service';
  9. import {AppConfig} from '../../app.config';
  10. @Component({
  11. selector: 'app-people-profile',
  12. templateUrl: './people-profile.component.html',
  13. styleUrls: ['./people-profile.component.scss']
  14. })
  15. export class PeopleProfileComponent implements OnInit {
  16. @Input() public People: PeopleModel = PeopleModel.EmptyNew();
  17. @Output() public newPeople = new EventEmitter<PeopleModel>();
  18. public avatarUrl = 'url(https://via.placeholder.com/128)' ;
  19. public myRestrictions: FileRestrictions = {
  20. allowedExtensions: ['.jpg', '.png', '.jpeg'],
  21. maxFileSize: 2194304
  22. };
  23. @ViewChild('messagebox', {static: true}) msgBox: MessageBoxComponent;
  24. @ViewChild('fileSelect', {static: true}) fs: FileSelectComponent;
  25. constructor(private ss: SessionService, private config: AppConfig, private ps: PeopleService) { }
  26. ngOnInit(): void {
  27. if (this.ss.isAdmin() && !this.ss.isCurrentUser(this.People.Id)) {
  28. //
  29. }else{
  30. this.People = this.ss.loggedIn.User;
  31. }
  32. this.avatarUrl = 'url(' + this.config.getUrl('avatar/' + this.People.Id) + ')';
  33. }
  34. onDialogClose(status: string): void {
  35. console.log(status);
  36. }
  37. public onSelectAvatar(ev: SelectEvent): void {
  38. if (ev.files) {
  39. ev.files.every((file: FileInfo) => {
  40. if (file.rawFile && !file.validationErrors) {
  41. const reader = new FileReader();
  42. reader.onloadend = () => {
  43. const str = reader.result as string;
  44. this.ps.updateAvatar(str, this.People.Id).subscribe( resp => {
  45. this.avatarUrl = 'url(' + str + ' )';
  46. }, err => {
  47. this.msgBox.Show('Failed to Update Avatar: ' + err.toString());
  48. });
  49. this.fs.clearFiles();
  50. };
  51. reader.readAsDataURL(file.rawFile);
  52. }else{
  53. this.msgBox.Show('Only jpg, and png are supported (max 2MB)');
  54. setTimeout(() => { this.fs.clearFiles(); }, 10);
  55. }
  56. return false; // we only take first file
  57. });
  58. }
  59. }
  60. public save(peopleForm: NgForm): void{
  61. if (! peopleForm.touched || peopleForm.form.pristine) {
  62. this.msgBox.Show('Nothing has been changed');
  63. return;
  64. }
  65. this.ps.savePeople(this.People).subscribe( () => {
  66. this.msgBox.Show('Updated successfully ');
  67. if ( this.ss.loggedIn.User.Id === this.People.Id ) {
  68. this.ss.UpdatePeopleInfo(this.People);
  69. }
  70. peopleForm.form.markAsPristine();
  71. }, err => {
  72. this.msgBox.Show('Failed to Update: ' + err.toString());
  73. });
  74. }
  75. public createPeople(): void{
  76. this.ps.createPeople(this.People).subscribe(
  77. ppl => {
  78. this.People.Copy(ppl);
  79. this.newPeople.emit(this.People);
  80. this.avatarUrl = 'url(' + this.config.getUrl('avatar/' + this.People.Id) + ')';
  81. }
  82. );
  83. }
  84. public PeopleChanged(ppl: PeopleModel): void {
  85. console.log(this);
  86. this.avatarUrl = 'url(' + this.config.getUrl('avatar/' + this.People.Id) + ')';
  87. }
  88. public startSelectAvatar(): void {
  89. const element: NodeListOf<HTMLElement> = document.getElementsByName('selectAvatar');
  90. element.forEach( v => {
  91. if ( v.tagName.toLowerCase() === 'input') {
  92. v.click();
  93. }
  94. });
  95. }
  96. }