Skip to content

Commit

Permalink
feat(signals): allow withEntitiesLoadingCall to receive a factory fun…
Browse files Browse the repository at this point in the history
…ction for config

the factory function config will receive the store , and allow access to it in the fetchEntities,
onSuccess and onErro methods

Fix #95
  • Loading branch information
Gabriel Guerrero committed Jun 6, 2024
1 parent 172a221 commit 6ec7c61
Show file tree
Hide file tree
Showing 5 changed files with 805 additions and 370 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const ProductsBranchStore = signalStore(
pageSize: 10,
entity,
}),
withEntitiesLoadingCall({
fetchEntities: async ({ entitiesPagedRequest, entitiesFilter }) => {
withEntitiesLoadingCall(({ entitiesPagedRequest, entitiesFilter }) => ({
fetchEntities: async () => {
const res = await lastValueFrom(
inject(BranchService).getBranches({
search: entitiesFilter().search,
Expand All @@ -41,7 +41,7 @@ export const ProductsBranchStore = signalStore(
);
return { entities: res.resultList };
},
}),
})),
),
signalStoreFeature(
withEntities({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,24 @@ export const ProductsShopStore = signalStore(
{ providedIn: 'root' },
productsStoreFeature,
orderItemsStoreFeature,
withEntitiesLoadingCall({
collection: productsCollection,
fetchEntities: async ({
productsPagedRequest,
productsFilter,
productsSort,
}) => {
const res = await lastValueFrom(
inject(ProductService).getProducts({
search: productsFilter().search,
skip: productsPagedRequest().startIndex,
take: productsPagedRequest().size,
sortAscending: productsSort().direction === 'asc',
sortColumn: productsSort().field,
}),
);
return { entities: res.resultList, total: res.total };
},
mapError: (error) => (error as Error).message,
}),
withEntitiesLoadingCall(
({ productsPagedRequest, productsFilter, productsSort }) => ({
collection: productsCollection,
fetchEntities: async () => {
const res = await lastValueFrom(
inject(ProductService).getProducts({
search: productsFilter().search,
skip: productsPagedRequest().startIndex,
take: productsPagedRequest().size,
sortAscending: productsSort().direction === 'asc',
sortColumn: productsSort().field,
}),
);
return { entities: res.resultList, total: res.total };
},
mapError: (error) => (error as Error).message,
}),
),
withCalls(({ orderItemsEntities }, snackBar = inject(MatSnackBar)) => ({
loadProductDetail: ({ id }: { id: string }) =>
inject(ProductService).getProductDetail(id),
Expand Down
2 changes: 1 addition & 1 deletion libs/ngrx-traits/signals/api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ if an error occurs it will set the error to the store using set[Collection]Error
| Param | Description |
| --- | --- |
| config | <p>Configuration object</p> |
| config | <p>Configuration object or factory function that returns the configuration object</p> |
| config.fetchEntities | <p>A function that fetches the entities from a remote source the return type</p> |
| config.collection | <p>The collection name</p> |
| config.onSuccess | <p>A function that is called when the fetchEntities is successful</p> |
Expand Down
Loading

0 comments on commit 6ec7c61

Please sign in to comment.