import { html } from 'lit-html'; import { customElement, instance } from '@lit-html-free/core'; import { UserService } from 'src/resources/services/userservice'; import { SharedState } from 'src/resources/state/sharedstate'; import { href } from '@lit-html-free/router'; @customElement('auth-comp') export default class extends HTMLElement { public userService: UserService; public sharedState: SharedState; public type = ''; public username = ''; public passwordConfirm = ''; public email = ''; public emailConfirm = ''; public password = ''; public errors: any[] = []; public controller: any; constructor() { super(); this.userService = instance(UserService); this.sharedState = instance(SharedState); } public async activate(_route: string, _params: any, route: any) { this.type = route.name; } public get canSave() { if (this.type === 'Login') { return this.email !== '' && this.password !== ''; } else { return ( this.username !== '' && this.email !== '' && this.password !== '' && this.password === this.passwordConfirm && this.email === this.emailConfirm ); } } public async submitForm() { this.errors = []; const credentials = { username: this.username, email: this.email, password: this.password }; try { await this.userService.attemptAuth(this.type, credentials); location.hash = 'home'; } catch (e) { const err: any = await Promise.resolve(e); for (const k in err.errors) { if (err.errors && err.errors[k]) { this.errors.push( err.errors[k].map((x: any) => { return k + ': ' + x; }) ); } } this.render(); } } public render() { return html`

Sign ${this.type === 'login' ? 'in' : 'up'}

${this.type === 'Register' ? html` Have an account? ` : html` Need an account? `}

    ${this.errors.map(error => { return html`
  • ${error}
  • `; })}
${this.type === 'Register' ? html`
{ this.username = e.target.value; this.render(); }} />
` : ''}
{ this.email = e.target.value; this.render(); }} />
${this.type === 'Register' ? html`
{ this.emailConfirm = e.target.value; this.render(); }} />
` : ''}
{ this.password = e.target.value; this.render(); }} />
${this.type === 'Register' ? html`
{ this.passwordConfirm = e.target.value; this.render(); }} />
` : ''}
`; } }