Slide 1

Slide 1 text

NgRx Action Group Creator Marko Stanimirović

Slide 2

Slide 2 text

Marko Stanimirović @MarkoStDev ★ Sr. Frontend Engineer at JobCloud ★ NgRx Team Member ★ Angular Belgrade Organizer ★ Hobby Musician ★ M.Sc. in Software Engineering

Slide 3

Slide 3 text

Action Creator

Slide 4

Slide 4 text

products-page.actions.ts import { createAction } from '@ngrx/store';

Slide 5

Slide 5 text

products-page.actions.ts import { createAction } from '@ngrx/store'; ./ defining an action without payload export const opened = createAction('[Products Page] Opened');

Slide 6

Slide 6 text

products-page.actions.ts import { createAction, props } from '@ngrx/store'; ./ defining an action without payload export const opened = createAction('[Products Page] Opened'); ./ defining an action with payload using the `props` function export const paginationChanged = createAction( '[Products Page] Pagination Changed', props<{ page: number; offset: number }>() );

Slide 7

Slide 7 text

products-page.actions.ts import { createAction, props } from '@ngrx/store'; ./ defining an action without payload export const opened = createAction('[Products Page] Opened'); ./ defining an action with payload using the `props` function export const paginationChanged = createAction( '[Products Page] Pagination Changed', props<{ page: number; offset: number }>() ); ./ defining an action with payload using the props factory export const queryChanged = createAction( '[Product Page] Query Changed', (query: string) .> ({ query }) );

Slide 8

Slide 8 text

products-page.actions.ts import { createAction, props } from '@ngrx/store'; ./ defining an action without payload export const opened = createAction('[Products Page] Opened'); ./ defining an action with payload using the `props` function export const paginationChanged = createAction( '[Products Page] Pagination Changed', props<{ page: number; offset: number }>() ); ./ defining an action with payload using the props factory export const queryChanged = createAction( '[Product Page] Query Changed', (query: string) .> ({ query }) ); products.component.ts import * as ProductsPageActions from './products-page.actions'; @Component({ ** **. */ }) export class ProductsComponent implements OnInit { constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch(ProductsPageActions.opened()); } }

Slide 9

Slide 9 text

products-page.actions.ts import { createAction, props } from '@ngrx/store'; ./ defining an action without payload export const opened = createAction('[Products Page] Opened'); ./ defining an action with payload using the `props` function export const paginationChanged = createAction( '[Products Page] Pagination Changed', props<{ page: number; offset: number }>() ); ./ defining an action with payload using the props factory export const queryChanged = createAction( '[Product Page] Query Changed', (query: string) .> ({ query }) ); products.component.ts import * as ProductsPageActions from './products-page.actions'; @Component({ ** **. */ }) export class ProductsComponent implements OnInit { constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch(ProductsPageActions.opened()); } } products/actions/index.ts export * as ProductsPageActions from './products-page.actions'; export * as ProductsApiActions from './products-api.actions';

Slide 10

Slide 10 text

products-page.actions.ts import { createAction, props } from '@ngrx/store'; ./ defining an action without payload export const opened = createAction('[Products Page] Opened'); ./ defining an action with payload using the `props` function export const paginationChanged = createAction( '[Products Page] Pagination Changed', props<{ page: number; offset: number }>() ); ./ defining an action with payload using the props factory export const queryChanged = createAction( '[Product Page] Query Changed', (query: string) .> ({ query }) ); products.component.ts import * as ProductsPageActions from './products-page.actions'; @Component({ ** **. */ }) export class ProductsComponent implements OnInit { constructor(private readonly store: Store) {} ngOnInit(): void { this.store.dispatch(ProductsPageActions.opened()); } } products/actions/index.ts export * as ProductsPageActions from './products-page.actions'; export * as ProductsApiActions from './products-api.actions';

Slide 11

Slide 11 text

createActionGroup

Slide 12

Slide 12 text

products-page.actions.ts import { createActionGroup } from '@ngrx/store';

Slide 13

Slide 13 text

products-page.actions.ts import { createActionGroup } from '@ngrx/store'; export const ProductsPageActions = createActionGroup({ source: 'Products Page', events: { }, });

Slide 14

Slide 14 text

products-page.actions.ts import { createActionGroup, emptyProps } from '@ngrx/store'; export const ProductsPageActions = createActionGroup({ source: 'Products Page', events: { ./ defining an event without payload using the `emptyProps` function Opened: emptyProps(), }, });

Slide 15

Slide 15 text

products-page.actions.ts import { createActionGroup, emptyProps, props } from '@ngrx/store'; export const ProductsPageActions = createActionGroup({ source: 'Products Page', events: { ./ defining an event without payload using the `emptyProps` function Opened: emptyProps(), ./ defining an event with payload using the `props` function 'Pagination Changed': props<{ page: number; offset: number }>(), }, });

Slide 16

Slide 16 text

products-page.actions.ts import { createActionGroup, emptyProps, props } from '@ngrx/store'; export const ProductsPageActions = createActionGroup({ source: 'Products Page', events: { ./ defining an event without payload using the `emptyProps` function Opened: emptyProps(), ./ defining an event with payload using the `props` function 'Pagination Changed': props<{ page: number; offset: number }>(), ./ defining an event with payload using the props factory 'Query Changed': (query: string) .> ({ query }), }, });

Slide 17

Slide 17 text

products-page.actions.ts import { createActionGroup, emptyProps, props } from '@ngrx/store'; export const ProductsPageActions = createActionGroup({ source: 'Products Page', events: { ./ defining an event without payload using the `emptyProps` function Opened: emptyProps(), ./ defining an event with payload using the `props` function 'Pagination Changed': props<{ page: number; offset: number }>(), ./ defining an event with payload using the props factory 'Query Changed': (query: string) .> ({ query }), }, }); ./ action type: [Products Page] Opened ProductsPageActions.opened(); ./ action type: [Products Page] Pagination Changed ProductsPageActions.paginationChanged({ page: 10, offset: 100 }); ./ action type: [Products Page] Query Changed ProductsPageActions.queryChanged('ngrx');

Slide 18

Slide 18 text

Limitations

Slide 19

Slide 19 text

export const productsLoadedSuccess = createAction( '[Products API] Products Are Loaded Successfully', props<{ products: Product[] }>() );

Slide 20

Slide 20 text

export const productsLoadedSuccess = createAction( '[Products API] Products Are Loaded Successfully', props<{ products: Product[] }>() );

Slide 21

Slide 21 text

Restrictions

Slide 22

Slide 22 text

export const ProductsApiActions = createActionGroup({ source: 'Products API', events: { '(Products) "Loaded" Success !!!': props<{ products: Product[] }>(), ./ ... }, });

Slide 23

Slide 23 text

products-api.actions.ts export const productsLoadedSuccess = createAction( '[Products API] Products Loaded Success', props<{ products: Product[] }>() ); export const productsLoadedFailure = createAction( '[Products API] Products Loaded Failure', props<{ errorMsg: string }>() ); export const productCreatedSuccess = createAction( '[Products API] Product Created Success', props<{ product: Product }>() ); export const productCreatedFailure = createAction( '[Products API] Product Created Failure', props<{ errorMsg: string }>() ); export const productUpdatedSuccess = createAction( '[Products API] Product Updated Success', props<{ product: Product }>() ); export const productUpdatedFailure = createAction( '[Products API] Product Updated Failure', props<{ errorMsg: string }>() ); export const ProductsApiActions = createActionGroup({ source: 'Products API', events: { 'Products Loaded Success': props<{ products: Product[] }>(), 'Products Loaded Failure': props<{ errorMsg: string }>(), 'Product Created Success': props<{ product: Product }>(), 'Product Created Failure': props<{ errorMsg: string }>(), 'Product Updated Success': props<{ product: Product }>(), 'Product Updated Failure': props<{ errorMsg: string }>(), }, });

Slide 24

Slide 24 text

Marko Stanimirović @MarkoStDev Thank You!