diff --git a/projects/ngrx-entity-crud/package.json b/projects/ngrx-entity-crud/package.json index 0714940..2aaaddb 100644 --- a/projects/ngrx-entity-crud/package.json +++ b/projects/ngrx-entity-crud/package.json @@ -1,6 +1,6 @@ { "name": "ngrx-entity-crud", - "version": "16.0.0", + "version": "16.1.0", "repository": "https://github.com/jucasoft/ngrx-entity-crud", "license": "MIT", "schematics": "./schematics/collection.json", diff --git a/projects/ngrx-entity-crud/schematics/store/files/crud-store/singular/__clazz@dasherize__-store/__clazz@dasherize__.selectors.ts b/projects/ngrx-entity-crud/schematics/store/files/crud-store/singular/__clazz@dasherize__-store/__clazz@dasherize__.selectors.ts index 42fd742..9ddddc2 100644 --- a/projects/ngrx-entity-crud/schematics/store/files/crud-store/singular/__clazz@dasherize__-store/__clazz@dasherize__.selectors.ts +++ b/projects/ngrx-entity-crud/schematics/store/files/crud-store/singular/__clazz@dasherize__-store/__clazz@dasherize__.selectors.ts @@ -14,5 +14,6 @@ export const { selectIsLoading, selectIsLoaded, selectResponses, + selectItemSelected, } = getSingeCrudSelectors<<%= clazz %>, object>(selectState) diff --git a/projects/ngrx-entity-crud/src/lib/entity_state.ts b/projects/ngrx-entity-crud/src/lib/entity_state.ts index 2e247ad..f7288d4 100755 --- a/projects/ngrx-entity-crud/src/lib/entity_state.ts +++ b/projects/ngrx-entity-crud/src/lib/entity_state.ts @@ -20,6 +20,7 @@ export function getInitialEntityCrudState(): EntityCrudState { export function getInitialSingleCrudState(): EntitySingleCrudState { return { item:null, + itemSelected:null, isLoading: false, isLoaded: false, error: null, diff --git a/projects/ngrx-entity-crud/src/lib/models.ts b/projects/ngrx-entity-crud/src/lib/models.ts index aef94cd..03d81f5 100755 --- a/projects/ngrx-entity-crud/src/lib/models.ts +++ b/projects/ngrx-entity-crud/src/lib/models.ts @@ -93,9 +93,9 @@ export interface OptRequestBase { * In questo modo sarĂ  possibile analizzare l'andamento */ dispatchResponse?: boolean; - + /** - * used to override the root of the service + * used to override the root of the service */ basePath?: string; } @@ -196,6 +196,13 @@ export interface EntityCrudBaseSelectors { */ selectResponses: (state: V) => OptResponse[]; + /** + * - used to select selectedItem + * - populated with action "...SelectedItems({item})", + * - local clone, does not match the instance in the store + * @param (state: V) => T + */ + selectItemSelected: (state: V) => T; } export interface EntitySingleCrudSelectors extends EntityCrudBaseSelectors { @@ -215,13 +222,6 @@ export interface EntityCrudSelectors extends EntityCrudBaseSelectors * @param (state: V) => string[] | number[] */ selectIdsSelected: (state: V) => string[] | number[]; - /** - * - used to select selectedItem - * - populated with action "...SelectedItems({item})", - * - local clone, does not match the instance in the store - * @param (state: V) => T - */ - selectItemSelected: (state: V) => T; /** * - used to select selected item * - populated with action "...SelectedItems({item:{id:string ...}})", @@ -278,7 +278,8 @@ export interface EntityCrudBaseState { } export interface EntitySingleCrudState extends EntityCrudBaseState { - item: T + item: T; + itemSelected: T; } export interface EntityCrudState extends EntityCrudBaseState, EntityState { @@ -356,6 +357,14 @@ export interface SingularActions { * @param item: T */ Edit: ActionCreator { item: T; } & TypedAction>; + + /** + * - action used to identify a single item to select + * @example store.dispatch(actions.SelectItem(payload)); + * @param item: T + */ + SelectItem: ActionCreator { item: T; } & TypedAction>; + } export interface Actions extends SingularActions { @@ -463,12 +472,6 @@ export interface Actions extends SingularActions { RemoveManySelected: ActionCreator { ids: string[]; } & TypedAction>; // TODO: doc RemoveAllSelected: ActionCreator; - /** - * - action used to identify a single item to select - * @example store.dispatch(actions.SelectItem(payload)); - * @param item: T - */ - SelectItem: ActionCreator { item: T; } & TypedAction>; /** * - action used to create an item on the store diff --git a/projects/ngrx-entity-crud/src/lib/reducer.ts b/projects/ngrx-entity-crud/src/lib/reducer.ts index 21a4018..0c44c71 100755 --- a/projects/ngrx-entity-crud/src/lib/reducer.ts +++ b/projects/ngrx-entity-crud/src/lib/reducer.ts @@ -498,14 +498,13 @@ export function createCrudReducerFactory(adapter: EntityAdapter) { }; } - export function createSingularCrudOns>(initialState: S, actions: SingularActions): { [key: string]: any } { const selectRequestOn = on(actions.SelectRequest, (state: S, {type}) => ( { ...state, isLoading: true, - error: null + error: initialState.error } )); @@ -573,6 +572,16 @@ export function createSingularCrudOns>(ini ); const resetOn = on(actions.Reset, (state: S) => ({...state, ...initialState})); + + const selectItemOn = on(actions.SelectItem, (state: S, {type, item}) => { + const result = { + ...state, + itemSelected: item + }; + + return result; + }); + return { responseOn, resetResponsesOn, @@ -585,6 +594,8 @@ export function createSingularCrudOns>(ini editSuccessOn, editFailureOn, + selectItemOn, + resetOn, editOn, }; diff --git a/projects/ngrx-entity-crud/src/lib/state_selectors.ts b/projects/ngrx-entity-crud/src/lib/state_selectors.ts index 9e69254..5cbacbd 100755 --- a/projects/ngrx-entity-crud/src/lib/state_selectors.ts +++ b/projects/ngrx-entity-crud/src/lib/state_selectors.ts @@ -23,12 +23,15 @@ export function getCrudBaseSelectors( const selectLastCriteria: MemoizedSelector = createSelector(selectState, getLastCriteria); const selectResponses: MemoizedSelector[]> = createSelector(selectState, getRespones); + const selectItemSelected: MemoizedSelector = createSelector(selectState, getItemSelected) as MemoizedSelector; + return { selectError, selectIsLoading, selectIsLoaded, selectLastCriteria, selectResponses, + selectItemSelected }; } @@ -40,7 +43,8 @@ export function getSingeCrudSelectors( selectIsLoading, selectIsLoaded, selectLastCriteria, - selectResponses + selectResponses, + selectItemSelected } = getCrudBaseSelectors(selectState); const selectItem: MemoizedSelector = createSelector(selectState, getItem) as MemoizedSelector @@ -50,7 +54,8 @@ export function getSingeCrudSelectors( selectIsLoading, selectIsLoaded, selectLastCriteria, - selectResponses + selectResponses, + selectItemSelected }; } @@ -64,7 +69,6 @@ export function createCrudSelectorsFactory(adapter) { getFilters ); - const selectItemSelected: MemoizedSelector = createSelector(selectState, getItemSelected) as MemoizedSelector; const selectEntitiesSelected: MemoizedSelector> = createSelector(selectState, getEntitiesSelected) as MemoizedSelector> const selectItemsSelected: MemoizedSelector = createSelector(selectEntitiesSelected, (entities: Dictionary) => Object.values(entities)) as MemoizedSelector @@ -76,7 +80,8 @@ export function createCrudSelectorsFactory(adapter) { selectIsLoading, selectIsLoaded, selectLastCriteria, - selectResponses + selectResponses, + selectItemSelected } = getCrudBaseSelectors(selectState); const {