This commit is contained in:
Amritanshu
2019-06-23 01:35:00 +05:30
parent 20801afc8a
commit 142a0f5a8a
42 changed files with 1069 additions and 234 deletions

View File

@ -0,0 +1,3 @@
.right-align {
text-align: right;
}

View File

@ -0,0 +1,22 @@
<div fxLayout="row" fxFlex="50%" fxLayoutAlign="space-around center" class="example-card">
<mat-card fxFlex>
<mat-card-title-group>
<mat-card-title>Section</mat-card-title>
</mat-card-title-group>
<mat-card-content>
<form [formGroup]="form" fxLayout="column">
<div fxLayout="row" fxLayoutAlign="space-around start" fxLayout.lt-md="column" fxLayoutGap="20px"
fxLayoutGap.lt-md="0px">
<mat-form-field fxFlex>
<mat-label>Name</mat-label>
<input matInput #nameElement placeholder="Name" formControlName="name" (keyup.enter)="save()">
</mat-form-field>
</div>
</form>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button [disabled]="item.isFixture" color="primary" (click)="save()">Save</button>
<button mat-raised-button [disabled]="item.isFixture" color="warn" (click)="confirmDelete()" *ngIf="!!item.id">Delete</button>
</mat-card-actions>
</mat-card>
</div>

View File

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

View File

@ -0,0 +1,102 @@
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog} from "@angular/material/dialog";
import { SectionService } from '../section.service';
import { Section } from '../../core/section';
import { ToasterService } from '../../core/toaster.service';
import { ConfirmDialogComponent } from "../../shared/confirm-dialog/confirm-dialog.component";
@Component({
selector: 'app-section-detail',
templateUrl: './section-detail.component.html',
styleUrls: ['./section-detail.component.css']
})
export class SectionDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement: ElementRef;
form: FormGroup;
item: Section;
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private fb: FormBuilder,
private toaster: ToasterService,
private ser: SectionService
) {
this.createForm();
}
createForm() {
this.form = this.fb.group({
name: ''
});
}
ngOnInit() {
this.route.data
.subscribe((data: { item: Section }) => {
this.showItem(data.item);
});
}
showItem(item: Section) {
this.item = item;
this.form.setValue({
name: this.item.name || ''
});
}
ngAfterViewInit() {
setTimeout(() => {
this.nameElement.nativeElement.focus();
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem())
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/sections');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
delete() {
this.ser.delete(this.item.id)
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/sections');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Delete Section?', content: 'Are you sure? This cannot be undone.'}
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Section {
const formModel = this.form.value;
this.item.name = formModel.name;
return this.item;
}
}

View File

@ -0,0 +1,15 @@
import {inject, TestBed} from '@angular/core/testing';
import {SectionListResolver} from './section-list-resolver.service';
describe('SectionListResolverService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SectionListResolver]
});
});
it('should be created', inject([SectionListResolver], (service: SectionListResolver) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,18 @@
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from '@angular/router';
import {Section} from '../core/section';
import {Observable} from 'rxjs/internal/Observable';
import {SectionService} from './section.service';
@Injectable({
providedIn: 'root'
})
export class SectionListResolver implements Resolve<Section[]> {
constructor(private ser: SectionService, private router: Router) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Section[]> {
return this.ser.list();
}
}

View File

@ -0,0 +1,17 @@
import { DataSource} from '@angular/cdk/collections';
import { Observable, of as observableOf} from 'rxjs';
import { Section} from '../../core/section';
export class SectionListDataSource extends DataSource<Section> {
constructor(public data: Section[]) {
super();
}
connect(): Observable<Section[]> {
return observableOf(this.data);
}
disconnect() {
}
}

View File

@ -0,0 +1,22 @@
<mat-card>
<mat-card-title-group>
<mat-card-title>Sections</mat-card-title>
<a mat-button [routerLink]="['/sections', 'new']">
<mat-icon>add_box</mat-icon>
Add
</a>
</mat-card-title-group>
<mat-card-content>
<mat-table #table [dataSource]="dataSource" aria-label="Elements">
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef>Name</mat-header-cell>
<mat-cell *matCellDef="let row"><a [routerLink]="['/sections', row.id]">{{row.name}}</a></mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
</mat-table>
</mat-card-content>
</mat-card>

View File

@ -0,0 +1,23 @@
import {ComponentFixture, fakeAsync, TestBed} from '@angular/core/testing';
import {SectionListComponent} from './section-list.component';
describe('SectionListComponent', () => {
let component: SectionListComponent;
let fixture: ComponentFixture<SectionListComponent>;
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [SectionListComponent]
})
.compileComponents();
fixture = TestBed.createComponent(SectionListComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
it('should compile', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,27 @@
import { Component, OnInit } from '@angular/core';
import { SectionListDataSource } from './section-list-datasource';
import { Section } from '../../core/section';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-section-list',
templateUrl: './section-list.component.html',
styleUrls: ['./section-list.component.css']
})
export class SectionListComponent implements OnInit {
dataSource: SectionListDataSource;
list: Section[];
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['name'];
constructor(private route: ActivatedRoute) {
}
ngOnInit() {
this.route.data
.subscribe((data: { list: Section[] }) => {
this.list = data.list;
});
this.dataSource = new SectionListDataSource(this.list);
}
}

View File

@ -0,0 +1,15 @@
import {inject, TestBed} from '@angular/core/testing';
import {SectionResolver} from './section-resolver.service';
describe('SectionResolver', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SectionResolver]
});
});
it('should be created', inject([SectionResolver], (service: SectionResolver) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,19 @@
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot} from '@angular/router';
import {SectionService} from './section.service';
import {Section} from '../core/section';
import {Observable} from 'rxjs/internal/Observable';
@Injectable({
providedIn: 'root'
})
export class SectionResolver implements Resolve<Section> {
constructor(private ser: SectionService, private router: Router) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Section> {
const id = route.paramMap.get('id');
return this.ser.get(id);
}
}

View File

@ -0,0 +1,15 @@
import {inject, TestBed} from '@angular/core/testing';
import {SectionService} from './section.service';
describe('SectionService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SectionService]
});
});
it('should be created', inject([SectionService], (service: SectionService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -0,0 +1,65 @@
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {ErrorLoggerService} from '../core/error-logger.service';
import {catchError} from 'rxjs/operators';
import {Observable} from 'rxjs/internal/Observable';
import {Section} from '../core/section';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
const url = '/v1/sections';
const serviceName = 'SectionService';
@Injectable({
providedIn: 'root'
})
export class SectionService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {
}
get(id: string): Observable<Section> {
const getUrl: string = (id === null) ? `${url}/new` : `${url}/${id}`;
return <Observable<Section>>this.http.get<Section>(getUrl)
.pipe(
catchError(this.log.handleError(serviceName, `get id=${id}`))
);
}
list(): Observable<Section[]> {
const options = {params: new HttpParams().set('l', '')};
return <Observable<Section[]>>this.http.get<Section[]>(url, options)
.pipe(
catchError(this.log.handleError(serviceName, 'list'))
);
}
save(section: Section): Observable<Section> {
return <Observable<Section>>this.http.post<Section>(`${url}/new`, section, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'save'))
);
}
update(section: Section): Observable<Section> {
return <Observable<Section>>this.http.put<Section>(`${url}/${section.id}`, section, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'update'))
);
}
saveOrUpdate(section: Section): Observable<Section> {
if (!section.id) {
return this.save(section);
} else {
return this.update(section);
}
}
delete(id: string): Observable<Section> {
return <Observable<Section>>this.http.delete<Section>(`${url}/${id}`, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'delete'))
);
}
}

View File

@ -0,0 +1,13 @@
import {SectionsRoutingModule} from './sections-routing.module';
describe('SectionsRoutingModule', () => {
let sectionsRoutingModule: SectionsRoutingModule;
beforeEach(() => {
sectionsRoutingModule = new SectionsRoutingModule();
});
it('should create an instance', () => {
expect(sectionsRoutingModule).toBeTruthy();
});
});

View File

@ -0,0 +1,61 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule, Routes} from '@angular/router';
import {SectionListResolver} from './section-list-resolver.service';
import {SectionResolver} from './section-resolver.service';
import {SectionListComponent} from './section-list/section-list.component';
import {SectionDetailComponent} from './section-detail/section-detail.component';
import {AuthGuard} from '../auth/auth-guard.service';
const sectionsRoutes: Routes = [
{
path: '',
component: SectionListComponent,
canActivate: [AuthGuard],
data: {
permission: 'Sections'
},
resolve: {
list: SectionListResolver
}
},
{
path: 'new',
component: SectionDetailComponent,
canActivate: [AuthGuard],
data: {
permission: 'Sections'
},
resolve: {
item: SectionResolver
}
},
{
path: ':id',
component: SectionDetailComponent,
canActivate: [AuthGuard],
data: {
permission: 'Sections'
},
resolve: {
item: SectionResolver
}
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(sectionsRoutes)
],
exports: [
RouterModule
],
providers: [
SectionListResolver,
SectionResolver
]
})
export class SectionsRoutingModule {
}

View File

@ -0,0 +1,13 @@
import {SectionsModule} from './sections.module';
describe('SectionsModule', () => {
let sectionsModule: SectionsModule;
beforeEach(() => {
sectionsModule = new SectionsModule();
});
it('should create an instance', () => {
expect(sectionsModule).toBeTruthy();
});
});

View File

@ -0,0 +1,37 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SectionListComponent } from './section-list/section-list.component';
import { SectionDetailComponent } from './section-detail/section-detail.component';
import { SectionsRoutingModule } from './sections-routing.module';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatTableModule } from '@angular/material/table';
import { CdkTableModule } from '@angular/cdk/table';
import { ReactiveFormsModule } from '@angular/forms';
import { FlexLayoutModule } from '@angular/flex-layout';
@NgModule({
imports: [
CommonModule,
CdkTableModule,
FlexLayoutModule,
MatButtonModule,
MatCardModule,
MatIconModule,
MatInputModule,
MatProgressSpinnerModule,
MatTableModule,
ReactiveFormsModule,
SectionsRoutingModule
],
declarations: [
SectionListComponent,
SectionDetailComponent
]
})
export class SectionsModule {
}