|
|
@@ -39,6 +39,9 @@ displayedColumns: string[] = ['id', 'title', 'ver', 'editar', 'publicar', 'elimi
|
|
|
|
|
|
constructor(private formService: FormService, private _enviarInfo: EnviarInfoService, private router: Router, private snackBar: MatSnackBar
|
|
|
) {}
|
|
|
+ get hayFormularioPublicado(): boolean {
|
|
|
+ return this.dataSource.data.some(f => f.is_published);
|
|
|
+ }
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
this._enviarInfo.currentTextColor.subscribe(color => this.textColor = color);
|
|
|
@@ -97,8 +100,75 @@ showMessage(message: string) {
|
|
|
horizontalPosition: 'right',
|
|
|
verticalPosition: 'top'
|
|
|
});
|
|
|
+}publicarFormulario(formId: number): void {
|
|
|
+ this.isLoading = true;
|
|
|
+
|
|
|
+ // Primero despublicamos cualquier otro formulario publicado antes de publicar el nuevo
|
|
|
+ const formPublicado = this.dataSource.data.find(f => f.is_published);
|
|
|
+
|
|
|
+ const despublicar$ = formPublicado && formPublicado.id !== formId
|
|
|
+ ? this.formService.unpublishForm(formPublicado.id)
|
|
|
+ : null;
|
|
|
+
|
|
|
+ const publicar$ = () => this.formService.publishForm(formId);
|
|
|
+
|
|
|
+ if (despublicar$) {
|
|
|
+ despublicar$.subscribe({
|
|
|
+ next: () => {
|
|
|
+ publicar$().subscribe({
|
|
|
+ next: () => {
|
|
|
+ this.updateFormsAfterPublish(formId);
|
|
|
+ },
|
|
|
+ error: (err) => this.handleError(err, 'Error al publicar el formulario.')
|
|
|
+ });
|
|
|
+ },
|
|
|
+ error: (err) => this.handleError(err, 'Error al despublicar el formulario anterior.')
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ publicar$().subscribe({
|
|
|
+ next: () => {
|
|
|
+ this.updateFormsAfterPublish(formId);
|
|
|
+ },
|
|
|
+ error: (err) => this.handleError(err, 'Error al publicar el formulario.')
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+updateFormsAfterPublish(formId: number) {
|
|
|
+ // Actualiza todos los formularios: solo el publicado queda true, los demás false
|
|
|
+ this.dataSource.data = this.dataSource.data.map(f => ({
|
|
|
+ ...f,
|
|
|
+ is_published: f.id === formId
|
|
|
+ }));
|
|
|
+ this.isLoading = false;
|
|
|
+ this.showMessage('Formulario publicado correctamente. Solo uno puede estar publicado a la vez.');
|
|
|
+}
|
|
|
+
|
|
|
+despublicarFormulario(formId: number): void {
|
|
|
+ this.isLoading = true;
|
|
|
+ this.formService.unpublishForm(formId).subscribe({
|
|
|
+ next: () => {
|
|
|
+ const form = this.dataSource.data.find(f => f.id === formId);
|
|
|
+ if (form) {
|
|
|
+ form.is_published = false;
|
|
|
+ }
|
|
|
+ this.dataSource.data = [...this.dataSource.data];
|
|
|
+ this.isLoading = false;
|
|
|
+ this.showMessage('Formulario despublicado correctamente.');
|
|
|
+ },
|
|
|
+ error: (err) => {
|
|
|
+ this.isLoading = false;
|
|
|
+ console.error('Error al despublicar el formulario:', err);
|
|
|
+ this.showMessage('Error al despublicar el formulario.');
|
|
|
+ }
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+handleError(error: any, message: string) {
|
|
|
+ this.isLoading = false;
|
|
|
+ console.error(error);
|
|
|
+ this.showMessage(message);
|
|
|
+}
|
|
|
|
|
|
|
|
|
|