Skip to content

Instantly share code, notes, and snippets.

@samverneck
Last active September 5, 2017 03:07
Show Gist options
  • Save samverneck/7ef83ddab66352f7d391ef788cfb8f7e to your computer and use it in GitHub Desktop.
Save samverneck/7ef83ddab66352f7d391ef788cfb8f7e to your computer and use it in GitHub Desktop.
// import { Component, OnInit, OnDestroy, HostBinding } from '@angular/core'
// import { Router, ActivatedRoute } from '@angular/router'
// import { Subscription } from 'rxjs/Subscription'
// import { AuthService } from '../../core'
// @Component( {
// templateUrl: './forgot.component.html',
// styleUrls: [ './forgot.component.scss' ]
// } )
// export class ForgotComponent implements OnInit, OnDestroy {
// public email: string
// public password: string
// public confirm: string
// public token: string
// public finish: boolean = false
// public alert = {
// type: '',
// message: ''
// }
// private sub: Subscription
// @HostBinding( 'class' ) public classes = 'forgot-page app'
// /**
// * Creates an instance of ForgotComponent.
// * @param {AuthService} auth
// * @param {Router} router
// * @param {ActivatedRoute} route
// * @memberof ForgotComponent
// */
// constructor ( private auth: AuthService, private router: Router, private route: ActivatedRoute ) {
// this.sub = this.route.params.subscribe( params => {
// let token = params[ 'token' ]
// if ( token ) {
// let subValidateToken = this.auth.forgotToken( token ).subscribe(
// user => {
// this.email = user.email
// this.token = token
// },
// message => {
// this.alert.type = 'danger'
// this.alert.message = message
// },
// () => {
// subValidateToken.unsubscribe()
// }
// )
// }
// } )
// }
// public submitEmail(): void {
// this.auth.sendEmailForgot( this.email ).subscribe(
// res => {
// this.alert.type = 'success'
// this.alert.message = res.message
// },
// message => {
// this.alert.type = 'danger'
// this.alert.message = message
// }
// )
// }
// public submitPassword( e: Event ): void {
// e.preventDefault()
// if ( !this.validate() ) {
// return
// }
// this.auth.updatePassword( this.token, this.password ).subscribe(
// message => {
// this.alert.type = 'success'
// this.alert.message = 'Senha atualizada com sucesso!'
// this.password = this.confirm = this.email = null
// this.finish = true
// },
// message => {
// this.alert.type = 'danger'
// this.alert.message = message
// }
// )
// }
// /**
// *
// *
// * @memberof ForgotComponent
// */
// public closeAlert() {
// this.alert.message = null
// }
// /**
// *
// *
// * @memberof ForgotComponent
// */
// public ngOnInit() { }
// /**
// *
// *
// * @memberof ForgotComponent
// */
// public ngOnDestroy(): void {
// this.sub.unsubscribe()
// }
// /**
// *
// *
// * @private
// * @returns {boolean}
// * @memberof ForgotComponent
// */
// private validate(): boolean {
// const isValid = this.password !== this.confirm
// if ( isValid ) {
// this.closeAlert()
// } else {
// this.alert.type = 'danger'
// this.alert.message = 'A confirmação de senha não confere!'
// }
// return isValid
// }
// }
import { Component, OnInit, OnDestroy, HostBinding } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
import { Subscription } from 'rxjs/Subscription'
import { AuthService } from '../../core'
@Component( {
templateUrl: './forgot.component.html',
styleUrls: [ './forgot.component.scss' ]
} )
export class ForgotComponent implements OnInit, OnDestroy {
id: any;
public email: string
public password: string
public confirm: string
public token: string
public finish: boolean = false
isLoading = true
public alert = {
type: '',
message: ''
}
private sub: Subscription
@HostBinding( 'class' ) public classes = 'forgot-page app'
/**
* Creates an instance of ForgotComponent.
* @param {AuthService} auth
* @param {Router} router
* @param {ActivatedRoute} route
* @memberof ForgotComponent
*/
constructor ( private auth: AuthService, private router: Router, private route: ActivatedRoute ) {
this.route.params.subscribe(params => {
setTimeout(() => {
if (params['id']) {
this.id = params['id'];
}
this.isLoading = false;
}
},
}
this.sub = this.route.params.subscribe( params => {
let token = params[ 'token' ]
if ( token ) {
let subValidateToken = this.auth.forgotToken( token ).subscribe(
user => {
this.email = user.email
this.token = token
},
message => {
this.alert.type = 'danger'
this.alert.message = message
},
() => {
subValidateToken.unsubscribe()
}
)
}
} )
}
public submitEmail(): void {
this.auth.sendEmailForgot( this.email ).subscribe(
res => {
this.alert.type = 'success'
this.alert.message = res.message
},
message => {
this.alert.type = 'danger'
this.alert.message = message
}
)
}
public submitPassword( e: Event ): void {
e.preventDefault()
if ( !this.validate() ) {
return
}
this.auth.updatePassword( this.token, this.password ).subscribe(
message => {
this.alert.type = 'success'
this.alert.message = 'Senha atualizada com sucesso!'
this.password = this.confirm = this.email = null
this.finish = true
},
message => {
this.alert.type = 'danger'
this.alert.message = message
}
)
}
/**
*
*
* @memberof ForgotComponent
*/
public closeAlert() {
this.alert.message = null
}
/**
*
*
* @memberof ForgotComponent
*/
public ngOnInit() { }
/**
*
*
* @memberof ForgotComponent
*/
public ngOnDestroy(): void {
this.sub.unsubscribe()
}
/**
*
*
* @private
* @returns {boolean}
* @memberof ForgotComponent
*/
private validate(): boolean {
const isValid = this.password !== this.confirm
if ( isValid ) {
this.closeAlert()
} else {
this.alert.type = 'danger'
this.alert.message = 'A confirmação de senha não confere!'
}
return isValid
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment