import { Component, Inject, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { MatchingValidator } from '../../../utils/matching.validator'; import { InviteService } from '../../../services/invites.service'; import { uniqueNamesGenerator, Config, adjectives, colors, animals } from 'unique-names-generator'; import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; @Component({ selector: 'app-invite-edit', templateUrl: './invite.edit.html', styleUrls: [ './invite.edit.scss' ] }) export class InviteEditComponent { form: FormGroup; error: string; working: boolean = false; success: boolean = false; invite: any; constructor( private inviteService: InviteService, private formBuilder: FormBuilder, @Inject(MAT_DIALOG_DATA) public data: any, public dialogRef: MatDialogRef, public dialog: MatDialog) { this.form = this.formBuilder.group({ message: [ '', Validators.nullValidator ], note: [ '', Validators.nullValidator ], }); this.invite = data; this.form.get("message").setValue(this.invite.message); this.form.get("note").setValue(this.invite.note); } save() { if (this.form.valid && !this.working) { this.working = true; this.invite.message = this.form.get("message").value; this.invite.note = this.form.get("note").value; this.inviteService.update(this.invite).subscribe({ next: (result: any) => { this.working = false; this.dialogRef.close(result); }, error: (error) => { this.working = false; if (error.status == 406) { this.error = "INVALID_CODE"; } if (error.status == 410) { this.error = "ALREADY_REDEEMED"; } else if (error.status == 409) { let errors = {}; for (let code of error.error) { errors[ code.field ] = errors[ code.field ] || {}; errors[ code.field ][ code.code ] = true; } for (let code in errors) { this.form.get(code).setErrors(errors[ code ]); } } } }) } } }