File

src/app/modules/program/services/program-stage/program-stage.service.ts

Constructor

constructor()

Methods

initialize
initialize()
Returns: void
addStage
addStage(stage: any)
Returns: void
removeStage
removeStage(item: any)
Returns: void
removeLastStage
removeLastStage()
Returns: void
getStage
getStage()
Returns: Observable<any>
getLastStage
getLastStage()
Returns: void

Properties

Private stageCount
stageCount: number
Default value: 1
Public stageObservable
stageObservable: Subject<InitialState>
Private stagesInService
stagesInService: InitialState
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import * as _ from 'lodash-es';

interface InitialState {
  stages: Array<{}>;
}

interface Stage {
  stageId: number;
  stage?: any;
}

@Injectable({
  providedIn: 'root'
})

export class ProgramStageService {
  private stagesInService: InitialState = {
    stages: []
  };
  private stageCount = 1;
  public stageObservable = new Subject<InitialState>();

  constructor() { }

  initialize() {
    let { stages } = this.stagesInService;
    this.stageCount = 1;
    stages = [];
    this.stagesInService = {
      stages: stages
    };
    this.stageObservable.next(this.stagesInService);
  }
  addStage(stage) {
    const stg = {
      stageId: this.stageCount++,
      stage: stage
    };
    this.stagesInService = {
      stages: [...this.stagesInService.stages, stg]
    };
    // this.stageObservable.next({stageId: this.stageCount++, stage: stage  });
    this.stageObservable.next(this.stagesInService);
  }

  removeStage(item) {
    const { stages } = this.stagesInService;
    const updatedStages = stages.filter((stage: Stage) => stage.stage !== item);
    this.stagesInService = {
      stages: updatedStages
    };
    // this.stagesInService.stages.filter(stg => stg.stage !== stage);
    this.stageObservable.next(this.stagesInService);
  }

  removeLastStage() {
    const { stages } = this.stagesInService;
    stages.splice(-1);
    // const updatedStages = stages.pop();
    this.stagesInService = {
      stages: stages
    };
    // this.stagesInService.stages.filter(stg => stg.stage !== stage);
    this.stageObservable.next(this.stagesInService);
    this.stageCount--;
  }

  getStage(): Observable<any> {
    return this.stageObservable.asObservable();
  }

  getLastStage() {
    const {stages} = this.stagesInService;
    return _.get(_.last(stages), 'stage');
  }
}

results matching ""

    No results matching ""