signup-modal.component.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { Component, OnInit } from '@angular/core';
  2. import { LocalStorage } from './../../core/services/local_storage.service';
  3. import { CONFIG } from '../../config';
  4. import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
  5. import { HyperService } from './../../core/services/http.service';
  6. import { timer } from 'rxjs/observable/timer';
  7. import { take, map } from 'rxjs/operators';
  8. import { Observable } from "rxjs/Observable";
  9. import "rxjs/add/observable/timer";
  10. import "rxjs/add/operator/finally";
  11. import "rxjs/add/operator/takeUntil";
  12. import "rxjs/add/operator/map";
  13. import { Subscription } from "rxjs";
  14. import { TimerObservable } from "rxjs/observable/TimerObservable";
  15. import { Router } from "@angular/router";
  16. import { ViewChild, ElementRef } from '@angular/core';
  17. declare var $: any;
  18. @Component({
  19. selector: 'app-signup-modal',
  20. templateUrl: './signup-modal.component.html',
  21. styleUrls: ['./signup-modal.component.scss']
  22. })
  23. export class SignupModalComponent implements OnInit {
  24. @ViewChild('closeBtn') closeBtn: ElementRef;
  25. otpform: FormGroup;
  26. otp:any;
  27. otp1: any;
  28. otp2: any;
  29. otp3: any;
  30. otp4: any;
  31. section: number;
  32. user_mobile: any;
  33. countdown: string;
  34. loader_timing: any;
  35. loader_delay: any;
  36. otp_error: string = '';
  37. otp_sucess: boolean = false;
  38. resend:boolean=false;
  39. resend_enable:boolean=false;
  40. sub: Subscription;
  41. stream$;
  42. isTimerRunning: boolean = false;
  43. constructor(private formBuilder: FormBuilder, private server: HyperService, private router: Router) {
  44. console.log("here")
  45. }
  46. ngOnInit() {
  47. // $('#fogotexampleModalCenter').modal({
  48. // backdrop: 'static',
  49. // keyboard: false
  50. // })
  51. this.formBuild();
  52. }
  53. formBuild() {
  54. this.otpform = this.formBuilder.group({
  55. otp1: [null, Validators.compose([Validators.required])],
  56. otp2: [null, Validators.compose([Validators.required])],
  57. otp3: [null, Validators.compose([Validators.required])],
  58. otp4: [null, Validators.compose([Validators.required])]
  59. });
  60. }
  61. accept() {
  62. this.section = 1;
  63. console.log(this.section)
  64. this.signUpOtp();
  65. }
  66. reject() {
  67. this.section=null;
  68. $("#exampleModalCenter").modal('hide');
  69. this.countdown = '';
  70. this.otpform.markAsUntouched();
  71. CONFIG.forgot_cont = false;
  72. console.log(CONFIG.forgot_cont)
  73. this.resend_enable=false;
  74. this.otp_error = '';
  75. this.otp1 = ''
  76. this.otp2=""
  77. this.otp3=""
  78. this.otp4=""
  79. }
  80. modelClose(){
  81. this.countdown=''
  82. this.section =null;
  83. }
  84. resendOtp() {
  85. this.resend=true;
  86. this.resend_enable=false;
  87. this.signUpOtp();
  88. }
  89. contactDealer(){
  90. this.section =null;
  91. }
  92. signUpOtp() {
  93. this.server.post("user/signup_otp", {
  94. username: LocalStorage.getValue('signup_user').username
  95. }).then((data) => {
  96. console.log(data)
  97. if (data.status == 200 && data.result.response == "success") {
  98. let user_data = LocalStorage.getValue('signup_user');
  99. console.log(user_data.mobile,user_data.mobile.substring(0, 2)),user_data.mobile.substring(7, 10)
  100. this.user_mobile = user_data.mobile.substring(0, 3) + 'xxxx' + user_data.mobile.substring(7, 10);
  101. this.TimeSet(data.result.data.otp_expiry)
  102. } else if (data.result.response == "failed") {
  103. }
  104. })
  105. }
  106. TimeSet(Time) {
  107. this.loader_timing = Time * 60;
  108. this.loader_delay = this.loader_timing / 2;
  109. console.log('Testarstars', this.loader_timing)
  110. console.log('Testarstars', this.loader_delay)
  111. const interval = 1000;
  112. const duration = 10 * 6000 * Time;
  113. this.stream$ = Observable.timer(0, interval)
  114. .finally(() => this.onclose())
  115. .takeUntil(Observable.timer(duration + interval))
  116. .map(value => duration - value * interval);
  117. this.sub = this.stream$.subscribe(value => this.formatMin(value));
  118. }
  119. onclose() {
  120. this.countdown=''
  121. this.resend_enable=true;
  122. }
  123. formatMin(s) {
  124. let ms = s % 1000;
  125. s = (s - ms) / 1000;
  126. let secs = s % 60;
  127. s = (s - secs) / 60;
  128. let mins = s % 60;
  129. let hrs = (s - mins) / 60;
  130. if (hrs > 0) {
  131. this.countdown = hrs + ':' + mins + ':' + secs;
  132. } else {
  133. this.countdown = mins + ':' + secs;
  134. }
  135. }
  136. onInputChange(){
  137. console.log("here1111")
  138. $('input.password').on('keyup', function() {
  139. if ($(this).val()) {
  140. $(this).next().focus();
  141. }
  142. });
  143. }
  144. onSubmitOtp() {
  145. if (this.otpform.valid) {
  146. this.server.post("user/otp_validation", {
  147. otp: (this.otp1.toString())+(this.otp2.toString())+(this.otp3.toString())+(this.otp4.toString()),
  148. type: 'register',
  149. username: LocalStorage.getValue('signup_user').username
  150. }).then((data) => {
  151. console.log(data)
  152. if (data.status == 200 && data.result.response == "success") {
  153. console.log('OTP VERIFIED')
  154. this.otp_sucess = true;
  155. LocalStorage.setValue('token', data.result.access_token);
  156. LocalStorage.setValue('forgot', 'register');
  157. } else if (data.result.response == "failed") {
  158. console.log('OTP not VERIFIED'+data.result.message)
  159. this.otp_error = data.result.message;
  160. }
  161. })
  162. } else {
  163. this.validateAllFormFields(this.otpform);
  164. }
  165. }
  166. gotoSetPassword() {
  167. this.router.navigateByUrl('setpassword');
  168. }
  169. validateAllFormFields(formGroup: FormGroup) {
  170. Object.keys(formGroup.controls).forEach(field => {
  171. console.log(field);
  172. const control = formGroup.get(field);
  173. if (control instanceof FormControl) {
  174. control.markAsTouched({
  175. onlySelf: true
  176. });
  177. } else if (control instanceof FormGroup) {
  178. this.validateAllFormFields(control);
  179. }
  180. });
  181. }
  182. onCancelOtp(){
  183. this.section=null;
  184. $("#exampleModalCenter").modal('hide');
  185. this.countdown = '';
  186. this.otpform.markAsUntouched();
  187. CONFIG.forgot_cont = false;
  188. console.log(CONFIG.forgot_cont)
  189. this.sub.unsubscribe()
  190. this.sub = this.stream$.subscribe(0 , this.formatMin(0));
  191. this.resend_enable=false;
  192. this.otp_error = '';
  193. this.otp1 = ''
  194. this.otp2=""
  195. this.otp3=""
  196. this.otp4=""
  197. }
  198. }