-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(signals): allow withCalls generated method to receive Signal and …
…Observables as params The methods generated by withCalls are rxMethods, this PR enables the generated methods to also receive Signal or Observable with the params of the method Fixes #68
- Loading branch information
1 parent
212e7b0
commit bf3d6f9
Showing
11 changed files
with
193 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...gnals/product-shop-page/components/smart-product-detail/smart-product-detail.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { CurrencyPipe } from '@angular/common'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
inject, | ||
input, | ||
OnInit, | ||
} from '@angular/core'; | ||
import { MatCardModule } from '@angular/material/card'; | ||
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; | ||
import { withCalls } from '@ngrx-traits/signals'; | ||
import { signalStore } from '@ngrx/signals'; | ||
|
||
import { ProductService } from '../../../../services/product.service'; | ||
|
||
const ProductDetailStore = signalStore( | ||
withCalls(() => ({ | ||
loadProductDetail: (id: string) => | ||
inject(ProductService).getProductDetail(id), | ||
})), | ||
); | ||
@Component({ | ||
selector: 'smart-product-detail', | ||
styles: [ | ||
` | ||
mat-spinner { | ||
margin: 10px auto; | ||
} | ||
`, | ||
], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
standalone: true, | ||
imports: [MatCardModule, MatProgressSpinnerModule, CurrencyPipe], | ||
providers: [ProductDetailStore], | ||
template: ` | ||
@if (store.isLoadProductDetailLoaded()) { | ||
<mat-card> | ||
<mat-card-header> | ||
<mat-card-title>{{ | ||
store.loadProductDetailResult()?.name | ||
}}</mat-card-title> | ||
<mat-card-subtitle | ||
>Price: £{{ store.loadProductDetailResult()?.price | currency }} | ||
Released: | ||
{{ | ||
store.loadProductDetailResult()?.releaseDate | ||
}}</mat-card-subtitle | ||
> | ||
</mat-card-header> | ||
<img | ||
mat-card-image | ||
src="/{{ store.loadProductDetailResult()?.image }}" | ||
/> | ||
<mat-card-content> | ||
<p>{{ store.loadProductDetailResult()?.description }}</p> | ||
</mat-card-content> | ||
</mat-card> | ||
} @else if (store.isLoadProductDetailLoading()) { | ||
<mat-spinner /> | ||
} | ||
`, | ||
}) | ||
export class SmartProductDetailComponent implements OnInit { | ||
store = inject(ProductDetailStore); | ||
productId = input.required<string>(); | ||
ngOnInit() { | ||
// 👇 loadProductDetail is an rxMethod it can receive a signal reference | ||
this.store.loadProductDetail(this.productId); | ||
} | ||
// other ways | ||
|
||
// loadProductDetail = effect( | ||
// () => { | ||
// this.store.loadProductDetail(this.productId()); | ||
// }, | ||
// { allowSignalWrites: true }, | ||
// ); | ||
|
||
// constructor() { | ||
// effect( | ||
// () => { | ||
// this.store.loadProductDetail(this.productId()); | ||
// }, | ||
// { allowSignalWrites: true }, | ||
// ); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters