Skip to content

Commit

Permalink
Merge pull request #44 from jucasoft/16.1.0
Browse files Browse the repository at this point in the history
16.1.0
  • Loading branch information
jucasoft authored Jan 22, 2024
2 parents 37b2b4d + 651a7df commit d3d189a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion projects/ngrx-entity-crud/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ export const {
selectIsLoading,
selectIsLoaded,
selectResponses,
selectItemSelected,
} = getSingeCrudSelectors<<%= clazz %>, object>(selectState)

1 change: 1 addition & 0 deletions projects/ngrx-entity-crud/src/lib/entity_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function getInitialEntityCrudState<V>(): EntityCrudState<V> {
export function getInitialSingleCrudState<V>(): EntitySingleCrudState<V> {
return {
item:null,
itemSelected:null,
isLoading: false,
isLoaded: false,
error: null,
Expand Down
35 changes: 19 additions & 16 deletions projects/ngrx-entity-crud/src/lib/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -196,6 +196,13 @@ export interface EntityCrudBaseSelectors<T, V> {
*/
selectResponses: (state: V) => OptResponse<T>[];

/**
* - 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<T, V> extends EntityCrudBaseSelectors<T, V> {
Expand All @@ -215,13 +222,6 @@ export interface EntityCrudSelectors<T, V> extends EntityCrudBaseSelectors<T, V>
* @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 ...}})",
Expand Down Expand Up @@ -278,7 +278,8 @@ export interface EntityCrudBaseState<T> {
}

export interface EntitySingleCrudState<T> extends EntityCrudBaseState<T> {
item: T
item: T;
itemSelected: T;
}

export interface EntityCrudState<T> extends EntityCrudBaseState<T>, EntityState<T> {
Expand Down Expand Up @@ -356,6 +357,14 @@ export interface SingularActions<T> {
* @param item: T
*/
Edit: ActionCreator<string, (props: { item: T; }) => { item: T; } & TypedAction<string>>;

/**
* - action used to identify a single item to select
* @example store.dispatch(actions.SelectItem(payload));
* @param item: T
*/
SelectItem: ActionCreator<string, (props: { item: T; }) => { item: T; } & TypedAction<string>>;

}

export interface Actions<T> extends SingularActions<T> {
Expand Down Expand Up @@ -463,12 +472,6 @@ export interface Actions<T> extends SingularActions<T> {
RemoveManySelected: ActionCreator<string, (props: { ids: string[]; }) => { ids: string[]; } & TypedAction<string>>;
// TODO: doc
RemoveAllSelected: ActionCreator<string>;
/**
* - action used to identify a single item to select
* @example store.dispatch(actions.SelectItem(payload));
* @param item: T
*/
SelectItem: ActionCreator<string, (props: { item: T; }) => { item: T; } & TypedAction<string>>;

/**
* - action used to create an item on the store
Expand Down
15 changes: 13 additions & 2 deletions projects/ngrx-entity-crud/src/lib/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,13 @@ export function createCrudReducerFactory<T>(adapter: EntityAdapter<T>) {
};
}


export function createSingularCrudOns<T, S extends EntitySingleCrudState<T>>(initialState: S, actions: SingularActions<T>): { [key: string]: any } {

const selectRequestOn = on(actions.SelectRequest, (state: S, {type}) => (
{
...state,
isLoading: true,
error: null
error: initialState.error
}
));

Expand Down Expand Up @@ -573,6 +572,16 @@ export function createSingularCrudOns<T, S extends EntitySingleCrudState<T>>(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,
Expand All @@ -585,6 +594,8 @@ export function createSingularCrudOns<T, S extends EntitySingleCrudState<T>>(ini
editSuccessOn,
editFailureOn,

selectItemOn,

resetOn,
editOn,
};
Expand Down
13 changes: 9 additions & 4 deletions projects/ngrx-entity-crud/src/lib/state_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ export function getCrudBaseSelectors<T, V>(
const selectLastCriteria: MemoizedSelector<V, ICriteria> = createSelector(selectState, getLastCriteria);
const selectResponses: MemoizedSelector<V, OptResponse<T>[]> = createSelector(selectState, getRespones);

const selectItemSelected: MemoizedSelector<V, T> = createSelector(selectState, getItemSelected) as MemoizedSelector<V, T>;

return {
selectError,
selectIsLoading,
selectIsLoaded,
selectLastCriteria,
selectResponses,
selectItemSelected
};
}

Expand All @@ -40,7 +43,8 @@ export function getSingeCrudSelectors<T, V>(
selectIsLoading,
selectIsLoaded,
selectLastCriteria,
selectResponses
selectResponses,
selectItemSelected
} = getCrudBaseSelectors(selectState);

const selectItem: MemoizedSelector<V, T> = createSelector(selectState, getItem) as MemoizedSelector<V, T>
Expand All @@ -50,7 +54,8 @@ export function getSingeCrudSelectors<T, V>(
selectIsLoading,
selectIsLoaded,
selectLastCriteria,
selectResponses
selectResponses,
selectItemSelected
};
}

Expand All @@ -64,7 +69,6 @@ export function createCrudSelectorsFactory<T>(adapter) {
getFilters
);

const selectItemSelected: MemoizedSelector<V, T> = createSelector(selectState, getItemSelected) as MemoizedSelector<V, T>;
const selectEntitiesSelected: MemoizedSelector<V, Dictionary<T>> = createSelector(selectState, getEntitiesSelected) as MemoizedSelector<V, Dictionary<T>>
const selectItemsSelected: MemoizedSelector<V, T[]> = createSelector(selectEntitiesSelected, (entities: Dictionary<T>) => Object.values(entities)) as MemoizedSelector<V, T[]>

Expand All @@ -76,7 +80,8 @@ export function createCrudSelectorsFactory<T>(adapter) {
selectIsLoading,
selectIsLoaded,
selectLastCriteria,
selectResponses
selectResponses,
selectItemSelected
} = getCrudBaseSelectors(selectState);

const {
Expand Down

0 comments on commit d3d189a

Please sign in to comment.