@giorgionatili
├── actions
│ └── user.action.ts
├── app.component.css
├── app.component.html
├── app.component.spec.ts
├── app.component.ts
├── app.module.ts
├── components
│ ├── create
│ └── index
├── models
│ └── User.ts
└── state
└── user.state.ts
@giorgionatili
@giorgionatili
export interface Person {
firstName: string;
lastName: string;
age: number;
}
@giorgionatili
export interface User {
id: string;
role: Roles;
}
enum Roles {
Admin,
RegisteredUser,
AnonymousUser
}
@giorgionatili
export interface Group {
id: number;
name: string;
users: User[];
permissions: Roles[];
}
@giorgionatili
import { Store } from '@ngrx/store';
import { AddPerson } from './person.actions';
@Component({ ... })
export class AddPersonComponent {
constructor(private store: Store) {}
addPerson(name: string) {
this.store.dispatch(new AddPerson(name));
}
}
@giorgionatili
export class AddPerson {
static readonly type = '[Person] Add Person';
constructor(public name: string) {}
}
@giorgionatili
import { State } from '@ngxs/store';
@State<string[]>({
name: 'users',
defaults: []
})
export class UsersState {}
Selects are a representation of Observable slices of the app state
The @Select decorator allows selecting slices of data from the store
The slices of data can be acquired passing a state class or a function
@giorgionatili
import { Select } from '@ngxs/store';
@Component({ ... })
export class GroupComponent {
// Reads the name of the state from the state class
@Select(GroupState) people$: Observable<string[]>;
}
@giorgionatili
@giorgionatili
@Component({
selector: 'app-profile-editor',
template: `
<form [formGroup]="profileForm">
<label>
First Name: <input type="text" formControlName="firstName">
</label>
<label>
Last Name: <input type="text" formControlName="lastName">
</label>
</form>
`,
})
export class ProfileEditorComponent {
profileForm = new FormGroup({
firstName: new FormControl(''),
lastName: new FormControl(''),
});
}
@giorgionatili
import { from } from 'rxjs';
// Emit array as a sequence of values
const arraySource = from([1, 2, 3, 4, 5]);
const subscribe = arraySource
.subscribe(val => console.log(val));
@giorgionatili
https://www.learnrxjs.io/operators/
import { from } from 'rxjs';
import { distinctUntilChanged } from 'rxjs/operators';
const myArrayWithDuplicatesInARow = from([1, 1, 2, 2, 3, 1, 2, 3]);
const distinctSub = myArrayWithDuplicatesInARow
.pipe(distinctUntilChanged())
.subscribe(val => console.log('DISTINCT SUB:', val));
// Output: DISTINCT SUB: 1,2,3,1,2,3
@giorgionatili
Keep calm and streakm on...