{"size": 2458, "ext": "js", "max_stars_repo_path": "src/output_lib/components/RespondToForm/Section.js", "max_stars_repo_name": "dailydrip/formulae_react", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/output_lib/components/RespondToForm/Section.js", "max_issues_repo_name": "dailydrip/formulae_react", "max_issues_repo_issues_event_min_datetime": "2017-04-27T03:36:05.000Z", "max_issues_repo_issues_event_max_datetime": "2018-01-23T21:16:27.000Z", "max_forks_repo_path": "src/output_lib/components/RespondToForm/Section.js", "max_forks_repo_name": "dailydrip/formulae_react", "max_forks_repo_forks_event_min_datetime": "2018-01-11T19:38:26.000Z", "max_forks_repo_forks_event_max_datetime": "2018-01-17T19:39:15.000Z"}
{"max_stars_count": null, "max_issues_count": 30, "max_forks_count": 2, "avg_line_length": 23.4095238095, "max_line_length": 79, "alphanum_fraction": 0.6562245728}
// @flow

import React from "react";
import { List } from "immutable";
import {
  SectionType,
  QuestionType,
  QuestionSubmissionType,
  AddressType
} from "../../types";
// Having a hard time globbing together just type exports, so pulling this in
// directly
import type {
  QuestionSubmissionsMapType
} from "../../types/QuestionSubmissionsMapType";
import type {
  QuestionSubmissionMapValueType
} from "../../types/QuestionSubmissionMapValueType";
import Question from "./Question";

type Props = {
  section: SectionType,
  submissions: QuestionSubmissionsMapType,
  setSubmission: Function,
  addError: Function,
  removeError: Function,
  errors: Object
};

export default function Section(props: Props) {
  const {
    section,
    submissions,
    setSubmission,
    addError,
    removeError,
    errors
  } = props;
  const questions = generateQuestions(
    section.get("questions"),
    submissions,
    setSubmission,
    addError,
    removeError,
    errors
  );
  return (
    <section className="form-section">
      <h2>{section.get("name")}</h2>
      <p>{section.get("content")}</p>
      {questions}
    </section>
  );
}

function generateQuestions(
  questions: List<QuestionType>,
  submissions: QuestionSubmissionsMapType,
  setSubmission: Function,
  addError: Function,
  removeError: Function,
  errors
): Array<Question> {
  if (questions === undefined) {
    return [];
  } else {
    return questions
      .sortBy(question => question.order)
      .map((question, i) => {
        return (
          <Question
            key={i}
            question={question}
            submission={getSubmission(question, submissions)}
            submissions={submissions}
            setSubmission={setSubmission}
            addError={addError}
            removeError={removeError}
            errorMessage={errors.get(`${question.get("id")}`)}
          />
        );
      })
      .toJS();
  }
}

function getSubmission(
  question: QuestionType,
  submissions: QuestionSubmissionsMapType
): QuestionSubmissionMapValueType {
  return submissions.get(String(question.get("id"))) ||
    List([getDefaultSubmission(question)]);
}

function getDefaultSubmission(question: QuestionType): QuestionSubmissionType {
  if (question.type === "address") {
    return new QuestionSubmissionType({
      key: question.get("id"),
      value: new AddressType()
    });
  }

  return new QuestionSubmissionType({ key: question.get("id") });
}
