Guest User

Untitled

a guest
Jun 22nd, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. import { Component, ViewEncapsulation, Injector, OnInit } from '@angular/core';
  2. import { Router, ActivatedRoute, ParamMap } from '@angular/router';
  3. import { FormBuilder, FormGroup, Validators, AbstractControl } from '@angular/forms';
  4.  
  5. import { ViewChild } from '@angular/core';
  6.  
  7. import { Config } from './../shared/config';
  8.  
  9. import { User } from './../shared/user/user';
  10. import { UserService } from './../shared/user/user.service';
  11.  
  12. import { Subscription } from 'rxjs';
  13.  
  14. @Component({
  15. selector: '[profile]',
  16. templateUrl: './profile.template.html',
  17. encapsulation: ViewEncapsulation.None,
  18. styleUrls: ['./profile.style.scss']
  19. })
  20. export class Profile implements OnInit {
  21. user: User = new User();
  22. editing: boolean = false;
  23. userForm: FormGroup;
  24. public busy: Subscription;
  25. alerts: Array<{type: string, msg: string}>;
  26.  
  27. constructor(injector: Injector,
  28. private router: Router,
  29. private route: ActivatedRoute,
  30. private fb: FormBuilder,
  31. private userService: UserService) {
  32.  
  33. console.log("User Profile");
  34. this.alerts = [];
  35. }
  36.  
  37. createUserForm() {
  38.  
  39. this.userForm = this.fb.group({
  40. uid: '',
  41. user_name: ['', Validators.required],
  42. email_address: ['', Validators.required],
  43. first_name: '',
  44. last_name: '',
  45. display_name: '',
  46. phone_number: '',
  47. mobile_number: '',
  48. user_settings: '',
  49. gender: '',
  50. dob: '',
  51. avatar_image_url: '',
  52. passwords: this.fb.group({
  53. password: ['', Validators.minLength(8)],
  54. confirm_password: ['', Validators.minLength(8)],
  55. }, { validator: this.passwordConfirming }),
  56.  
  57.  
  58. inspector: this.fb.group({
  59. uid: '',
  60. name: '',
  61. rfc: '',
  62. razon_social: '',
  63. contact_phone: '',
  64. contact_name: '',
  65. email_address: '',
  66. inspector_type: '',
  67. bank_account_holder: '',
  68. bank_account_number: '',
  69. bank_clabe: '',
  70. bank_name: '',
  71.  
  72. address: this.fb.group({
  73. name: '',
  74. street: '',
  75. ext_number: '',
  76. int_number: '',
  77. neighborhood: '',
  78. community: '',
  79. district: '',
  80. city: '',
  81. state: '',
  82. zipcode: '',
  83. country_desc: '',
  84. reference: '',
  85. phone_work: '',
  86. phone_other: ''
  87.  
  88. })
  89.  
  90. })
  91.  
  92. });
  93.  
  94. // fill form values
  95. this.userForm.patchValue(this.user);
  96. this.onChanges();
  97.  
  98. }
  99.  
  100. onChanges(): void {
  101. this.userForm.get('first_name').valueChanges.subscribe(val => {
  102. this.userForm.get('display_name').setValue(
  103. this.userForm.get('first_name').value
  104. + " "
  105. + this.userForm.get('last_name').value);
  106. });
  107. this.userForm.get('last_name').valueChanges.subscribe(val => {
  108. this.userForm.get('display_name').setValue(
  109. this.userForm.get('first_name').value
  110. + " "
  111. + this.userForm.get('last_name').value);
  112. });
  113. }
  114.  
  115. rebuildForm() {
  116. this.userForm.reset({
  117. user_name: this.user.user_name,
  118. email_address: this.user.email_address,
  119. first_name: this.user.first_name,
  120. last_name: this.user.last_name,
  121. display_name: this.user.display_name,
  122. phone_number: this.user.phone_number,
  123. mobile_number: this.user.mobile_number,
  124. user_settings: this.user.user_settings,
  125. gender: this.user.gender,
  126. dob: this.user.dob,
  127. avatar_image_url: this.user.avatar_image_url
  128. });
  129. }
  130.  
  131. passwordConfirming(c: AbstractControl): { invalid: boolean } {
  132. if (c.get('password').value !== c.get('confirm_password').value) {
  133. return { invalid: true };
  134. }
  135. }
  136.  
  137. prepareSaveUser(): User {
  138. const formModel = this.userForm.value;
  139. let updatedUser: User = new User();
  140.  
  141. Object.assign(updatedUser, formModel);
  142.  
  143. if (this.userForm.get('passwords.password').value) {
  144. // changing user password, adding to object
  145. let new_password = this.userForm.get('passwords.password').value;
  146. updatedUser.password = new_password;
  147. }
  148.  
  149. return updatedUser;
  150. }
  151.  
  152. ngOnInit(): void {
  153. let uid = this.route.snapshot.paramMap.get('uid');
  154. if (uid == 'mine') {
  155. // get current user data profile
  156. this.userService.getCurrent().subscribe(
  157. data => {
  158. if (data.success) {
  159. this.user = new User();
  160. Object.assign(this.user, data.user);
  161. console.log("current_user: ", JSON.stringify(this.user));
  162. }
  163. },
  164. error => {
  165. console.error("error: ", error);
  166. });
  167. }
  168. }
  169.  
  170. addAlert(content): void {
  171. // content i.e. {type: 'warning', msg: 'Another alert!'}
  172. this.alerts.push(content);
  173. };
  174.  
  175. public editProfile() {
  176. this.createUserForm();
  177. this.editing = true;
  178. }
  179.  
  180. public cancelEdit() {
  181. this.editing = false;
  182. }
  183.  
  184. onSubmit() {
  185. if (this.userForm.valid) {
  186. this.user = this.prepareSaveUser();
  187. this.busy = this.userService.update(this.user)
  188. .subscribe(
  189. data => {
  190. // alert success
  191. console.log("user saved");
  192. },
  193. err => {
  194. console.error("error saving user: ", err);
  195. }
  196. );
  197. this.rebuildForm();
  198. this.addAlert({type: 'success', msg: '<span class="fw-semi-bold">Éxito:</span> El usuario ha sido actualizado'});
  199. this.editing = false;
  200. } else {
  201. // form no valid to post
  202. console.error("trying to submit invalid form");
  203. }
  204. }
  205. }
Add Comment
Please, Sign In to add comment