Browse Source

feat(admin-ui): Create api state branch

Michael Bromley 7 years ago
parent
commit
975d36ce9f

+ 19 - 0
admin-ui/src/app/state/api/api-actions.ts

@@ -0,0 +1,19 @@
+import { Injectable } from '@angular/core';
+
+import { StateStore } from '../state-store.service';
+
+import {Actions} from './api-state';
+
+@Injectable()
+export class ApiActions {
+
+    constructor(private store: StateStore) {}
+
+    startRequest(): void {
+        this.store.dispatch(new Actions.StartRequest());
+    }
+
+    requestCompleted(): void {
+        this.store.dispatch(new Actions.RequestCompleted());
+    }
+}

+ 26 - 0
admin-ui/src/app/state/api/api-reducer.ts

@@ -0,0 +1,26 @@
+import produce from 'immer';
+
+import {reducerCaseNever} from '../../common/utilities/common';
+
+import {Actions, ActionType, ApiState, initialApiState} from './api-state';
+
+/**
+ * Reducer for user (auth state etc.)
+ */
+export function api(state: ApiState = initialApiState, action: Actions): ApiState {
+    return produce(state, draft => {
+        switch (action.type) {
+
+            case ActionType.START_REQUEST:
+                draft.inFlightRequests++;
+                return draft;
+
+            case ActionType.REQUEST_COMPLETED:
+                draft.inFlightRequests--;
+                return draft;
+
+            default:
+                reducerCaseNever(action);
+        }
+    });
+}

+ 31 - 0
admin-ui/src/app/state/api/api-state.ts

@@ -0,0 +1,31 @@
+import { Action } from '@ngrx/store';
+
+export interface ApiState {
+    inFlightRequests: number;
+}
+
+export const initialApiState: ApiState = {
+    inFlightRequests: 0,
+};
+
+export enum ActionType  {
+    START_REQUEST = 'api/START_REQUEST',
+    REQUEST_COMPLETED = 'api/REQUEST_COMPLETED',
+}
+
+export class StartRequest implements Action {
+    readonly type = ActionType.START_REQUEST;
+}
+
+export class RequestCompleted implements Action {
+    readonly type = ActionType.REQUEST_COMPLETED;
+}
+
+export const Actions = {
+    StartRequest,
+    RequestCompleted,
+};
+
+export type Actions =
+    StartRequest |
+    RequestCompleted;

+ 2 - 0
admin-ui/src/app/state/app-state.ts

@@ -1,8 +1,10 @@
 import { NormalizedCacheObject } from 'apollo-cache-inmemory/src/types';
 
+import { ApiState } from './api/api-state';
 import { UserState } from './user/user-state';
 
 export interface AppState {
+    api: ApiState;
     entities: NormalizedCacheObject;
     user: UserState;
 }

+ 7 - 2
admin-ui/src/app/state/state.module.ts

@@ -3,10 +3,13 @@ import { Store, StoreModule } from '@ngrx/store';
 import { apolloReducer, NgrxCache, NgrxCacheModule } from 'apollo-angular-cache-ngrx';
 import { InMemoryCache } from 'apollo-cache-inmemory';
 
-import { StateStore } from './state-store.service';
-import { UserActions } from './user/user-actions';
 import { environment } from '../../environments/environment';
+
 import { actionLogger } from './action-logger';
+import { ApiActions } from './api/api-actions';
+import { api } from './api/api-reducer';
+import { StateStore } from './state-store.service';
+import { UserActions } from './user/user-actions';
 import { user } from './user/user-reducer';
 
 export const APOLLO_NGRX_CACHE = new InjectionToken<InMemoryCache>('APOLLO_NGRX_CACHE');
@@ -22,6 +25,7 @@ export const metaReducers = environment.production ? [] : [actionLogger];
         NgrxCacheModule,
         StoreModule.forRoot({
             entities: apolloReducer,
+            api,
             user,
         }, { metaReducers }),
         NgrxCacheModule.forRoot('entities'),
@@ -34,6 +38,7 @@ export const metaReducers = environment.production ? [] : [actionLogger];
         },
         UserActions,
         StateStore,
+        ApiActions,
     ],
 })
 export class StateModule {}