File

src/app/core/ngxs/interview.state.ts

Description

State for managing the interviews of the application.

Has: Action handlers to read, write, update and delete a contact person. Static and dynamic selectors to select interviews.

Index

Methods

Constructor

constructor(interviewService: InterviewService, questionService: QuestionService, answerService: AnswerService)
Parameters :
Name Type Optional
interviewService InterviewService No
questionService QuestionService No
answerService AnswerService No

Methods

addInterview
addInterview(undefined: StateContext, undefined: AddInterview)
Decorators :
@Action(AddInterview)
Parameters :
Name Type Optional
StateContext<InterviewStateModel> No
AddInterview No
Returns : void
Static answers
answers(state: InterviewStateModel)
Decorators :
@Selector()
Parameters :
Name Type Optional
state InterviewStateModel No
Returns : any
Static interview
interview(id: number)
Parameters :
Name Type Optional
id number No
Returns : any
Static interviewsByAuditId
interviewsByAuditId(auditId: number)
Parameters :
Name Type Optional
auditId number No
Returns : any
ngxsOnInit
ngxsOnInit(undefined: StateContext)
Parameters :
Name Type Optional
StateContext<InterviewStateModel> No
Returns : void
Static question
question(id: number)
Parameters :
Name Type Optional
id number No
Returns : any
Static questions
questions(state: InterviewStateModel)
Decorators :
@Selector()
Parameters :
Name Type Optional
state InterviewStateModel No
Returns : any
updateAnswer
updateAnswer(undefined: StateContext, undefined: UpdateAnswer)
Decorators :
@Action(UpdateAnswer)
Parameters :
Name Type Optional
StateContext<InterviewStateModel> No
UpdateAnswer No
Returns : void
updateInterview
updateInterview(undefined: StateContext, undefined: UpdateInterview)
Decorators :
@Action(UpdateInterview)
Parameters :
Name Type Optional
StateContext<InterviewStateModel> No
UpdateInterview No
Returns : void
import { State, Action, StateContext, NgxsOnInit, createSelector, Selector } from '@ngxs/store';
import { patch, append, updateItem } from '@ngxs/store/operators';
import { Injectable } from '@angular/core';
import { Interview } from '../data/models/interview.model';
import { AddInterview, UpdateInterview, UpdateAnswer } from './actions/inteview.actions';
import { InterviewService } from '../http/interview.service';
import { QuestionService } from '../http/question.service';
import { Answer } from '../data/models/answer.model';
import { Question } from '../data/models/question.model';
import { forkJoin } from 'rxjs';
import { AnswerService } from '../http/answer.service';

export interface InterviewStateModel {
  interviews: Interview[];
  answers: Answer[];
  questions: Question[];
}

/**
 * State for managing the interviews of the application.
 *
 * Has: Action handlers to read, write, update and delete a contact person.
 * Static and dynamic selectors to select interviews.
 */
@State<InterviewStateModel>({
  name: 'interviewState',
})
@Injectable()
export class InterviewState implements NgxsOnInit {
  constructor(
    private interviewService: InterviewService,
    private questionService: QuestionService,
    private answerService: AnswerService,
  ) {}

  ngxsOnInit({ patchState }: StateContext<InterviewStateModel>) {
    this.interviewService.getInterviews().subscribe(interviews => {
      const answers: Answer[] = [].concat.apply(
        [],
        interviews.map(i => i.answers),
      );

      const questions$ = [];
      for (const answer of answers) {
        questions$.push(this.questionService.getQuestion(answer.questionId));
      }

      forkJoin([...questions$]).subscribe((questions: Question[]) => {
        const distinctQuestions: Question[] = [];

        for (const question of questions) {
          const included = distinctQuestions.find(q => q.id === question.id);
          if (!included) {
            distinctQuestions.push(question);
          }
        }

        patchState({
          interviews,
          answers,
          questions: distinctQuestions,
        });
      });
    });
  }

  @Selector()
  static answers(state: InterviewStateModel) {
    return state.answers ?? [];
  }

  @Selector()
  static questions(state: InterviewStateModel) {
    return state.questions ?? [];
  }

  static question(id: number) {
    return createSelector([InterviewState], (state: InterviewStateModel) => {
      const result = state.questions.find(x => x.id === id);
      return result;
    });
  }

  static interview(id: number) {
    return createSelector([InterviewState], (state: InterviewStateModel) => {
      return state.interviews.find(x => x.id === id);
    });
  }

  static interviewsByAuditId(auditId: number) {
    return createSelector([InterviewState], (state: InterviewStateModel) => {
      return state.interviews.filter(x => x.auditId === auditId);
    });
  }

  @Action(AddInterview)
  addInterview(
    { setState, getState }: StateContext<InterviewStateModel>,
    { interview, interviewScope }: AddInterview,
  ) {
    this.interviewService.postInterview(interview, interviewScope).subscribe(interview => {
      setState(
        patch({
          interviews: append<Interview>([interview]),
          answers: append<Answer>([...interview.answers]),
        }),
      );

      const questions = getState().questions;

      for (const answer of interview.answers) {
        if (!questions?.find(q => q.id === answer.questionId)) {
          this.questionService.getQuestion(answer.questionId).subscribe(question => {
            setState(
              patch({
                questions: append<Question>([question]),
              }),
            );
          });
        }
      }
    });
  }

  @Action(UpdateInterview)
  updateInterview(
    { getState, setState }: StateContext<InterviewStateModel>,
    { id, interview }: UpdateInterview,
  ) {
    const i = getState().interviews.find(x => x.id === id);
    this.interviewService.putInterview({ ...i, ...interview }).subscribe(interview => {
      setState(
        patch({
          interviews: updateItem<Interview>(x => x.id === id, { ...i, ...interview }),
        }),
      );
    });
  }

  @Action(UpdateAnswer)
  updateAnswer({ setState }: StateContext<InterviewStateModel>, { answer }: UpdateAnswer) {
    this.answerService.putAnswer(answer).subscribe(answer => {
      setState(
        patch({
          answers: updateItem<Answer>(
            a =>
              a.questionId === answer.questionId &&
              a.faccritId === answer.faccritId &&
              a.interviewId === answer.interviewId,
            answer,
          ),
        }),
      );
    });
  }
}

result-matching ""

    No results matching ""