<commit_before>// @flow

import React from "react";
import Section from "../enums/Section.js";
import Headers from "../common/Headers.jsx";
import EventOverviewForm from "../componentsBySection/CreateEvent/EventOverviewForm.jsx";
import EventPreviewForm from "../componentsBySection/CreateEvent/EventPreviewForm.jsx";
import EventDescriptionForm from "../componentsBySection/CreateEvent/EventDescriptionForm.jsx";
import EventAPIUtils, {EventData} from "../utils/EventAPIUtils.js";
import api from "../utils/api.js";
import url from "../utils/url.js";
import utils from "../utils/utils.js";
import FormWorkflow, {FormWorkflowStepConfig} from "../forms/FormWorkflow.jsx";

type State = {|
  id: ?number,
  Event: ?EventData,
  steps: $ReadOnlyArray<FormWorkflowStepConfig>
|};

/**
 * Encapsulates form for creating Events
 */
class CreateEventController extends React.PureComponent<{||},State> {
  constructor(props: {||}): void {
    super(props);
    const eventId: number = url.argument("id");
    this.onNextPageSuccess = this.onNextPageSuccess.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
    this.onFinalSubmitSuccess = this.onFinalSubmitSuccess.bind(this);
    this.state = {
      eventId: eventId,
      steps: [
        {
          header: "Let's get started!",
          subHeader: "",
          onSubmit: this.onSubmit,
          onSubmitSuccess: this.onNextPageSuccess,
          formComponent: EventOverviewForm
        }, {
          header: "Let others know what your Event is about...",
          subHeader: "You can always change details about your Event later.",
          onSubmit: this.onSubmit,
          onSubmitSuccess: this.onNextPageSuccess,
          formComponent: EventDescriptionForm
        }, {
          header: "Ready to publish your Event?",
          subHeader: "Congratulations!  You have successfully created a tech-for-good Event.",
          onSubmit: this.onSubmit,
          onSubmitSuccess: this.onFinalSubmitSuccess,
          formComponent: EventPreviewForm
        }
      ]
    };
  }
  
  componentDidMount(): void {
    if(this.state.eventId) {
      EventAPIUtils.fetchEventDetails(this.state.eventId, this.loadEventDetails.bind(this), this.handleLoadEventError.bind(this));
    }
  }

  updatePageUrl() {
    if(this.state.eventId && !url.argument('id')) {
      url.updateArgs({id: this.state.eventId});
    }
    utils.navigateToTopOfPage();
  }
  
  loadEventDetails(event: EventData): void {
    if(!EventAPIUtils.isOwner(event)) {
      // TODO: Handle someone other than owner
    } else {
      this.setState({
        event: event,
        eventIsLoading: false
      });
    }
  }
  
  handleLoadEventError(error: APIError): void {
    this.setState({
      error: "Failed to load Event information"
    });
  }
  
  onSubmit(event: SyntheticEvent<HTMLFormElement>, formRef: HTMLFormElement, onSubmitSuccess: (EventData, () => void) => void): void {
    const formSubmitUrl: string = this.state.event && this.state.event.event_id<commit_msg>Bug Fix: Edit Event form doesn't appear for admin non-owner<commit_after>// @flow

import React from "react";
import Section from "../enums/Section.js";
import Headers from "../common/Headers.jsx";
import EventOverviewForm from "../componentsBySection/CreateEvent/EventOverviewForm.jsx";
import EventPreviewForm from "../componentsBySection/CreateEvent/EventPreviewForm.jsx";
import EventDescriptionForm from "../componentsBySection/CreateEvent/EventDescriptionForm.jsx";
import EventAPIUtils, {EventData} from "../utils/EventAPIUtils.js";
import api from "../utils/api.js";
import url from "../utils/url.js";
import utils from "../utils/utils.js";
import FormWorkflow, {FormWorkflowStepConfig} from "../forms/FormWorkflow.jsx";
import CurrentUser from "../utils/CurrentUser";

type State = {|
  id: ?number,
  Event: ?EventData,
  steps: $ReadOnlyArray<FormWorkflowStepConfig>
|};

/**
 * Encapsulates form for creating Events
 */
class CreateEventController extends React.PureComponent<{||},State> {
  constructor(props: {||}): void {
    super(props);
    const eventId: number = url.argument("id");
    this.onNextPageSuccess = this.onNextPageSuccess.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
    this.onFinalSubmitSuccess = this.onFinalSubmitSuccess.bind(this);
    this.state = {
      eventId: eventId,
      steps: [
        {
          header: "Let's get started!",
          subHeader: "",
          onSubmit: this.onSubmit,
          onSubmitSuccess: this.onNextPageSuccess,
          formComponent: EventOverviewForm
        }, {
          header: "Let others know what your Event is about...",
          subHeader: "You can always change details about your Event later.",
          onSubmit: this.onSubmit,
          onSubmitSuccess: this.onNextPageSuccess,
          formComponent: EventDescriptionForm
        }, {
          header: "Ready to publish your Event?",
          subHeader: "Congratulations!  You have successfully created a tech-for-good Event.",
          onSubmit: this.onSubmit,
          onSubmitSuccess: this.onFinalSubmitSuccess,
          formComponent: EventPreviewForm
        }
      ]
    };
  }
  
  componentDidMount(): void {
    if(this.state.eventId) {
      EventAPIUtils.fetchEventDetails(this.state.eventId, this.loadEventDetails.bind(this), this.handleLoadEventError.bind(this));
    }
  }

  updatePageUrl() {
    if(this.state.eventId && !url.argument('id')) {
      url.updateArgs({id: this.state.eventId});
    }
    utils.navigateToTopOfPage();
  }
  
  loadEventDetails(event: EventData): void {
    if(!EventAPIUtils.isOwner(event) && !CurrentUser.isStaff()) {
      // TODO: Handle someone other than owner/admin
    } else {
      this.setState({
        event: event,
        eventIsLoading: false
      });
    }
  }
  
  handleLoadEventError(error: APIError): void {
    this.setState({
      error: "Failed to load Event information"
    });
  }
  
  onSubmit(event: SyntheticEvent<HTMLFormElement>, formRef: HTMLFormElement, onSubmitSuccess: (EventData, () => void) => void): void {
    const formSubmitUrl: string = this.state.event && this.state.event.event_id
{'fraction_non_alphanumeric': 0.12463438414039649, 'fraction_numerical': 0.0, 'mean_word_length': 3.2013651877133107, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7317072', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1764, 'n_words': 395}