Allow editing of product version with the right permission.
This commit is contained in:
@ -5,6 +5,7 @@ import { Tax } from './tax';
|
||||
|
||||
export class Product {
|
||||
id: string | undefined;
|
||||
versionId?: string;
|
||||
code: number;
|
||||
name: string;
|
||||
units: string;
|
||||
@ -20,6 +21,9 @@ export class Product {
|
||||
enabled: boolean;
|
||||
tax: Tax;
|
||||
|
||||
validFrom?: string;
|
||||
validTill?: string;
|
||||
|
||||
public constructor(init?: Partial<Product>) {
|
||||
this.id = undefined;
|
||||
this.code = 0;
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
.mat-radio-button ~ .mat-radio-button {
|
||||
margin-left: 16px;
|
||||
}
|
||||
|
||||
@ -1,95 +1,111 @@
|
||||
<div fxLayout="row" fxFlex="50%" fxLayoutAlign="space-around center" class="example-card">
|
||||
<mat-card fxFlex>
|
||||
<mat-card-title-group>
|
||||
<mat-card-title>Product</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>Code</mat-label>
|
||||
<input matInput placeholder="Code" formControlName="code" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex="75">
|
||||
<mat-label>Name</mat-label>
|
||||
<input matInput #name placeholder="Name" formControlName="name" />
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="25">
|
||||
<mat-label>Units</mat-label>
|
||||
<input matInput placeholder="Units" formControlName="units" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Price</mat-label>
|
||||
<input matInput type="number" placeholder="Price" formControlName="price" />
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Quantity</mat-label>
|
||||
<input matInput type="number" placeholder="Quantity" formControlName="quantity" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-checkbox formControlName="hasHappyHour">Has Happy Hour?</mat-checkbox>
|
||||
<mat-checkbox formControlName="isNotAvailable">Is Not Available?</mat-checkbox>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Menu Category</mat-label>
|
||||
<mat-select placeholder="Menu Category" formControlName="menuCategory">
|
||||
<mat-option *ngFor="let mc of menuCategories" [value]="mc.id">
|
||||
{{ mc.name }}
|
||||
</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Sale Category</mat-label>
|
||||
<mat-select placeholder="Sale Category" formControlName="saleCategory">
|
||||
<mat-option *ngFor="let sc of saleCategories" [value]="sc.id">
|
||||
{{ sc.name }}
|
||||
</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
</form>
|
||||
</mat-card-content>
|
||||
<mat-card-actions>
|
||||
<button mat-raised-button color="primary" (click)="save()">Save</button>
|
||||
<button mat-raised-button color="warn" (click)="confirmDelete()" *ngIf="!!item.id">
|
||||
Delete
|
||||
</button>
|
||||
</mat-card-actions>
|
||||
</mat-card>
|
||||
<div fxLayout="column">
|
||||
<div fxLayout="row" fxFlex="50%" fxLayoutAlign="space-around center" class="example-card">
|
||||
<mat-card fxFlex>
|
||||
<mat-card-title-group>
|
||||
<mat-card-title>Product</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>Code</mat-label>
|
||||
<input matInput placeholder="Code" formControlName="code" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex="75">
|
||||
<mat-label>Name</mat-label>
|
||||
<input matInput #name placeholder="Name" formControlName="name" />
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex="25">
|
||||
<mat-label>Units</mat-label>
|
||||
<input matInput placeholder="Units" formControlName="units" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Price</mat-label>
|
||||
<input matInput type="number" placeholder="Price" formControlName="price" />
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Quantity</mat-label>
|
||||
<input matInput type="number" placeholder="Quantity" formControlName="quantity" />
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-checkbox formControlName="hasHappyHour">Has Happy Hour?</mat-checkbox>
|
||||
<mat-checkbox formControlName="isNotAvailable">Is Not Available?</mat-checkbox>
|
||||
</div>
|
||||
<div
|
||||
fxLayout="row"
|
||||
fxLayoutAlign="space-around start"
|
||||
fxLayout.lt-md="column"
|
||||
fxLayoutGap="20px"
|
||||
fxLayoutGap.lt-md="0px"
|
||||
>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Menu Category</mat-label>
|
||||
<mat-select placeholder="Menu Category" formControlName="menuCategory">
|
||||
<mat-option *ngFor="let mc of menuCategories" [value]="mc.id">
|
||||
{{ mc.name }}
|
||||
</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
<mat-form-field fxFlex>
|
||||
<mat-label>Sale Category</mat-label>
|
||||
<mat-select placeholder="Sale Category" formControlName="saleCategory">
|
||||
<mat-option *ngFor="let sc of saleCategories" [value]="sc.id">
|
||||
{{ sc.name }}
|
||||
</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
</form>
|
||||
</mat-card-content>
|
||||
<mat-card-actions>
|
||||
<button mat-raised-button color="primary" (click)="save()">Save</button>
|
||||
<button mat-raised-button color="warn" (click)="confirmDelete()" *ngIf="!!item.id">
|
||||
Delete
|
||||
</button>
|
||||
</mat-card-actions>
|
||||
</mat-card>
|
||||
</div>
|
||||
<div fxLayout="row" fxFlex="50%" fxLayoutAlign="space-around center" class="example-card">
|
||||
<mat-radio-group [hidden]="this.list.length === 1">
|
||||
<mat-radio-button
|
||||
class="example-radio-button"
|
||||
*ngFor="let product of list"
|
||||
[value]="this.product.versionId"
|
||||
(change)="loadProduct($event)"
|
||||
[checked]="this.item.versionId === product.versionId"
|
||||
>
|
||||
{{ !!product.validFrom ? product.validFrom : '\u221E' }} -
|
||||
{{ !!product.validTill ? product.validTill : '\u221E' }}
|
||||
</mat-radio-button>
|
||||
</mat-radio-group>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { MatRadioChange } from '@angular/material/radio';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
|
||||
import { MenuCategory } from '../../core/menu-category';
|
||||
@ -21,6 +22,7 @@ export class ProductDetailComponent implements OnInit, AfterViewInit {
|
||||
menuCategories: MenuCategory[] = [];
|
||||
saleCategories: SaleCategory[] = [];
|
||||
item: Product = new Product();
|
||||
list: Product[] = [];
|
||||
|
||||
constructor(
|
||||
private route: ActivatedRoute,
|
||||
@ -47,13 +49,14 @@ export class ProductDetailComponent implements OnInit, AfterViewInit {
|
||||
ngOnInit() {
|
||||
this.route.data.subscribe((value) => {
|
||||
const data = value as {
|
||||
item: Product;
|
||||
items: Product[];
|
||||
menuCategories: MenuCategory[];
|
||||
saleCategories: SaleCategory[];
|
||||
};
|
||||
this.menuCategories = data.menuCategories;
|
||||
this.saleCategories = data.saleCategories;
|
||||
this.showItem(data.item);
|
||||
this.list = data.items;
|
||||
this.showItem(this.list[this.list.length - 1]);
|
||||
});
|
||||
}
|
||||
|
||||
@ -93,7 +96,7 @@ export class ProductDetailComponent implements OnInit, AfterViewInit {
|
||||
}
|
||||
|
||||
delete() {
|
||||
this.ser.delete(this.item.id as string).subscribe(
|
||||
this.ser.delete(this.item.versionId as string).subscribe(
|
||||
() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigateByUrl('/products');
|
||||
@ -135,4 +138,9 @@ export class ProductDetailComponent implements OnInit, AfterViewInit {
|
||||
this.item.quantity = +formModel.quantity;
|
||||
return this.item;
|
||||
}
|
||||
|
||||
loadProduct($event: MatRadioChange) {
|
||||
const product = this.list.find((x) => x.versionId === $event.value);
|
||||
this.showItem(product as Product);
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,10 +9,10 @@ import { ProductService } from './product.service';
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class ProductResolver implements Resolve<Product> {
|
||||
export class ProductResolver implements Resolve<Product[]> {
|
||||
constructor(private ser: ProductService) {}
|
||||
|
||||
resolve(route: ActivatedRouteSnapshot): Observable<Product> {
|
||||
resolve(route: ActivatedRouteSnapshot): Observable<Product[]> {
|
||||
const id = route.paramMap.get('id');
|
||||
return this.ser.get(id);
|
||||
}
|
||||
|
||||
@ -17,11 +17,11 @@ const serviceName = 'ProductService';
|
||||
export class ProductService {
|
||||
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
|
||||
|
||||
get(id: string | null): Observable<Product> {
|
||||
get(id: string | null): Observable<Product[]> {
|
||||
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
|
||||
return this.http
|
||||
.get<Product>(getUrl)
|
||||
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Product>;
|
||||
.get<Product[]>(getUrl)
|
||||
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Product[]>;
|
||||
}
|
||||
|
||||
list(): Observable<Product[]> {
|
||||
@ -48,16 +48,16 @@ export class ProductService {
|
||||
>;
|
||||
}
|
||||
|
||||
save(product: Product): Observable<Product> {
|
||||
save(product: Product): Observable<void> {
|
||||
return this.http
|
||||
.post<Product>(`${url}`, product, httpOptions)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<Product>;
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<void>;
|
||||
}
|
||||
|
||||
update(product: Product): Observable<Product> {
|
||||
update(product: Product): Observable<void> {
|
||||
return this.http
|
||||
.put<Product>(`${url}/${product.id}`, product, httpOptions)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<Product>;
|
||||
.put<Product>(`${url}/${product.versionId}`, product, httpOptions)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<void>;
|
||||
}
|
||||
|
||||
updateSortOrder(list: Product[]): Observable<Product[]> {
|
||||
@ -68,17 +68,17 @@ export class ProductService {
|
||||
>;
|
||||
}
|
||||
|
||||
saveOrUpdate(product: Product): Observable<Product> {
|
||||
if (!product.id) {
|
||||
saveOrUpdate(product: Product): Observable<void> {
|
||||
if (!product.versionId) {
|
||||
return this.save(product);
|
||||
}
|
||||
return this.update(product);
|
||||
}
|
||||
|
||||
delete(id: string): Observable<Product> {
|
||||
delete(id: string): Observable<void> {
|
||||
return this.http
|
||||
.delete<Product>(`${url}/${id}`, httpOptions)
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Product>;
|
||||
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<void>;
|
||||
}
|
||||
|
||||
balance(id: string, date: string): Observable<number> {
|
||||
|
||||
@ -32,7 +32,7 @@ const productsRoutes: Routes = [
|
||||
permission: 'Products',
|
||||
},
|
||||
resolve: {
|
||||
item: ProductResolver,
|
||||
items: ProductResolver,
|
||||
menuCategories: MenuCategoryListResolver,
|
||||
saleCategories: SaleCategoryListResolver,
|
||||
},
|
||||
@ -45,7 +45,7 @@ const productsRoutes: Routes = [
|
||||
permission: 'Products',
|
||||
},
|
||||
resolve: {
|
||||
item: ProductResolver,
|
||||
items: ProductResolver,
|
||||
menuCategories: MenuCategoryListResolver,
|
||||
saleCategories: SaleCategoryListResolver,
|
||||
},
|
||||
|
||||
@ -11,6 +11,7 @@ import { MatOptionModule } from '@angular/material/core';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
||||
import { MatRadioModule } from '@angular/material/radio';
|
||||
import { MatSelectModule } from '@angular/material/select';
|
||||
import { MatTableModule } from '@angular/material/table';
|
||||
|
||||
@ -35,6 +36,7 @@ import { ProductsRoutingModule } from './products-routing.module';
|
||||
MatCheckboxModule,
|
||||
ReactiveFormsModule,
|
||||
ProductsRoutingModule,
|
||||
MatRadioModule,
|
||||
],
|
||||
declarations: [ProductListComponent, ProductDetailComponent],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user