Consolidated commit message to v22 signals

This commit is contained in:
tanshu committed 2026-08-25 14:45:09 +00:00
1 parent 6403d25d3e
commit df289b20b8
443 files changed
+16058 -17332

No files matched your search

@@ -1,14 +1,21 @@
<h2>Cost Center</h2>
<form [formGroup]="form" class="flex-col wrapped">
<div class="row-container">
<mat-form-field class="flex-auto">
<mat-label>Name</mat-label>
<input matInput #nameElement formControlName="name" (keyup.enter)="save()" />
</mat-form-field>
</div>
</form>
@if (itemResource.isLoading()) {
<app-skeleton-loader type="card" [count]="1" />
<app-skeleton-loader type="line" [count]="10" />
} @else if (itemResource.error()) {
<app-error-state [message]="'Failed to load data.'" (retryAction)="itemResource.reload()" />
} @else {
<form class="flex-col wrapped" [formRoot]="form">
<div class="row-container">
<mat-form-field class="flex-auto">
<mat-label>Name</mat-label>
<input matInput [formField]="form.name" (keyup.enter)="save()" />
</mat-form-field>
</div>
</form>
<div class="row-container">
<button mat-raised-button (click)="save()" color="primary">Save</button>
</div>
<div class="row-container">
<button mat-raised-button (click)="save()" color="primary">Save</button>
</div>
}
@@ -1,60 +1,60 @@
import { AfterViewInit, Component, ElementRef, inject, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { Component, inject, afterNextRender, linkedSignal, input, computed } from '@angular/core';
import { form as createForm, FormField, FormRoot } from '@angular/forms/signals';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSnackBar } from '@angular/material/snack-bar';
import { ActivatedRoute, Router } from '@angular/router';
import { Router } from '@angular/router';
import { CostCentre } from '../../core/cost-centre';
import { ErrorStateComponent } from '../../shared/error-state/error-state.component';
import { SkeletonLoaderComponent } from '../../shared/skeleton-loader/skeleton-loader.component';
import { CostCentreService } from '../cost-centre.service';
export interface CostCentreDetailFormData {
name: string;
}
@Component({
selector: 'app-cost-centre-detail',
templateUrl: './cost-centre-detail.component.html',
styleUrls: ['./cost-centre-detail.component.css'],
imports: [ReactiveFormsModule, MatFormFieldModule, MatInputModule, MatButtonModule],
imports: [
FormField,
FormRoot,
MatFormFieldModule,
MatInputModule,
MatButtonModule,
SkeletonLoaderComponent,
ErrorStateComponent,
],
})
export class CostCentreDetailComponent implements OnInit, AfterViewInit {
private route = inject(ActivatedRoute);
export class CostCentreDetailComponent {
id = input(null, { transform: (v: string | null | undefined) => v ?? null });
private router = inject(Router);
private snackBar = inject(MatSnackBar);
private ser = inject(CostCentreService);
@ViewChild('nameElement', { static: true }) nameElement!: ElementRef<HTMLInputElement>;
form: FormGroup<{
name: FormControl<string | null>;
}>;
itemResource = this.ser.get(this.id);
item: CostCentre = new CostCentre();
item = computed(() => this.itemResource.value() ?? new CostCentre());
model = linkedSignal({
source: this.item,
computation: (itemVal): CostCentreDetailFormData => ({
name: itemVal.name ?? '',
}),
});
form = createForm(this.model);
constructor() {
this.form = new FormGroup({
name: new FormControl<string | null>(null),
afterNextRender(() => {
this.form().focusBoundControl();
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { item: CostCentre };
this.showItem(data.item);
});
}
showItem(item: CostCentre) {
this.item = item;
this.form.setValue({
name: this.item.name,
});
}
ngAfterViewInit() {
setTimeout(() => {
this.nameElement.nativeElement.focus();
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe({
next: () => {
@@ -62,14 +62,15 @@ export class CostCentreDetailComponent implements OnInit, AfterViewInit {
this.router.navigateByUrl('/cost-centres');
},
error: (error) => {
this.snackBar.open(error, 'Danger');
this.snackBar.open(error as string, 'Danger');
},
});
}
getItem(): CostCentre {
const formModel = this.form.value;
this.item.name = formModel.name ?? '';
return this.item;
const formModel = this.model();
const item = this.item();
item.name = formModel.name ?? '';
return item;
}
}
@@ -1,18 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { ResolveFn } from '@angular/router';
import { CostCentre } from '../core/cost-centre';
import { costCentreListResolver } from './cost-centre-list.resolver';
describe('costCentreListResolver', () => {
const executeResolver: ResolveFn<CostCentre[]> = (...resolverParameters) =>
TestBed.runInInjectionContext(() => costCentreListResolver(...resolverParameters));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(executeResolver).toBeTruthy();
});
});
@@ -1,9 +0,0 @@
import { inject } from '@angular/core';
import { ResolveFn } from '@angular/router';
import { CostCentre } from '../core/cost-centre';
import { CostCentreService } from './cost-centre.service';
export const costCentreListResolver: ResolveFn<CostCentre[]> = () => {
return inject(CostCentreService).list();
};
@@ -1,69 +0,0 @@
import { DataSource } from '@angular/cdk/collections';
import { EventEmitter } from '@angular/core';
import { MatPaginator, PageEvent } from '@angular/material/paginator';
import { MatSort, Sort } from '@angular/material/sort';
import { merge, Observable, of as observableOf } from 'rxjs';
import { map } from 'rxjs/operators';
import { CostCentre } from '../../core/cost-centre';
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
const compare = (a: string | number, b: string | number, isAsc: boolean) => (a < b ? -1 : 1) * (isAsc ? 1 : -1);
export class CostCentreListDataSource extends DataSource<CostCentre> {
constructor(
public data: CostCentre[],
private paginator?: MatPaginator,
private sort?: MatSort,
) {
super();
}
connect(): Observable<CostCentre[]> {
const dataMutations: (EventEmitter<PageEvent> | EventEmitter<Sort>)[] = [];
if (this.paginator) {
dataMutations.push((this.paginator as MatPaginator).page);
}
if (this.sort) {
dataMutations.push((this.sort as MatSort).sortChange);
}
// Set the paginators length
if (this.paginator) {
this.paginator.length = this.data.length;
}
return merge(observableOf(this.data), ...dataMutations).pipe(
map(() => this.getPagedData(this.getSortedData([...this.data]))),
);
}
disconnect() {}
private getPagedData(data: CostCentre[]) {
if (this.paginator === undefined) {
return data;
}
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
return data.splice(startIndex, this.paginator.pageSize);
}
private getSortedData(data: CostCentre[]) {
if (this.sort === undefined) {
return data;
}
if (!this.sort.active || this.sort.direction === '') {
return data;
}
const sort = this.sort as MatSort;
return data.sort((a, b) => {
const isAsc = sort.direction === 'asc';
switch (sort.active) {
case 'name':
return compare(a.name, b.name, isAsc);
default:
return 0;
}
});
}
}
@@ -6,30 +6,36 @@
</a>
</h2>
<mat-table #table [dataSource]="dataSource" matSort aria-label="Elements">
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell>
<mat-cell *matCellDef="let row"
><a [routerLink]="['/cost-centres', row.id]">{{ row.name }}</a></mat-cell
>
</ng-container>
@if (listResource.isLoading()) {
<app-skeleton-loader type="table" [count]="1" />
} @else if (listResource.error()) {
<app-error-state [message]="'Failed to load list.'" (retryAction)="listResource.reload()" />
} @else {
<mat-table #table [dataSource]="dataSource()" matSort (matSortChange)="sortData($event)">
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef mat-sort-header>Name</mat-header-cell>
<mat-cell *matCellDef="let row"
><a [routerLink]="['/cost-centres', row.id]">{{ row.name }}</a></mat-cell
>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="isFixture">
<mat-header-cell *matHeaderCellDef mat-sort-header>Is Fixture?</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.isFixture }}</mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="isFixture">
<mat-header-cell *matHeaderCellDef mat-sort-header>Is Fixture?</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.isFixture }}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
<mat-paginator
#paginator
[length]="dataSource.data.length"
[pageIndex]="0"
[pageSize]="50"
[pageSizeOptions]="[25, 50, 100, 250]"
>
</mat-paginator>
<mat-paginator
(page)="handlePageEvent($event)"
[length]="list().length"
[pageIndex]="pageIndex()"
[pageSize]="pageSize()"
[pageSizeOptions]="[25, 50, 100, 250]"
>
</mat-paginator>
}
@@ -1,36 +1,77 @@
import { Component, inject, OnInit, ViewChild } from '@angular/core';
import { Component, computed, inject, signal } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatPaginator, MatPaginatorModule } from '@angular/material/paginator';
import { MatSort, MatSortModule } from '@angular/material/sort';
import { MatPaginatorModule, PageEvent } from '@angular/material/paginator';
import { MatSortModule, Sort } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { RouterModule } from '@angular/router';
import { CostCentre } from '../../core/cost-centre';
import { CostCentreListDataSource } from './cost-centre-list-datasource';
import { ErrorStateComponent } from '../../shared/error-state/error-state.component';
import { SkeletonLoaderComponent } from '../../shared/skeleton-loader/skeleton-loader.component';
import { CostCentreService } from '../cost-centre.service';
@Component({
selector: 'app-cost-centre-list',
templateUrl: './cost-centre-list.component.html',
styleUrls: ['./cost-centre-list.component.css'],
imports: [RouterModule, MatIconModule, MatTableModule, MatSortModule, MatPaginatorModule, MatButtonModule],
imports: [
RouterModule,
MatIconModule,
MatTableModule,
MatSortModule,
MatPaginatorModule,
MatButtonModule,
SkeletonLoaderComponent,
ErrorStateComponent,
],
})
export class CostCentreListComponent implements OnInit {
private route = inject(ActivatedRoute);
export class CostCentreListComponent {
private ser = inject(CostCentreService);
pageSize = signal(50);
pageIndex = signal(0);
sortActive = signal('');
sortDirection = signal('');
listResource = this.ser.list();
list = computed(() => this.listResource.value() ?? []);
sortedList = computed(() => {
const data = this.list();
const active = this.sortActive();
const direction = this.sortDirection();
if (!active || direction === '') {
return data;
}
return [...data].sort((a, b) => {
const isAsc = direction === 'asc';
switch (active) {
case 'name':
return compare(a.name, b.name, isAsc);
default:
return 0;
}
});
});
dataSource = computed(() => {
const data = this.sortedList();
const pageIndex = this.pageIndex();
const pageSize = this.pageSize();
return data.slice(pageIndex * pageSize, (pageIndex + 1) * pageSize);
});
@ViewChild(MatPaginator, { static: true }) paginator!: MatPaginator;
@ViewChild(MatSort, { static: true }) sort!: MatSort;
list: CostCentre[] = [];
dataSource: CostCentreListDataSource = new CostCentreListDataSource(this.list, this.paginator, this.sort);
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['name', 'isFixture'];
handlePageEvent(e: PageEvent) {
this.pageSize.set(e.pageSize);
this.pageIndex.set(e.pageIndex);
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { list: CostCentre[] };
this.list = data.list;
});
this.dataSource = new CostCentreListDataSource(this.list, this.paginator, this.sort);
sortData(sort: Sort) {
this.sortActive.set(sort.active);
this.sortDirection.set(sort.direction);
}
}
const compare = (a: string | number | boolean, b: string | number | boolean, isAsc: boolean) =>
(a < b ? -1 : 1) * (isAsc ? 1 : -1);
@@ -1,18 +0,0 @@
import { TestBed } from '@angular/core/testing';
import { ResolveFn } from '@angular/router';
import { CostCentre } from '../core/cost-centre';
import { costCentreResolver } from './cost-centre.resolver';
describe('costCentreResolver', () => {
const executeResolver: ResolveFn<CostCentre> = (...resolverParameters) =>
TestBed.runInInjectionContext(() => costCentreResolver(...resolverParameters));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(executeResolver).toBeTruthy();
});
});
@@ -1,10 +0,0 @@
import { inject } from '@angular/core';
import { ResolveFn } from '@angular/router';
import { CostCentre } from '../core/cost-centre';
import { CostCentreService } from './cost-centre.service';
export const costCentreResolver: ResolveFn<CostCentre> = (route) => {
const id = route.paramMap.get('id');
return inject(CostCentreService).get(id);
};
@@ -2,9 +2,7 @@ import { Routes } from '@angular/router';
import { authGuard } from '../auth/auth-guard.service';
import { CostCentreDetailComponent } from './cost-centre-detail/cost-centre-detail.component';
import { costCentreListResolver } from './cost-centre-list.resolver';
import { CostCentreListComponent } from './cost-centre-list/cost-centre-list.component';
import { costCentreResolver } from './cost-centre.resolver';
export const routes: Routes = [
{
@@ -14,9 +12,6 @@ export const routes: Routes = [
data: {
permission: 'Cost Centres',
},
resolve: {
list: costCentreListResolver,
},
},
{
path: 'new',
@@ -25,9 +20,6 @@ export const routes: Routes = [
data: {
permission: 'Cost Centres',
},
resolve: {
item: costCentreResolver,
},
},
{
path: ':id',
@@ -36,8 +28,5 @@ export const routes: Routes = [
data: {
permission: 'Cost Centres',
},
resolve: {
item: costCentreResolver,
},
},
];
@@ -1,5 +1,5 @@
import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import { HttpClient, httpResource } from '@angular/common/http';
import { inject, Injectable, Signal } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { catchError } from 'rxjs/operators';
@@ -7,8 +7,6 @@ import { CostCentre } from '../core/cost-centre';
import { ErrorLoggerService } from '../core/error-logger.service';
const url = '/api/cost-centres';
const serviceName = 'CostCentreService';
@Injectable({
providedIn: 'root',
})
@@ -16,29 +14,27 @@ export class CostCentreService {
private http = inject(HttpClient);
private log = inject(ErrorLoggerService);
get(id: string | null): Observable<CostCentre> {
const getUrl: string = id === null ? `${url}` : `${url}/${id}`;
return this.http
.get<CostCentre>(getUrl)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<CostCentre>;
get(id: Signal<string | null>) {
return httpResource<CostCentre>(() => {
const getUrl: string = id() === null ? `${url}` : `${url}/${id()}`;
return getUrl;
});
}
list(): Observable<CostCentre[]> {
return this.http
.get<CostCentre[]>(`${url}/list`)
.pipe(catchError(this.log.handleError(serviceName, 'list'))) as Observable<CostCentre[]>;
list() {
return httpResource<CostCentre[]>(() => `${url}/list`);
}
save(costCentre: CostCentre): Observable<CostCentre> {
return this.http
.post<CostCentre>(`${url}`, costCentre)
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<CostCentre>;
.pipe(catchError(this.log.handleError('CostCentreService', 'save'))) as Observable<CostCentre>;
}
update(costCentre: CostCentre): Observable<CostCentre> {
return this.http
.put<CostCentre>(`${url}/${costCentre.id}`, costCentre)
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<CostCentre>;
.pipe(catchError(this.log.handleError('CostCentreService', 'update'))) as Observable<CostCentre>;
}
saveOrUpdate(costCentre: CostCentre): Observable<CostCentre> {
@@ -51,6 +47,6 @@ export class CostCentreService {
delete(id: string): Observable<CostCentre> {
return this.http
.delete<CostCentre>(`${url}/${id}`)
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<CostCentre>;
.pipe(catchError(this.log.handleError('CostCentreService', 'delete'))) as Observable<CostCentre>;
}
}