-
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.
feat(signals): allow idkey to withEntities* to have allow custom key …
…prop in entities Fix ##85
- Loading branch information
1 parent
711d1b7
commit 05e0424
Showing
19 changed files
with
1,067 additions
and
446 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
75 changes: 75 additions & 0 deletions
75
...ample-app/src/app/examples/signals/product-list-paginated-page/product-custom-Id.store.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,75 @@ | ||
import { inject } from '@angular/core'; | ||
import { | ||
withCalls, | ||
withCallStatus, | ||
withEntitiesLoadingCall, | ||
withEntitiesLocalFilter, | ||
withEntitiesLocalPagination, | ||
withEntitiesLocalSort, | ||
withEntitiesSingleSelection, | ||
} from '@ngrx-traits/signals'; | ||
import { signalStore, type } from '@ngrx/signals'; | ||
import { withEntities } from '@ngrx/signals/entities'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { Product } from '../../models'; | ||
import { OrderService } from '../../services/order.service'; | ||
import { ProductService } from '../../services/product.service'; | ||
|
||
// Example of wihEntities with a custom id key | ||
type ProductCustom = Omit<Product, 'id'> & { productId: string }; | ||
const entityConfig = { | ||
entity: type<ProductCustom>(), | ||
collection: 'products', | ||
idKey: 'productId' as any, | ||
} as const; | ||
|
||
export const ProductsLocalStore = signalStore( | ||
{ providedIn: 'root' }, | ||
withEntities(entityConfig), | ||
withCallStatus({ ...entityConfig, initialValue: 'loading' }), | ||
withEntitiesLocalPagination({ | ||
...entityConfig, | ||
pageSize: 5, | ||
}), | ||
withEntitiesLocalFilter({ | ||
...entityConfig, | ||
defaultFilter: { search: '' }, | ||
filterFn: (entity, filter) => | ||
!filter?.search || | ||
entity?.name.toLowerCase().includes(filter?.search.toLowerCase()), | ||
}), | ||
withEntitiesLocalSort({ | ||
...entityConfig, | ||
defaultSort: { field: 'name', direction: 'asc' }, | ||
}), | ||
withEntitiesSingleSelection({ | ||
...entityConfig, | ||
}), | ||
withEntitiesLoadingCall({ | ||
...entityConfig, | ||
fetchEntities: ({ productsFilter }) => { | ||
return inject(ProductService) | ||
.getProducts({ | ||
search: productsFilter().search, | ||
}) | ||
.pipe( | ||
map((d) => | ||
d.resultList.map(({ id, ...product }) => ({ | ||
...product, | ||
productId: id, | ||
})), | ||
), | ||
); | ||
}, | ||
}), | ||
withCalls(() => ({ | ||
loadProductDetail: { | ||
call: ({ id }: { id: string }) => | ||
inject(ProductService).getProductDetail(id), | ||
resultProp: 'productDetail', | ||
mapPipe: 'switchMap', | ||
}, | ||
checkout: () => inject(OrderService).checkout(), | ||
})), | ||
); |
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
Oops, something went wrong.