angularngrxngrx-data

Angular - Ngrx Entity Collection Service Base - Serialization


In my current project I have a backend with nestjs and a frontend in angular.

I am loading some data from the backend in my service using ngrx entity as follows:

calendar-event.service.ts

@Injectable({
  providedIn: 'root'
})
export class CalendarEventService extends EntityCollectionServiceBase<CalendarEvent> {

  constructor(elementsFactory: EntityCollectionServiceElementsFactory) {
    super('CalendarEvent', elementsFactory);
  }
}

And I have my entity-metadata setup

import {
  EntityMetadataMap,
  EntityDataModuleConfig,
  DefaultDataServiceConfig,
} from '@ngrx/data';

const entityMetadata: EntityMetadataMap = {
  CalendarEvent: {},
};

const pluralNames = {};

export const entityConfig: EntityDataModuleConfig = {
  entityMetadata,
  pluralNames,
};

export const defaultDataServiceConfig: DefaultDataServiceConfig = {
  entityHttpResourceUrls: {
    CalendarEvent: {
      collectionResourceUrl: 'http://localhost:3000/api/calendar/',
      entityResourceUrl: 'http://localhost:3000/api/calendar/',
    },
  },
};

My data are loading fine when I load them from app.component.ts

import { Observable } from 'rxjs';
import { Component, OnInit } from '@angular/core';
import { CalendarEventService } from './shared/services/calendar-event.service';
import { CalendarEvent } from 'angular-calendar';

@Component({
  selector: 'app-root',
  template: `
    <app-shell class="app-shell">
      <router-outlet></router-outlet>
    </app-shell>
  `,
})
export class AppComponent implements OnInit {
  title = 'memento-front';
  events$: Observable<CalendarEvent[] | any>;

  constructor(private readonly events: CalendarEventService) {
    this.events$ = this.events.filteredEntities$;
  }

  ngOnInit(): void {
    this.events.load();
  }
}

I see them fine:

{ "id": 2, "title": "Second event", "start": "2021-08-13T17:22:43.192Z", "color": { "primary": "#1e90ff", "secondary": "#D1E8FF" } }

What I am trying to achieve is when the data are serialized instead of giving to start the string date, creating an Date object. The idea would to do something like

const entityMetadata: EntityMetadataMap = {
  CalendarEvent: {
    filterFn: (events) => {
      events.map(event => event.start = new Date(event.start));
      return events;
    }
  },
};

But instead of doing when filterFn is called, doing when the http call is made, before it becomes immutable

I tried to look up serialization with ngrx entity collection service base but I didn't find anything letting me know how I could take some action on the data properties upon loading from http call


Solution

  • I tried to look up serialization with ngrx entity collection service base but I didn't find anything letting me know how I could take some action on the data properties upon loading from http call

    You can follow this steps in order to perform action on data properties upon api call:

    app.component.ts:

    this.events$ = store.select(selectAllEvents);
    this.eventsService.load().subscribe(events => {      
      this.store.dispatch(EventsApiActions.evensLoaded({ events }));
    });
    

    app/events/actions/events-api.actions.ts

    export const eventsLoaded = createAction(
        "[Events API] Events Loaded Success",
        props<{ events: CalendarEvent[] }>()
    );
    

    app/shared/state/events.reducer.ts

    export interface State {
      collection: CalendarEvent[];
      ...
    }
    
    export const initialState: State = {
        collection: [],
        ...
    };
    
    export const eventsReducer = createReducer(
        initialState,
        on(EventsApiActions.eventsLoaded, (state, action) => {
            return {
                ...state,
                collection: action.events
            };
        }),
        ...
    );
    

    Now, instead of mutating the events collection with collection: action.events, you can pass into your utility function of filterFn with:

    const mappedEvents = (events: CalendarEvents[]) =>
        events.map((event) => event.start = new Date(event.start))
    });
    

    Now you may use it here:

    on(EventsApiActions.eventsLoaded, (state, action) => {
        return {
            ...state,
            collection: mappedEvents(action.events)
        };
    }),
    

    Finally the selectors (events.reduce.ts):

    export const selectAll = (state: State) => state.collection;
    export const selectEventsState = (state: State) => state.events;
    export const selectAllEvents = createSelector(
        selectEventsState,
        selectAll
    );