Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
feat: add example app
Browse files Browse the repository at this point in the history
  • Loading branch information
markostanimirovic committed Mar 11, 2021
1 parent 9ef67e9 commit 5f67782
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 3 deletions.
2 changes: 1 addition & 1 deletion projects/example/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { Component } from '@angular/core';

@Component({
selector: 'ngrx-root',
template: '',
template: '<ngrx-users></ngrx-users>',
})
export class AppComponent {}
5 changes: 3 additions & 2 deletions projects/example/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { StoreModule } from '@ngrx/store';
import { AppComponent } from './app.component';
import { UsersModule } from './users/users.module';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule],
providers: [],
imports: [BrowserModule, StoreModule.forRoot({}), UsersModule],
bootstrap: [AppComponent],
})
export class AppModule {}
7 changes: 7 additions & 0 deletions projects/example/src/app/users/users.actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createAction, props } from '@ngrx/store';

export const enter = createAction('[Users Page] Enter');
export const updateSearchTerm = createAction(
'[Users Page] Update Search Term',
props<{ searchTerm: string }>(),
);
41 changes: 41 additions & 0 deletions projects/example/src/app/users/users.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Store } from '@ngrx/store';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { selectFilteredUsers } from './users.selectors';
import * as UsersPageActions from './users.actions';

@Component({
selector: 'ngrx-users',
template: `
<h2>Users</h2>
<input type="text" [formControl]="searchControl" placeholder="Search..." />
<ul>
<li *ngFor="let user of filteredUsers$ | async">
{{ user }}
</li>
</ul>
`,
})
export class UsersComponent implements OnInit, OnDestroy {
private readonly destroy = new Subject();
readonly searchControl = new FormControl('');
readonly filteredUsers$ = this.store.select(selectFilteredUsers);

constructor(private readonly store: Store) {}

ngOnInit(): void {
this.store.dispatch(UsersPageActions.enter());

this.searchControl.valueChanges
.pipe(takeUntil(this.destroy))
.subscribe(searchTerm =>
this.store.dispatch(UsersPageActions.updateSearchTerm({ searchTerm })),
);
}

ngOnDestroy(): void {
this.destroy.next();
}
}
13 changes: 13 additions & 0 deletions projects/example/src/app/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ReactiveFormsModule } from '@angular/forms';
import { StoreModule } from '@ngrx/store';
import { UsersComponent } from './users.component';
import * as fromUsers from './users.reducer';

@NgModule({
imports: [CommonModule, ReactiveFormsModule, StoreModule.forFeature('users', fromUsers.reducer)],
declarations: [UsersComponent],
exports: [UsersComponent],
})
export class UsersModule {}
18 changes: 18 additions & 0 deletions projects/example/src/app/users/users.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createReducer, on } from '@ngrx/store';
import * as UsersPageActions from './users.actions';

export interface State {
users: string[];
searchTerm: string;
}

export const initialState: State = {
users: ['marko', 'john', 'jimmy', 'steve', 'michael'],
searchTerm: '',
};

export const reducer = createReducer(
initialState,
on(UsersPageActions.enter, () => initialState),
on(UsersPageActions.updateSearchTerm, (state, { searchTerm }) => ({ ...state, searchTerm })),
);
16 changes: 16 additions & 0 deletions projects/example/src/app/users/users.selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { createFeatureSelector, createSelector } from '@ngrx/store';
import { createChildSelectors } from 'ngrx-child-selectors';
import * as fromUsers from './users.reducer';

export const selectUserState = createFeatureSelector<fromUsers.State>('users');

export const { selectUsers, selectSearchTerm } = createChildSelectors(
selectUserState,
fromUsers.initialState,
);

export const selectFilteredUsers = createSelector(
selectUsers,
selectSearchTerm,
(users, searchTerm) => users.filter(u => u.includes(searchTerm)),
);

0 comments on commit 5f67782

Please sign in to comment.