Initial commit for the Angular part. We are nowhere yet.

This commit is contained in:
Amritanshu
2019-06-14 00:32:34 +05:30
parent 1a1fa7881d
commit d59c60e81d
123 changed files with 3748 additions and 374 deletions

View File

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

View File

@ -0,0 +1,27 @@
.full-width {
width: 100%;
}
.shipping-card {
min-width: 120px;
margin: 20px auto;
}
.mat-radio-button {
display: block;
margin: 5px 0;
}
.row {
display: flex;
flex-direction: row;
}
.col {
flex: 1;
margin-right: 20px;
}
.col:last-child {
margin-right: 0;
}

View File

@ -0,0 +1,51 @@
<form [formGroup]="form" novalidate (ngSubmit)="onSubmit()">
<mat-card class="shipping-card">
<mat-card-header>
<mat-card-title>Shipping Information</mat-card-title>
</mat-card-header>
<mat-card-content>
<div class="row">
<div class="col">
<mat-form-field class="full-width">
<input matInput placeholder="Company" formControlName="company">
</mat-form-field>
</div>
</div>
<div class="row">
<div class="col">
<mat-form-field class="full-width">
<input matInput placeholder="Name" formControlName="name">
<mat-error *ngIf="form.controls['name'].hasError('required')">
Name is <strong>required</strong>
</mat-error>
</mat-form-field>
</div>
</div>
<div class="row">
<div class="col">
<mat-form-field class="full-width">
<textarea matInput placeholder="Address" formControlName="address"></textarea>
</mat-form-field>
</div>
</div>
<div class="row">
<div class="col">
<mat-form-field class="full-width">
<input matInput #phone placeholder="Phone" type="number" formControlName="phone">
<mat-hint align="end">{{phone.value.length}} / 10</mat-hint>
</mat-form-field>
</div>
</div>
<div class="row">
<div class="col">
<mat-form-field class="full-width">
<input matInput placeholder="Pax" type="number" formControlName="pax">
</mat-form-field>
</div>
</div>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary" type="submit">Submit</button>
</mat-card-actions>
</mat-card>
</form>

View File

@ -0,0 +1,78 @@
import {Component, OnInit} from '@angular/core';
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
import {RoleService} from "../../role/role.service";
import {GuestBookService} from "../guest-book.service";
import {ToasterService} from "../../core/toaster.service";
import {Role} from "../../role/role";
import {GuestBook} from "../guest-book";
import {ActivatedRoute, Router} from "@angular/router";
import {User} from "../../user/user";
@Component({
selector: 'app-guest-book-detail',
templateUrl: './guest-book-detail.component.html',
styleUrls: ['./guest-book-detail.component.css']
})
export class GuestBookDetailComponent implements OnInit {
form: FormGroup;
item: GuestBook;
constructor(
private fb: FormBuilder,
private route: ActivatedRoute,
private router: Router,
private toaster: ToasterService,
private ser: GuestBookService
) {
this.createForm();
}
createForm() {
this.form = this.fb.group({
company: null,
name: [null, Validators.required],
phone: [null, Validators.required],
pax: ['0', Validators.required],
address: null
});
}
ngOnInit() {
this.route.data
.subscribe((data: { item: GuestBook }) => {
this.showItem(data.item);
});
}
showItem(item: GuestBook) {
this.item = item;
this.form.get('company').setValue(item.company);
this.form.get('name').setValue(item.name);
this.form.get('phone').setValue(item.phone);
this.form.get('pax').setValue('' + item.pax);
this.form.get('address').setValue(item.address);
}
onSubmit() {
this.ser.saveOrUpdate(this.getItem())
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/guest-book/list');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
getItem(): GuestBook {
const formModel = this.form.value;
this.item.company = formModel.company;
this.item.name = formModel.name;
this.item.phone = formModel.phone;
this.item.pax = parseInt(formModel.pax);
this.item.address = formModel.address;
return this.item;
}
}

View File

@ -0,0 +1,18 @@
import {Injectable} from '@angular/core';
import {ActivatedRouteSnapshot, Resolve, RouterStateSnapshot} from '@angular/router';
import {GuestBook} from "./guest-book";
import {Observable} from 'rxjs/internal/Observable';
import {GuestBookService} from './guest-book.service';
@Injectable({
providedIn: 'root'
})
export class GuestBookListResolver implements Resolve<GuestBook[]> {
constructor(private ser: GuestBookService) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<GuestBook[]> {
return this.ser.list(null);
}
}

View File

@ -0,0 +1,74 @@
import {DataSource} from '@angular/cdk/collections';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import {map, tap} from 'rxjs/operators';
import {Observable, of as observableOf, merge} from 'rxjs';
import {GuestBook} from "../guest-book";
/**
* Data source for the GuestBookList view. This class should
* encapsulate all logic for fetching and manipulating the displayed data
* (including sorting, pagination, and filtering).
*/
export class GuestBookListDataSource extends DataSource<GuestBook> {
private dataObservable: Observable<GuestBook[]>;
constructor(
private paginator: MatPaginator,
private sort: MatSort,
public data: GuestBook[]
) {
super();
}
connect(): Observable<GuestBook[]> {
this.dataObservable = observableOf(this.data);
const dataMutations = [
this.dataObservable,
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(
map((x: any) => {
return this.getPagedData(this.getSortedData([...this.data]));
}),
tap((x: GuestBook[]) => this.paginator.length = x.length)
);
}
/**
* Called when the table is being destroyed. Use this function, to clean up
* any open connections or free any held resources that were set up during connect.
*/
disconnect() {
}
private getPagedData(data: GuestBook[]) {
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
return data.splice(startIndex, this.paginator.pageSize);
}
private getSortedData(data: GuestBook[]) {
if (!this.sort.active || this.sort.direction === '') {
return data;
}
return data.sort((a, b) => {
const isAsc = this.sort.direction === 'asc';
switch (this.sort.active) {
case 'name':
return compare(a.name, b.name, isAsc);
case 'id':
return compare(+a.id, +b.id, isAsc);
default:
return 0;
}
});
}
}
function compare(a, b, isAsc) {
return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

View File

@ -0,0 +1,3 @@
.full-width-table {
width: 100%;
}

View File

@ -0,0 +1,70 @@
<div class="mat-elevation-z8">
<form [formGroup]="form" fxLayout="column">
<div fxLayout="row" fxLayout.lt-md="column" fxLayoutGap="20px" fxLayoutGap.lt-md="0px">
<mat-form-field fxFlex>
<input matInput [matDatepicker]="date" (focus)="date.open()" placeholder="Date" formControlName="date"
autocomplete="off">
<mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
<mat-datepicker #date></mat-datepicker>
</mat-form-field>
<div fxFlex>
<a mat-button [routerLink]="['/guest-book', 'new']">
<mat-icon>add_box</mat-icon>
Add
</a>
</div>
</div>
</form>
<table mat-table class="full-width-table" [dataSource]="dataSource" matSort aria-label="Elements">
<!-- Id Column -->
<ng-container matColumnDef="sno">
<th mat-header-cell *matHeaderCellDef mat-sort-header>S. No</th>
<td mat-cell *matCellDef="let row">{{row.serial}}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Name</th>
<td mat-cell *matCellDef="let row">{{row.name}}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="phone">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Phone</th>
<td mat-cell *matCellDef="let row">{{row.phone}}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="pax">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Pax</th>
<td mat-cell *matCellDef="let row">{{row.pax}}</td>
</ng-container>
<!-- Action Column -->
<ng-container matColumnDef="action">
<mat-header-cell *matHeaderCellDef class="center">Action</mat-header-cell>
<mat-cell *matCellDef="let row" class="center">
<button mat-icon-button (click)="seat(row)">
Seat
</button>
<button mat-icon-button (click)="editRow(row)">
<mat-icon>edit</mat-icon>
</button>
<button mat-icon-button color="warn" (click)="deleteRow(row)">
<mat-icon>delete</mat-icon>
</button>
</mat-cell>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
<mat-paginator #paginator
[length]="dataSource.data.length"
[pageIndex]="0"
[pageSize]="250"
[pageSizeOptions]="[25, 50, 100, 250]">
</mat-paginator>
</div>

View File

@ -0,0 +1,58 @@
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import {GuestBookListDataSource} from './guest-book-list-datasource';
import {ActivatedRoute} from "@angular/router";
import {FormBuilder, FormGroup} from "@angular/forms";
import {debounceTime, distinctUntilChanged, map} from "rxjs/operators";
import {GuestBook} from "../guest-book";
import {Observable} from "rxjs";
import * as moment from 'moment';
import {GuestBookService} from "../guest-book.service";
@Component({
selector: 'app-guest-book-list',
templateUrl: './guest-book-list.component.html',
styleUrls: ['./guest-book-list.component.css']
})
export class GuestBookListComponent implements OnInit {
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
@ViewChild(MatSort, { static: true }) sort: MatSort;
dataSource: GuestBookListDataSource;
filter: Observable<string>;
form: FormGroup;
list: GuestBook[];
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['sno', 'name', 'phone', 'pax', 'action'];
constructor(private route: ActivatedRoute, private fb: FormBuilder, private ser: GuestBookService) {
this.createForm();
this.listenToDateChange();
}
createForm() {
this.form = this.fb.group({
date: ''
});
}
listenToDateChange(): void {
this.form.get('date').valueChanges.pipe(
map(x => moment(x).format('DD-MMM-YYYY'))
).subscribe(x => {
return this.ser.list(x)
.subscribe((list: GuestBook[]) => {
this.list = list;
this.dataSource = new GuestBookListDataSource(this.paginator, this.sort, this.list);
});
});
}
ngOnInit() {
this.route.data
.subscribe((data: { list: GuestBook[] }) => {
this.list = data.list;
});
this.dataSource = new GuestBookListDataSource(this.paginator, this.sort, this.list);
}
}

View File

@ -0,0 +1,54 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { GuestBookListComponent } from "./guest-book-list/guest-book-list.component";
import { GuestBookDetailComponent } from "./guest-book-detail/guest-book-detail.component";
import { AuthGuard } from "../auth/auth-guard.service";
import { GuestBookListResolver } from "./guest-book-list-resolver.service";
import { GuestBookDetailResolver } from "./guest-book-detail-resolver.service";
const routes: Routes = [
{
path: '',
component: GuestBookListComponent,
canActivate: [AuthGuard],
data: {
permission: 'Guest Book'
},
resolve: {
list: GuestBookListResolver
}
},
{
path: 'new',
component: GuestBookDetailComponent,
canActivate: [AuthGuard],
data: {
permission: 'Guest Book'
},
resolve: {
item: GuestBookDetailResolver
}
},
{
path: ':id',
component: GuestBookDetailComponent,
canActivate: [AuthGuard],
data: {
permission: 'Guest Book'
},
resolve: {
item: GuestBookDetailResolver
}
}
];
@NgModule({
declarations: [],
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
providers: [
GuestBookListResolver,
GuestBookDetailResolver
]
})
export class GuestBookRoutingModule { }

View File

@ -0,0 +1,55 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatNativeDateModule, MAT_DATE_FORMATS, MAT_DATE_LOCALE, DateAdapter } from '@angular/material/core';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatRadioModule } from '@angular/material/radio';
import { MatSelectModule } from '@angular/material/select';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import {MomentDateAdapter} from '@angular/material-moment-adapter';
import { GuestBookDetailComponent } from './guest-book-detail/guest-book-detail.component';
import { ReactiveFormsModule } from '@angular/forms';
import { GuestBookListComponent } from './guest-book-list/guest-book-list.component';
import { GuestBookRoutingModule } from './guest-book-routing.module';
export const MY_FORMATS = {
parse: {
dateInput: 'DD-MMM-YYYY',
},
display: {
dateInput: 'DD-MMM-YYYY',
monthYearLabel: 'MMM YYYY',
dateA11yLabel: 'DD-MMM-YYYY',
monthYearA11yLabel: 'MMM YYYY',
},
};
@NgModule({
declarations: [GuestBookDetailComponent, GuestBookListComponent],
imports: [
CommonModule,
MatTableModule,
MatPaginatorModule,
MatSortModule,
MatInputModule,
MatButtonModule,
MatSelectModule,
MatRadioModule,
MatCardModule,
MatIconModule,
MatDatepickerModule,
MatNativeDateModule,
ReactiveFormsModule,
GuestBookRoutingModule
],
providers: [
{provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]},
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
]
})
export class GuestBookModule { }

View File

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

View File

@ -0,0 +1,13 @@
export class GuestBook {
id: string;
serial: number;
company: string;
name: string;
phone: string;
pax: number;
address: string;
public constructor(init?: Partial<GuestBook>) {
Object.assign(this, init);
}
}