SectionPrinter is now working
Migration:
Section, Printers, SectionPrinters working
This commit is contained in:
Amritanshu
2019-06-25 16:44:59 +05:30
parent 142a0f5a8a
commit 7b08fe611f
27 changed files with 546 additions and 30 deletions

View File

@ -0,0 +1,18 @@
import { DataSource } from '@angular/cdk/collections';
import { Observable } from 'rxjs';
import { SectionPrinterItem } from "../core/section-printer";
export class SectionPrinterDataSource extends DataSource<SectionPrinterItem> {
constructor(private data: Observable<SectionPrinterItem[]>) {
super();
}
connect(): Observable<SectionPrinterItem[]> {
return this.data;
}
disconnect() {
}
}

View File

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

View File

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

View File

@ -0,0 +1,3 @@
.example-card {
max-width: 400px;
}

View File

@ -0,0 +1,65 @@
<div fxLayout="row" fxFlex="50%" fxLayoutAlign="space-around center" class="example-card">
<mat-card fxFlex>
<mat-card-title-group>
<mat-card-title>Printers for 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>Section</mat-label>
<mat-select placeholder="Section" formControlName="section" (selectionChange)="show($event)">
<mat-option *ngFor="let s of sections" [value]="s.id">
{{ s.name }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
<mat-divider></mat-divider>
<mat-table #table [dataSource]="dataSource" formArrayName="menuCategories">
<!-- Menu Category Column -->
<ng-container matColumnDef="menuCategory">
<mat-header-cell *matHeaderCellDef>Menu Category</mat-header-cell>
<mat-cell *matCellDef="let row">{{row.menuCategory.name}}</mat-cell>
</ng-container>
<!-- Printer Column -->
<ng-container matColumnDef="printer">
<mat-header-cell *matHeaderCellDef>Printer</mat-header-cell>
<mat-cell *matCellDef="let row; let i = index" [formGroupName]="i" fxFlex>
<mat-form-field>
<mat-label>Printer</mat-label>
<mat-select formControlName="printer" name="printer">
<mat-option>-- Default --</mat-option>
<mat-option *ngFor="let p of printers" [value]="p.id">
{{ p.name }}
</mat-option>
</mat-select>
</mat-form-field>
</mat-cell>
</ng-container>
<!-- Copies Column -->
<ng-container matColumnDef="copies">
<mat-header-cell *matHeaderCellDef>Copies</mat-header-cell>
<mat-cell *matCellDef="let row; let i = index" [formGroupName]="i" fxFlex>
<mat-form-field *ngIf="!!form.get('menuCategories').at(i).value.printer">
<mat-label>Copies</mat-label>
<input matInput type="number" placeholder="Copies" formControlName="copies">
</mat-form-field>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; let i = index; columns: displayedColumns;"></mat-row>
</mat-table>
</form>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary" (click)="save()">Save</button>
<button mat-raised-button color="warn" *ngIf="id" (click)="confirmDelete()">Delete</button>
</mat-card-actions>
</mat-card>
</div>

View File

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

View File

@ -0,0 +1,128 @@
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router} from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { BehaviorSubject, Observable } from "rxjs";
import { map } from 'rxjs/operators';
import { SectionPrinter } from '../core/section-printer';
import { ToasterService } from '../core/toaster.service';
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
import { Section } from "../core/section";
import { Printer } from "../core/printer";
import { SectionPrinterService } from './section-printer.service';
import { SectionPrinterDataSource } from "./section-printer-datasource";
@Component({
selector: 'app-section-printer',
templateUrl: './section-printer.component.html',
styleUrls: ['./section-printer.component.css']
})
export class SectionPrinterComponent implements OnInit {
@ViewChild('section', {static: true}) sectionElement: ElementRef;
form: FormGroup;
dataSource: SectionPrinterDataSource;
public itemObservable = new BehaviorSubject<SectionPrinter>(new SectionPrinter());
item: SectionPrinter;
sections: Section[];
printers: Printer[];
displayedColumns = ['menuCategory', 'printer', 'copies'];
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private toaster: ToasterService,
private dialog: MatDialog,
private ser: SectionPrinterService
) {
this.createForm();
}
createForm() {
this.form = this.fb.group({
section: '',
menuCategories: this.fb.array([])
});
}
ngOnInit() {
this.route.data
.subscribe((data: { item: SectionPrinter, sections: Section[], printers: Printer[] }) => {
this.sections = data.sections;
this.printers = data.printers;
this.showItem(data.item);
this.dataSource = new SectionPrinterDataSource(this.itemObservable.pipe(map(p => p.menuCategories)));
this.itemObservable.next(this.item);
});
}
showItem(item: SectionPrinter) {
this.item = item;
this.form.get('section').setValue(this.item.id);
this.form.setControl('menuCategories', this.fb.array(
this.item.menuCategories.map(
x => this.fb.group({
menuCategory: x.menuCategory.name,
printer: x.printer.id,
copies: "" + x.copies
})
)
));
}
save() {
this.ser.save(this.getItem())
.subscribe(
(result) => {
this.toaster.show('Success', '');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
delete() {
this.ser.delete(this.item.id)
.subscribe(
(result) => {
this.toaster.show('Success', '');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Delete all printers for this section?', content: 'Are you sure? This cannot be undone.'}
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): SectionPrinter {
const formModel = this.form.value;
this.item.id = formModel.section;
const array = this.form.get('menuCategories') as FormArray;
this.item.menuCategories.forEach((item, index) => {
const cont = array.controls[index].value;
item.printer.id = cont.printer;
item.copies = +cont.copies;
});
return this.item;
}
show(val: any) {
this.router.navigate(['/section-printers', val.value]);
}
}

View File

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

View File

@ -0,0 +1,50 @@
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 {SectionPrinter, SectionPrinterItem} from '../core/section-printer';
const httpOptions = {
headers: new HttpHeaders({'Content-Type': 'application/json'})
};
const url = '/v1/section-printers';
const serviceName = 'SectionPrinterService';
@Injectable({
providedIn: 'root'
})
export class SectionPrinterService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {
}
get(id: string): Observable<SectionPrinter> {
const getUrl: string = (id === null) ? `${url}` : `${url}/${id}`;
return <Observable<SectionPrinter>>this.http.get<SectionPrinter>(getUrl)
.pipe(
catchError(this.log.handleError(serviceName, `get id=${id}`))
);
}
item(id: string, menuCategoryId: string): Observable<SectionPrinterItem> {
const options = {params: new HttpParams().set('m', menuCategoryId)};
return <Observable<SectionPrinterItem>>this.http.get<SectionPrinterItem>(`${url}/${id}`, options)
.pipe(
catchError(this.log.handleError(serviceName, 'list'))
);
}
save(item: SectionPrinter): Observable<SectionPrinter> {
return <Observable<SectionPrinter>>this.http.post<SectionPrinter>(`${url}/${item.id}`, item, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'save'))
);
}
delete(id: string): Observable<SectionPrinter> {
return <Observable<SectionPrinter>>this.http.delete<SectionPrinter>(`${url}/${id}`, httpOptions)
.pipe(
catchError(this.log.handleError(serviceName, 'delete'))
);
}
}

View File

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

View File

@ -0,0 +1,53 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {RouterModule, Routes} from '@angular/router';
import {SectionPrinterResolver} from './section-printer-resolver.service';
import {SectionPrinterComponent} from './section-printer.component';
import {AuthGuard} from '../auth/auth-guard.service';
import {SectionListResolver} from "../sections/section-list-resolver.service";
import {PrinterListResolver} from "../printers/printer-list-resolver.service";
const sectionPrinterRoutes: Routes = [
{
path: '',
component: SectionPrinterComponent,
canActivate: [AuthGuard],
data: {
permission: 'Users'
},
resolve: {
item: SectionPrinterResolver,
sections: SectionListResolver,
printers: PrinterListResolver
}
},
{
path: ':id',
component: SectionPrinterComponent,
canActivate: [AuthGuard],
data: {
permission: 'Users'
},
resolve: {
item: SectionPrinterResolver,
sections: SectionListResolver,
printers: PrinterListResolver
}
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(sectionPrinterRoutes)
],
exports: [
RouterModule
],
providers: [
SectionPrinterResolver
]
})
export class SectionPrintersRoutingModule {
}

View File

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

View File

@ -0,0 +1,43 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import { SectionPrinterComponent } from './section-printer.component';
import { SectionPrintersRoutingModule } from './section-printers-routing.module';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatDividerModule } from '@angular/material/divider';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSelectModule } from '@angular/material/select';
import { MatTableModule } from '@angular/material/table';
import { CdkTableModule } from '@angular/cdk/table';
import { ReactiveFormsModule } from '@angular/forms';
import { SharedModule } from '../shared/shared.module';
import { FlexLayoutModule } from '@angular/flex-layout';
@NgModule({
imports: [
CommonModule,
CdkTableModule,
FlexLayoutModule,
MatButtonModule,
MatCardModule,
MatCheckboxModule,
MatDividerModule,
MatIconModule,
MatInputModule,
MatProgressSpinnerModule,
MatSelectModule,
MatTableModule,
ReactiveFormsModule,
SharedModule,
SectionPrintersRoutingModule
],
declarations: [
SectionPrinterComponent
]
})
export class SectionPrintersModule {
}