Skip to content
Snippets Groups Projects
request-cache.reducer.ts 2.92 KiB
Newer Older
Art Lowel's avatar
Art Lowel committed
import { PaginationOptions } from "../shared/pagination-options.model";
import { SortOptions } from "../shared/sort-options.model";
import {
Art Lowel's avatar
Art Lowel committed
  RequestCacheAction, RequestCacheActionTypes, FindAllRequestCacheAction,
  RequestCacheSuccessAction, RequestCacheErrorAction, FindByIDRequestCacheAction
} from "./request-cache.actions";
Art Lowel's avatar
Art Lowel committed
import { OpaqueToken } from "@angular/core";

Art Lowel's avatar
Art Lowel committed
export interface CachedRequest {
Art Lowel's avatar
Art Lowel committed
  service: OpaqueToken
  scopeID: string;
  resourceID: string;
  resourceUUIDs: Array<String>;
  resourceType: String;
  isLoading: boolean;
  errorMessage: string;
  paginationOptions: PaginationOptions;
  sortOptions: SortOptions;
  timeAdded: number;
  msToLive: number;
}

Art Lowel's avatar
Art Lowel committed
export interface RequestCacheState {
  [key: string]: CachedRequest
Art Lowel's avatar
Art Lowel committed
}

// Object.create(null) ensures the object has no default js properties (e.g. `__proto__`)
const initialState = Object.create(null);

Art Lowel's avatar
Art Lowel committed
export const requestCacheReducer = (state = initialState, action: RequestCacheAction): RequestCacheState => {
Art Lowel's avatar
Art Lowel committed
  switch (action.type) {

Art Lowel's avatar
Art Lowel committed
    case RequestCacheActionTypes.FIND_ALL_REQUEST: {
      return findAllRequest(state, <FindAllRequestCacheAction> action);
Art Lowel's avatar
Art Lowel committed
    case RequestCacheActionTypes.FIND_BY_ID_REQUEST: {
      return findByIDRequest(state, <FindByIDRequestCacheAction> action);
Art Lowel's avatar
Art Lowel committed
    case RequestCacheActionTypes.SUCCESS: {
      return success(state, <RequestCacheSuccessAction> action);
Art Lowel's avatar
Art Lowel committed
    case RequestCacheActionTypes.ERROR: {
      return error(state, <RequestCacheErrorAction> action);
Art Lowel's avatar
Art Lowel committed
    }

    default: {
      return state;
    }
  }
};

Art Lowel's avatar
Art Lowel committed
function findAllRequest(state: RequestCacheState, action: FindAllRequestCacheAction): RequestCacheState {
Art Lowel's avatar
Art Lowel committed
  return Object.assign({}, state, {
    [action.payload.key]: {
      service: action.payload.service,
      scopeID: action.payload.scopeID,
      resourceUUIDs: [],
      isLoading: true,
      errorMessage: undefined,
      paginationOptions: action.payload.paginationOptions,
      sortOptions: action.payload.sortOptions
    }
  });
}

Art Lowel's avatar
Art Lowel committed
function findByIDRequest(state: RequestCacheState, action: FindByIDRequestCacheAction): RequestCacheState {
Art Lowel's avatar
Art Lowel committed
  return Object.assign({}, state, {
    [action.payload.key]: {
      service: action.payload.service,
      resourceID: action.payload.resourceID,
      resourceUUIDs: [],
      isLoading: true,
      errorMessage: undefined,
    }
  });
}

Art Lowel's avatar
Art Lowel committed
function success(state: RequestCacheState, action: RequestCacheSuccessAction): RequestCacheState {
Art Lowel's avatar
Art Lowel committed
  return Object.assign({}, state, {
    [action.payload.key]: Object.assign({}, state[action.payload.key], {
      isLoading: false,
      resourceUUIDs: action.payload.resourceUUIDs,
      errorMessage: undefined
    })
  });
}

Art Lowel's avatar
Art Lowel committed
function error(state: RequestCacheState, action: RequestCacheErrorAction): RequestCacheState {
Art Lowel's avatar
Art Lowel committed
  return Object.assign({}, state, {
    [action.payload.key]: Object.assign({}, state[action.payload.key], {
      isLoading: false,
      errorMessage: action.payload.errorMessage
    })
  });
}