{"size": 2592, "ext": "js", "max_stars_repo_path": "src/pages/BasePersonalisationPage.js", "max_stars_repo_name": "mengnans/swen90013-2018-as", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/pages/BasePersonalisationPage.js", "max_issues_repo_name": "mengnans/swen90013-2018-as", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/pages/BasePersonalisationPage.js", "max_forks_repo_name": "mengnans/swen90013-2018-as", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.2842105263, "max_line_length": 72, "alphanum_fraction": 0.5597993827}
/* @flow */

import React from "react";
import { browserHistory } from "react-router";

import BaseCategoriesPage from "./BaseCategoriesPage";
import storage from "../storage";

class BasePersonalisationPage extends BaseCategoriesPage {
    constructor(props: Object) {
        super(props);
    }

    static contextTypes = {
        router: React.PropTypes.object.isRequired,
    };

    static childContextTypes = {
        controller: React.PropTypes.instanceOf(BasePersonalisationPage),
    };

    getChildContext(): Object {
        return {
            controller: this,
        };
    }

    previousStep(): void {
        browserHistory.goBack();
    }

    nextStep(): void {
        // If our subpage has an onNextStep hook, call it.
        this.refs.subpage &&
        this.refs.subpage.onNextStep &&
        this.refs.subpage.onNextStep();
    }

    urlFor(subpath: string): string {
        // Rewrites the URL based on search location/personalisation
        const parts = this.props.location.pathname.split("/");
        const location = storage.getLocation();

        if (location) {
            const newUrlLocation = location
                .split(", ")
                .map(encodeURIComponent)
                .join("-");

            // If URL has suburb, replace the existing suburb
            if (parts.length > 3 && parts[3].includes("-")) {
                parts.splice(3, 1, newUrlLocation)
            } else if (parts.length > 2 && parts[2].includes("-")) {
                parts.splice(2, 1, newUrlLocation)
            } else {
                // We didn't find any suburb
                // just add the new location to the url
                if (parts[1].includes("search")) {
                    parts.splice(3, 0, newUrlLocation)
                } else {
                    parts.splice(2, 0, newUrlLocation)
                }
            }
        }

        // Replace everything after 'personalise'
        parts.splice(
            parts.indexOf("personalise"),
            parts.length,
            subpath
        )

        return parts.join('/');
    }

    navigate(subpath: string): void {
        this.context.router.push(
            this.urlFor(subpath),
        );
    }

    get currentComponent(): ?ReactClass<*> {
        return this.personalisationComponents[this.currentComponentIdx];
    }

    get currentComponentIdx(): number {
        return this.personalisationComponents.findIndex(component =>
            component.defaultProps.name == this.props.params.subpage
        );
    }

}

export default BasePersonalisationPage;
