[frontend] Fix edit

This commit is contained in:
Cathy Hu 2020-09-01 14:51:27 +02:00
parent 0df7f39551
commit d8ae3ff693
2 changed files with 53 additions and 18 deletions

View file

@ -1,9 +1,10 @@
import { Component, OnInit, Inject } from '@angular/core'; import { Component, OnInit, Inject } from '@angular/core';
import { Validators, FormBuilder, FormGroup } from '@angular/forms'; import { Validators, FormBuilder, FormGroup } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog'; import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { TelegramService } from 'src/app/core/api'; import { TelegramService, BodyTelegram } from 'src/app/core/api';
import { MatSnackBar } from '@angular/material/snack-bar'; import { MatSnackBar } from '@angular/material/snack-bar';
import { first } from 'rxjs/operators'; import { first } from 'rxjs/operators';
import { map } from 'rxjs/operators';
@Component({ @Component({
selector: 'app-telegram-dialog', selector: 'app-telegram-dialog',
@ -26,31 +27,65 @@ export class TelegramDialogComponent implements OnInit {
api_token: ['', Validators.required], api_token: ['', Validators.required],
welcome_message: '', welcome_message: '',
}); });
if (this.data.telegramId) {
this.telegramService
.getTelegram(this.data.telegramId, this.data.hoodId)
.subscribe((data) => {
this.form.controls.api_token.setValue(data.api_token);
this.form.controls.welcome_message.setValue(data.welcome_message);
});
}
} }
onCancel() { onCancel() {
this.dialogRef.close(); this.dialogRef.close();
} }
onSubmit() { success() {
if (this.form.invalid) {
return;
}
this.telegramService
.createTelegram(this.data.hoodId, {
api_token: this.form.controls.api_token.value,
welcome_message: this.form.controls.welcome_message.value,
})
.pipe(first())
.subscribe(
(data) => {
this.dialogRef.close(); this.dialogRef.close();
}, }
(error) => {
error() {
this.snackBar.open('Invalid API Key. Try again!', 'Close', { this.snackBar.open('Invalid API Key. Try again!', 'Close', {
duration: 2000, duration: 2000,
}); });
} }
onSubmit() {
if (this.form.invalid) {
return;
}
const response = {
api_token: this.form.controls.api_token.value,
welcome_message: this.form.controls.welcome_message.value,
};
if (this.data.telegramId) {
this.telegramService
.updateTelegram(this.data.telegramId, this.data.hoodId, response)
.pipe(first())
.subscribe(
() => {
this.success();
},
() => {
this.error();
}
);
} else {
this.telegramService
.createTelegram(this.data.hoodId, response)
.pipe(first())
.subscribe(
() => {
this.success();
},
() => {
this.error();
}
); );
} }
}
} }

View file

@ -1,3 +1,3 @@
<div > <div>
<mat-spinner class="spinner"></mat-spinner> <mat-spinner class="spinner"></mat-spinner>
</div> </div>