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

85 lines
2.7KB

  1. import {Component, Input, OnInit, ViewChild} from '@angular/core';
  2. import {PeopleModel} from '../models/people.model';
  3. import {NgForm} from '@angular/forms';
  4. import {AuthService} from '../service/auth.service';
  5. import {PeopleService} from '../service/people.service';
  6. import {FileInfo, FileRestrictions, FileSelectComponent, SelectEvent} from '@progress/kendo-angular-upload';
  7. import {BrokerModel} from '../models/broker.model';
  8. import {AppConfig} from '../app.config';
  9. import {SessionService} from '../service/session.service';
  10. @Component({
  11. selector: 'app-client-profile',
  12. templateUrl: './client-profile.component.html',
  13. styleUrls: ['./client-profile.component.scss']
  14. })
  15. export class ClientProfileComponent implements OnInit {
  16. @Input() User: PeopleModel = PeopleModel.EmptyNew();
  17. @ViewChild('fileSelect', {static: true}) fs: FileSelectComponent;
  18. public avatarUrl = '' ;
  19. public myRestrictions: FileRestrictions = {
  20. allowedExtensions: ['.jpg', '.png', '.jpeg'],
  21. maxFileSize: 2194304
  22. };
  23. public opened = false; // dialog box
  24. public Message = ''; // dialog message
  25. constructor(private ss: SessionService, private config: AppConfig, private ps: PeopleService) {
  26. this.avatarUrl = 'url("' + location.origin + './assets/img/avatar.png' + '")';
  27. }
  28. ngOnInit(): void {
  29. this.User = this.ss.loggedIn.User;
  30. this.avatarUrl = 'url(' + this.config.getUrl('avatar/' + this.User.Id) + ')';
  31. }
  32. public save(brokerForm: NgForm): void{
  33. if (! brokerForm.touched || brokerForm.form.pristine) {
  34. return;
  35. }
  36. this.ps.savePeople(this.User).subscribe( () => {
  37. this.showDialog('updated successfully ');
  38. brokerForm.form.markAsPristine();
  39. }, err => {
  40. this.showDialog('Failed to Update: ' + err.toString());
  41. });
  42. }
  43. public onSelect(ev: SelectEvent): void {
  44. if (ev.files) {
  45. ev.files.every((file: FileInfo) => {
  46. if (file.rawFile && !file.validationErrors) {
  47. const reader = new FileReader();
  48. reader.onloadend = () => {
  49. const str = reader.result as string;
  50. this.ps.updateAvatar(str, this.User.Id).subscribe( resp => {
  51. this.avatarUrl = 'url(' + str + ' )';
  52. }, err => {
  53. this.showDialog('Failed to Update Avatar: ' + err.toString());
  54. });
  55. this.fs.clearFiles();
  56. };
  57. reader.readAsDataURL(file.rawFile);
  58. }else{
  59. this.showDialog('Only jpg, and png are supported (max 2MB)');
  60. setTimeout(() => { this.fs.clearFiles(); }, 10);
  61. }
  62. return false; // we only take first file
  63. });
  64. }
  65. }
  66. public showDialog(msg: string): void {
  67. this.Message = msg;
  68. this.opened = true; // open dialog
  69. }
  70. public close(status): void {
  71. this.opened = false;
  72. }
  73. }