Broker System for Supercredit
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

109 lines
3.5KB

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