Store Features
Generates necessary state, computed and methods for call progress status to the store
Kind: global function
Import the withCallStatus trait from @ngrx-traits/signals.
import { withCallStatus } from '@ngrx-traits/signals';
Use this when you need a special way to call you backend that is not handled by withCalls
The following generates a callStatus signal, and getters isLoading isLoaded error, and computed setLoading setLoaded and setError
const store = signalStore(withCallStatus());
The following generates a userCallStatus signal, and getters isUserLoading isUserLoaded userError, and computed setUserLoading setUserLoaded and setUserError
const store = signalStore(withCallStatus({ prop: 'user' }));
The following generates a userEntitiesCallStatus signal, and getters isUserEntitiesLoading isUserEntitiesLoaded userEntitiesError, and computed setUserEntitiesLoading setUserEntitiesLoaded and setUserEntitiesError
const store = signalStore(withCallStatus({ collection: 'user' }));
const store = signalStore(withCallStatus({ collection: 'user', initialValue: 'loading', errorType: type<string>() }));
import { withEntities } from '@ngrx/signals/entities';
const entity = type<Product>();
const collection = 'product';
export const ProductsRemoteStore = signalStore(
{ providedIn: 'root' },
withEntities({ entity, collection }),
withCallStatus({ collection, initialValue: 'loading' }),
withMethods(({setProductEntitiesLoading, setProductEntitiesLoaded, setProductEntitiesError, ...store}) => ({
loadProducts: rxMethod(pipe(switchMap(() => {
setProductEntitiesLoading()
return inject(ProductService)
.getProducts()
.pipe(
tap((res) =>
patchState(
store,
setAllEntities(res.resultList, { collection: 'product' }),
),
),
catchError((error) => {
setProductEntitiesError(error);
return EMPTY;
}));
})))
}))
);
const entity = type<Product>();
const collection = 'product';
export const ProductsRemoteStore = signalStore(
{ providedIn: 'root' },
withEntities({ entity, collection }),
withCallStatus({ prop: collection, initialValue: 'loading' }),
// withEntitiesLoading will Call the fetch function when the status is set to loading
// in this case it will happen on init because the initial value is loading
withEntitiesLoadingCall({
collection,
fetchEntities: () => {
return inject(ProductService)
.getProducts()
.pipe(
map((d) => ({
entities: d.resultList,
total: d.total,
})),
);
},
});
To know more about withEntitiesLoadingCall see docs here
This trait receives an object to allow specific configurations:
| Property | Description | Value |
|---|---|---|
| prop | The name of the property for which this represents the call status. | string |
| initialValue | The initial value of the call status. | init | loading | loaded |
| collection | The name of the collection for which this represents the call status is an alias to prop param | string |
| errorType | The type of the error | T |
Generates the following signals
callStatus: 'init' | 'loading' | 'loaded' | { error: unknown };
If collection provided, the following signals are generated, example: users
callUsersStatus: 'init' | 'loading' | 'loaded' | { error: unknown };
Generates the following computed signals
isLoading: Signal<boolean>;
isLoaded: Signal<boolean>;
error: Signal<unknown | null>;
If collection provided, the following computed signals are generated, example: users
isUserEntitiesLoading: Signal<boolean>;
isUserEntitiesLoaded: Signal<boolean>;
userEntitiesError: Signal<unknown | null>;
Generates the following methods
setLoading: () => void;
setLoaded:() => void;
setError:(error?: unknown) => void;
If collection provided, the following methods are generated, example: users
setUserEntitiesLoading: () => void;
setUserEntitiesLoaded:() => void;
setUserEntitiesError:(error?: unknown) => void;