[frontend] Implement delete and edit hood

This commit is contained in:
Cathy Hu 2020-09-04 20:56:07 +02:00
parent ad1fe435a3
commit 84e506e367
7 changed files with 162 additions and 3 deletions

View file

@ -18,5 +18,11 @@
<mat-tab label="Hoodpage"> <mat-tab label="Hoodpage">
<app-hoodpage [hoodId]="hoodId"></app-hoodpage> <app-hoodpage [hoodId]="hoodId"></app-hoodpage>
</mat-tab> </mat-tab>
<mat-tab>
<ng-template mat-tab-label>
<mat-icon>settings</mat-icon>
</ng-template>
<app-settings [hoodId]="hoodId"></app-settings>
</mat-tab>
</mat-tab-group> </mat-tab-group>
</div> </div>

View file

@ -13,12 +13,12 @@ export class HoodpageComponent implements OnInit {
submit = false; submit = false;
hood: BodyHood; hood: BodyHood;
constructor(private hoodService: HoodsService) {} constructor(private hoodsService: HoodsService) {}
markdown = `# TODO Hoodpage`; markdown = `# TODO Hoodpage`;
ngOnInit(): void { ngOnInit(): void {
this.hoodService.getHood(this.hoodId).subscribe((hood) => { this.hoodsService.getHood(this.hoodId).subscribe((hood) => {
if (hood) { if (hood) {
this.hood = hood; this.hood = hood;
if (hood.landingpage && hood.landingpage !== '') { if (hood.landingpage && hood.landingpage !== '') {
@ -31,7 +31,7 @@ export class HoodpageComponent implements OnInit {
onSubmit() { onSubmit() {
this.submit = true; this.submit = true;
this.hood.landingpage = this.markdown; this.hood.landingpage = this.markdown;
this.hoodService this.hoodsService
.updateHood(this.hoodId, this.hood) .updateHood(this.hoodId, this.hood)
.pipe(first()) .pipe(first())
.subscribe( .subscribe(

View file

@ -0,0 +1,25 @@
<div class="container">
<mat-card>
<form [formGroup]="form" class="form">
<mat-form-field>
<mat-label>Update hood name</mat-label>
<input matInput formControlName="name" />
</mat-form-field>
<button mat-raised-button color="primary" (click)="onUpdate()">
Update hood name
</button>
</form>
</mat-card>
<mat-card>
<div class="delete">
<mat-card-title>Danger zone</mat-card-title>
<mat-divider></mat-divider>
<mat-card-content>
<button mat-raised-button color="warn" (click)="onDelete()">
Delete hood
</button>
</mat-card-content>
</div>
</mat-card>
</div>

View file

@ -0,0 +1,25 @@
.container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
align-items: stretch;
justify-items: stretch;
@media (max-width: 600px) {
grid-template-columns: 1fr;
}
margin: 1%;
}
.form {
display: grid;
grid-template-columns: 1fr;
gap: 10px;
}
.delete {
display: grid;
grid-template-columns: 1fr;
gap: 10px;
align-items: center;
justify-items: center;
}

View file

@ -0,0 +1,24 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SettingsComponent } from './settings.component';
describe('SettingsComponent', () => {
let component: SettingsComponent;
let fixture: ComponentFixture<SettingsComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [SettingsComponent],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SettingsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View file

@ -0,0 +1,77 @@
import { Component, OnInit, Input } from '@angular/core';
import { HoodsService } from 'src/app/core/api';
import { MatDialog } from '@angular/material/dialog';
import { YesNoDialogComponent } from 'src/app/shared/yes-no-dialog/yes-no-dialog.component';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { first } from 'rxjs/operators';
import { MatSnackBar } from '@angular/material/snack-bar';
@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.scss'],
})
export class SettingsComponent implements OnInit {
@Input() hoodId;
form: FormGroup;
hood;
constructor(
private hoodsService: HoodsService,
private dialog: MatDialog,
private router: Router,
private formBuilder: FormBuilder,
private snackBar: MatSnackBar
) {}
ngOnInit(): void {
this.hoodsService.getHood(this.hoodId).subscribe((hood) => {
if (hood) {
this.hood = hood;
}
});
this.form = this.formBuilder.group({
name: ['', Validators.required],
});
}
onDelete() {
const dialogRef = this.dialog.open(YesNoDialogComponent, {
data: {
title: 'Warning',
content: 'This will also delete all of your platform connections.',
},
});
dialogRef.afterClosed().subscribe((response) => {
if (response) {
this.hoodsService.deleteHood(this.hoodId).subscribe(() => {
this.router.navigate(['/dashboard']);
});
}
});
}
onUpdate() {
if (this.form.invalid) {
return;
}
this.hood.name = this.form.controls.name.value;
this.hoodsService
.updateHood(this.hoodId, this.hood)
.pipe(first())
.subscribe(
(data) => {
this.form.reset();
location.reload();
},
(error) => {
this.snackBar.open('Update failed. Try again!', 'Close', {
duration: 2000,
});
}
);
}
}

View file

@ -13,6 +13,7 @@ import { MarkdownModule } from 'ngx-markdown';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { NewHoodDialogComponent } from './hoods/new-hood-dialog/new-hood-dialog.component'; import { NewHoodDialogComponent } from './hoods/new-hood-dialog/new-hood-dialog.component';
import { PlatformsModule } from '../platforms/platforms.module'; import { PlatformsModule } from '../platforms/platforms.module';
import { SettingsComponent } from './board/settings/settings.component';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -25,6 +26,7 @@ import { PlatformsModule } from '../platforms/platforms.module';
TriggerComponent, TriggerComponent,
BadwordsComponent, BadwordsComponent,
NewHoodDialogComponent, NewHoodDialogComponent,
SettingsComponent,
], ],
imports: [ imports: [
DashboardRoutingModule, DashboardRoutingModule,