Files
we_bstly-web/src/app/pages/invites/edit/invite.edit.ts
T
2025-05-11 18:05:06 +02:00

73 lines
2.0 KiB
TypeScript

import { Component, Inject } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialog, MatDialogRef } from '@angular/material/dialog';
import { InviteService } from '../../../services/invites.service';
@Component({
standalone: false,
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<InviteEditComponent>,
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]);
}
}
}
})
}
}
}