public void SetSectionStates(VacancyPreviewViewModel viewModel, ModelStateDictionary modelState)
 {
     viewModel.TitleSectionState                   = GetSectionState(viewModel, new[] { FieldIdentifiers.Title }, true, modelState, vm => vm.Title);
     viewModel.ShortDescriptionSectionState        = GetSectionState(viewModel, new[] { FieldIdentifiers.ShortDescription }, true, modelState, vm => vm.ShortDescription);
     viewModel.ClosingDateSectionState             = GetSectionState(viewModel, new[] { FieldIdentifiers.ClosingDate }, true, modelState, vm => vm.ClosingDate);
     viewModel.WorkingWeekSectionState             = GetSectionState(viewModel, new[] { FieldIdentifiers.WorkingWeek }, true, modelState, vm => vm.HoursPerWeek, vm => vm.WorkingWeekDescription);
     viewModel.WageTextSectionState                = GetSectionState(viewModel, new[] { FieldIdentifiers.Wage }, true, modelState, vm => vm.HasWage, vm => vm.WageText);
     viewModel.ExpectedDurationSectionState        = GetSectionState(viewModel, new[] { FieldIdentifiers.ExpectedDuration }, true, modelState, vm => vm.ExpectedDuration);
     viewModel.PossibleStartDateSectionState       = GetSectionState(viewModel, new[] { FieldIdentifiers.PossibleStartDate }, true, modelState, vm => vm.PossibleStartDate);
     viewModel.TrainingLevelSectionState           = GetSectionState(viewModel, new[] { FieldIdentifiers.TrainingLevel }, true, modelState, vm => vm.HasProgramme, vm => vm.TrainingLevel);
     viewModel.NumberOfPositionsSectionState       = GetSectionState(viewModel, new[] { FieldIdentifiers.NumberOfPositions }, true, modelState, vm => vm.NumberOfPositions);
     viewModel.DescriptionsSectionState            = GetSectionState(viewModel, new[] { FieldIdentifiers.VacancyDescription, FieldIdentifiers.TrainingDescription, FieldIdentifiers.OutcomeDescription }, true, modelState, vm => vm.VacancyDescription, vm => vm.TrainingDescription, vm => vm.OutcomeDescription);
     viewModel.SkillsSectionState                  = GetSectionState(viewModel, new[] { FieldIdentifiers.Skills }, true, modelState, vm => vm.Skills);
     viewModel.QualificationsSectionState          = GetSectionState(viewModel, new[] { FieldIdentifiers.Qualifications }, true, modelState, vm => vm.Qualifications);
     viewModel.ThingsToConsiderSectionState        = GetSectionState(viewModel, new[] { FieldIdentifiers.ThingsToConsider }, true, modelState, vm => vm.ThingsToConsider);
     viewModel.EmployerNameSectionState            = GetSectionState(viewModel, new[] { FieldIdentifiers.EmployerName }, true, modelState, vm => vm.EmployerName);
     viewModel.EmployerDescriptionSectionState     = GetSectionState(viewModel, new[] { FieldIdentifiers.EmployerDescription }, true, modelState, vm => vm.EmployerDescription);
     viewModel.EmployerWebsiteUrlSectionState      = GetSectionState(viewModel, new[] { FieldIdentifiers.EmployerWebsiteUrl }, true, modelState, vm => vm.EmployerWebsiteUrl);
     viewModel.EmployerContactSectionState         = GetSectionState(viewModel, new[] { FieldIdentifiers.EmployerContact }, false, modelState, vm => vm.EmployerContactName, vm => vm.EmployerContactEmail, vm => vm.EmployerContactTelephone);
     viewModel.EmployerAddressSectionState         = GetSectionState(viewModel, new[] { FieldIdentifiers.EmployerAddress }, true, modelState, vm => vm.EmployerAddressElements);
     viewModel.ApplicationInstructionsSectionState = GetSectionState(viewModel, new[] { FieldIdentifiers.ApplicationInstructions }, true, modelState, vm => vm.ApplicationInstructions);
     viewModel.ApplicationMethodSectionState       = GetSectionState(viewModel, new[] { FieldIdentifiers.ApplicationMethod }, true, modelState, vm => vm.ApplicationMethod);
     viewModel.ApplicationUrlSectionState          = GetSectionState(viewModel, new[] { FieldIdentifiers.ApplicationUrl }, true, modelState, vm => vm.ApplicationUrl);
     viewModel.ProviderSectionState                = GetSectionState(viewModel, new[] { FieldIdentifiers.Provider }, true, modelState, vm => vm.ProviderName);
     viewModel.TrainingSectionState                = GetSectionState(viewModel, new[] { FieldIdentifiers.Training }, true, modelState, vm => vm.TrainingType, vm => vm.TrainingTitle);
     viewModel.DisabilityConfidentSectionState     = GetSectionState(viewModel, new[] { FieldIdentifiers.DisabilityConfident }, true, modelState, vm => vm.IsDisabilityConfident);
 }
        private bool IsSectionComplete(VacancyPreviewViewModel vm, bool requiresAll, params Expression <Func <VacancyPreviewViewModel, object> >[] sectionProperties)
        {
            foreach (var requiredPropertyExpression in sectionProperties)
            {
                var requiredPropertyFunc = requiredPropertyExpression.Compile();
                var propertyValue        = requiredPropertyFunc(vm);

                var result = true;
                switch (propertyValue)
                {
                case null:
                    result = false;
                    break;

                case string stringProperty:
                    if (string.IsNullOrWhiteSpace(stringProperty))
                    {
                        result = false;
                    }
                    break;

                case IEnumerable listProperty:
                    if (listProperty.Cast <object>().Any() == false)
                    {
                        result = false;
                    }
                    break;

                case bool _:
                    //No way to tell if a bool has been 'completed' so just skip
                    break;

                default:
                    //Skipping other types for now
                    break;
                }

                if (requiresAll && result == false)
                {
                    return(false);
                }

                if (!requiresAll && result)
                {
                    return(true);
                }
            }

            return(requiresAll);
        }
        private VacancyPreviewSectionState GetSectionState(VacancyPreviewViewModel vm, IEnumerable <string> reviewFieldIndicators, bool requiresAll, ModelStateDictionary modelState, params Expression <Func <VacancyPreviewViewModel, object> >[] sectionProperties)
        {
            if (IsSectionModelStateValid(modelState, sectionProperties) == false)
            {
                return(IsSectionComplete(vm, requiresAll, sectionProperties) ?
                       VacancyPreviewSectionState.Invalid :
                       VacancyPreviewSectionState.InvalidIncomplete);
            }

            if (IsSectionForReview(vm, reviewFieldIndicators))
            {
                return(VacancyPreviewSectionState.Review);
            }

            return(IsSectionComplete(vm, requiresAll, sectionProperties) ?
                   VacancyPreviewSectionState.Valid :
                   VacancyPreviewSectionState.Incomplete);
        }
 private bool IsSectionForReview(VacancyPreviewViewModel vm, IEnumerable <string> reviewFieldIndicators)
 {
     return(reviewFieldIndicators != null && reviewFieldIndicators.Any(reviewFieldIndicator =>
                                                                       vm.Review.FieldIndicators.Select(r => r.ReviewFieldIdentifier)
                                                                       .Contains(reviewFieldIndicator)));
 }