import { Component , OnInit , Inject } from '@angular/core' ; import { MAT_DIALOG_DATA , MatDialogRef } from '@angular/material' ; import { FormBuilder , FormGroup , FormArray } from '@angular/forms' ; import { IDEATION_STAGE_QUESTIONS , PRODUCT_DEVELOPMENT_STAGE_QUESTIONS , LAUNCHING_AND_TESTING_STAGE_QUESTIONS , CONSUMER_FEEDBACK_AND_ITERATION_STAGE_QUESTIONS , FUNDING_STAGE_QUESTIONS } from 'app/shared/constants' ; import { ProjectService } from '@app/core/project/project.service' ; import { ErrorHandlerService } from '@app/core/error-handler.service' ; @ Component ( { selector : 'app-review' , templateUrl : './review.component.html' , styleUrls : [ './review.component.scss' ] } ) export class ReviewComponent implements OnInit { reviewForm : FormGroup ; questionsObject = { } ; totalQuestions = 0 ; isIncomplete = false ; errorMessage : string ; constructor ( public dialogRef , @ Inject ( MAT_DIALOG_DATA ) public data , private fb , private projectService , private errorHandler ) { switch ( data . context ) { case 'ideation_stage' : this . questionsObject = IDEATION_STAGE_QUESTIONS ; break ; case 'prototype_development_stage' : this . questionsObject = PRODUCT_DEVELOPMENT_STAGE_QUESTIONS ; break ; case 'consumer_feedback_stage' : this . questionsObject = CONSUMER_FEEDBACK_AND_ITERATION_STAGE_QUESTIONS ; break ; case 'launching_and_testing_stage' : this . questionsObject = LAUNCHING_AND_TESTING_STAGE_QUESTIONS ; break ; case 'funding_stage' : this . questionsObject = FUNDING_STAGE_QUESTIONS ; break ; default : this . questionsObject = IDEATION_STAGE_QUESTIONS ; break ; } this . totalQuestions = Object . keys ( this . questionsObject ) . length ; const formFields = { } ; Object . keys ( this . questionsObject ) . forEach ( fieldNames => { formFields [ fieldNames ] = [ '' ] ; } ) ; this . reviewForm = this . fb . group ( formFields ) ; } onNoClick ( ) { this . dialogRef . close ( ) ; } ngOnInit ( ) { } submitReviewForm ( ) { const answersObject = this . reviewForm . value ; let flag = 0 ; Object . keys ( answersObject ) . forEach ( answerKey => { if ( answersObject [ answerKey ] . trim ( ) === '' ) { flag = 1 ; } } ) ; if ( flag === 0 ) { this . errorMessage = '' ; } else { this . errorMessage = 'Please answer all the questions properly.' ; } if ( ! this . errorMessage ) { answersObject [ 'project_id' ] = this . data . projectId ; this . projectService . applyForReview ( answersObject , this . data . context ) . subscribe ( ( arg ) => { this . data . context = 'waiting' ; } , error => { this . errorHandler . subj_notification . next ( error ) ; } ) ; } } }