-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Answer:2 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Answer:2 #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import { Store } from '@ngrx/store'; | |
| import { loadActivities } from './store/activity/activity.actions'; | ||
| import { ActivityType } from './store/activity/activity.model'; | ||
| import { selectActivities } from './store/activity/activity.selectors'; | ||
| import { initApp } from './store/app/app.actions'; | ||
| import { loadStatuses } from './store/status/status.actions'; | ||
| import { selectAllTeachersByActivityType } from './store/status/status.selectors'; | ||
| import { loadUsers } from './store/user/user.actions'; | ||
|
|
@@ -29,8 +30,7 @@ import { loadUsers } from './store/user/user.actions'; | |
| *ngFor=" | ||
| let teacher of getAllTeachersForActivityType$(activity.type) | ||
| | async | ||
| " | ||
| > | ||
| "> | ||
| {{ teacher.name }} | ||
| </li> | ||
| </ul> | ||
|
|
@@ -63,9 +63,7 @@ export class AppComponent implements OnInit { | |
| activities$ = this.store.select(selectActivities); | ||
|
|
||
| ngOnInit(): void { | ||
| this.store.dispatch(loadActivities()); | ||
| this.store.dispatch(loadUsers()); | ||
| this.store.dispatch(loadStatuses()); | ||
| this.store.dispatch(initApp()); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, that cleaner |
||
| } | ||
|
|
||
| getAllTeachersForActivityType$ = (type: ActivityType) => | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { createAction } from '@ngrx/store'; | ||
|
|
||
| export const initApp = createAction('[AppComponent] initialize Application'); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { Actions, createEffect, ofType } from '@ngrx/effects'; | ||
| import { flatMap, mergeMap } from 'rxjs/operators'; | ||
| import * as AppActions from './app.actions'; | ||
| import * as ActivityActions from '../activity/activity.actions'; | ||
| import * as UserActions from '../user/user.actions'; | ||
|
|
||
| @Injectable() | ||
| export class AppEffects { | ||
| initialiseApp$ = createEffect(() => | ||
| this.actions$.pipe( | ||
| ofType(AppActions.initApp), | ||
| mergeMap(() => [ | ||
| ActivityActions.loadActivities(), | ||
| UserActions.loadUsers(), | ||
| ]) | ||
| ) | ||
| ); | ||
|
|
||
| constructor(private actions$: Actions) {} | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this effect is not necessary. UserEffect and ActivityEffect can both listen to appAction.initApp. |
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,39 @@ | ||
| import { createFeatureSelector, createSelector } from '@ngrx/store'; | ||
| import { createSelector } from '@ngrx/store'; | ||
| import { ActivityType } from '../activity/activity.model'; | ||
| import { statusFeatureKey, StatusState } from './status.reducer'; | ||
|
|
||
| export const selectStatusState = | ||
| createFeatureSelector<StatusState>(statusFeatureKey); | ||
| import { selectActivities } from '../activity/activity.selectors'; | ||
| import { selectUser } from '../user/user.selectors'; | ||
| import { Status } from './status.model'; | ||
|
|
||
| export const selectStatuses = createSelector( | ||
| selectStatusState, | ||
| (state) => state.statuses | ||
| selectUser, | ||
| selectActivities, | ||
| (user, activities): Status[] => { | ||
| if (user?.isAdmin) { | ||
| return activities.reduce((status: Status[], activity): Status[] => { | ||
| const index = status.findIndex((s) => s.name === activity.type); | ||
| if (index === -1) { | ||
| return [ | ||
| ...status, | ||
| { name: activity.type, teachers: [activity.teacher] }, | ||
| ]; | ||
| } else { | ||
| status[index].teachers.push(activity.teacher); | ||
| return status; | ||
| } | ||
| }, []); | ||
| } | ||
| return []; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that 's the way to do it, nice |
||
| } | ||
| ); | ||
|
|
||
| export const selectTeachersMap = createSelector(selectStatuses, (statuses) => { | ||
| const map = new Map(); | ||
| statuses.forEach((s) => map.set(s.name, s.teachers)); | ||
| return map; | ||
| }); | ||
|
|
||
| export const selectAllTeachersByActivityType = (name: ActivityType) => | ||
| createSelector( | ||
| selectStatusState, | ||
| (state) => state.teachersMap.get(name) ?? [] | ||
| selectTeachersMap, | ||
| (teachersMap) => teachersMap.get(name) ?? [] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need the last two selectors. You should create a viewModel with construct your final object that you will use in your view |
||
| ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the goal is to avoid calling a function inside a template. The view object should be constructed inside the selector. Thus in the view, only property of that object will be rendered