import { Component, Inject, inject } from '@angular/core';
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { NgIf } from '@angular/common';
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatButtonModule } from '@angular/material/button';
import { PageService } from '../../core/services/page.service';
@Component({
selector: 'app-create-page-dialog',
standalone: true,
imports: [NgIf, MatDialogModule, MatFormFieldModule, MatInputModule, MatButtonModule, ReactiveFormsModule],
template: `
Add Page
`,
})
export class CreatePageDialog {
private fb = inject(FormBuilder);
private dialogRef = inject(MatDialogRef);
private pageService = inject(PageService);
constructor() {
const data: { siteId: string } = inject(MAT_DIALOG_DATA);
this.siteId = data.siteId;
}
siteId: string;
form = this.fb.group({
title: ['', Validators.required],
slug: ['', [Validators.pattern(/^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/)]],
});
loading = false;
onCreate(): void {
if (this.form.invalid) return;
this.loading = true;
this.pageService.create(this.siteId, {
title: this.form.value.title || '',
slug: this.form.value.slug || '',
orderIndex: 0,
}).subscribe({
next: (page) => {
this.dialogRef.close(page);
},
error: () => {
this.loading = false;
},
});
}
}