<commit_before>import React, { Component } from "react";
import { connect } from "react-redux";
import { push } from "react-router-redux";
import PropTypes from "prop-types";

import { addNewJob, updateAllRecipes } from "../../../config/actions";
import recipes from "../../../utilities/api-clients/recipes";
import datasetImport from "../../../utilities/api-clients/datasetImport";
import notifications from "../../../utilities/notifications";
import log from "../../../utilities/logging/log";

import DatasetUploadRecipe from "./uploads-components/DatasetUploadRecipe";
import Input from "../../../components/Input";

const propTypes = {
    dispatch: PropTypes.func.isRequired,
    recipes: PropTypes.arrayOf(
        PropTypes.shape({
            id: PropTypes.string.isRequired,
            alias: PropTypes.string
        })
    )
};

export class DatasetUploadsController extends Component {
    constructor(props) {
        super(props);

        this.state = {
            isFetchingData: false,
            disabledDataset: "",
            datasets: [],
            filteredDatasets: [],
            searchTerm: ""
        };

        this.handleNewVersionClick = this.handleNewVersionClick.bind(this);
    }

    componentWillMount() {
        this.setState({ isFetchingData: true });

        const getRecipes = recipes.getAll();

        getRecipes
            .then(response => {
                this.props.dispatch(updateAllRecipes(response.items));
                this.setState({
                    isFetchingData: false,
                    datasets: this.mapDatasetsToState(response.items)
                });
            })
            .catch(error => {
                switch (error.status) {
                    case 403: {
                        const notification = {
                            type: "warning",
                            message: "You do not permission to view all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "RESPONSE_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An error's occurred whilst trying to get all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "FETCH_ERR": {
                        const notification = {
                            type: "warning",
                            message:
                                "There's been a network error whilst trying to get all datasets. Please check you internet connection and try again in a few moments.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "UNEXPECTED_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error has occurred whilst trying to get all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    default: {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error's occurred whilst trying to get all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                }
                this.setState({ isFetchingData: false });
            });
    }

    handleNewVersionClick(event) {
        const recipeID = event.target.getAttribute("data-recipe-id");
        this.setState({ disabledDataset: recipeID });
        datasetImport
            .create(recipeID)
            .then(newJob => {
                this.props.dispatch(addNewJob(newJob));
                this.props.dispatch(push(`${location.pathname}/${newJob.id}`));
            })
            .catch(error => {
                this.setState({ disabledDataset: "" });
                switch (error.status) {
                    case 403: {
                        const notification = {
                            type: "warning",
                            message: "You do not have permission to upload a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case 404: {
                        const notification = {
                            type: "warning",
                            message: "This dataset was not recognised.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "RESPONSE_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An error's occurred whilst trying to create a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "FETCH_ERR": {
                        const notification = {
                            type: "warning",
                            message:
                                "There's been a network error whilst trying to create a new version of this dataset. Please check you internet connection and try again in a few moments.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "UNEXPECTED_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error has occurred whilst trying to create a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    default: {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error's occurred whilst trying to create a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                }
                console.error("Error creating new import job: ", error);
            });
    }

    mapDatasetsToState = datasets => {
        try {
            return datasets.map(dataset => {
                return {
                    alias: dataset.alias || dataset.id,
                    id: dataset.id
                };
            });
        } catch (error) {
            log.event("Error mapping datasets to to state.", log.error(error));
            const notification = {
                type: "warning",
                message:
                    "An unexpected error occurred when trying to get datasets, so some functionality in Florence may not work as expected. Try refreshing the page",
                isDismissable: true
            };
            notifications.add(notification);
            console.error("Error mapping datasets to state:\n", error);<commit_msg>add sort step in mapDatasetsToState func to order recipes alphabetically<commit_after>import React, { Component } from "react";
import { connect } from "react-redux";
import { push } from "react-router-redux";
import PropTypes from "prop-types";

import { addNewJob, updateAllRecipes } from "../../../config/actions";
import recipes from "../../../utilities/api-clients/recipes";
import datasetImport from "../../../utilities/api-clients/datasetImport";
import notifications from "../../../utilities/notifications";
import log from "../../../utilities/logging/log";

import DatasetUploadRecipe from "./uploads-components/DatasetUploadRecipe";
import Input from "../../../components/Input";

const propTypes = {
    dispatch: PropTypes.func.isRequired,
    recipes: PropTypes.arrayOf(
        PropTypes.shape({
            id: PropTypes.string.isRequired,
            alias: PropTypes.string
        })
    )
};

export class DatasetUploadsController extends Component {
    constructor(props) {
        super(props);

        this.state = {
            isFetchingData: false,
            disabledDataset: "",
            datasets: [],
            filteredDatasets: [],
            searchTerm: ""
        };

        this.handleNewVersionClick = this.handleNewVersionClick.bind(this);
    }

    componentWillMount() {
        this.setState({ isFetchingData: true });

        const getRecipes = recipes.getAll();

        getRecipes
            .then(response => {
                this.props.dispatch(updateAllRecipes(response.items));
                this.setState({
                    isFetchingData: false,
                    datasets: this.mapDatasetsToState(response.items)
                });
            })
            .catch(error => {
                switch (error.status) {
                    case 403: {
                        const notification = {
                            type: "warning",
                            message: "You do not permission to view all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "RESPONSE_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An error's occurred whilst trying to get all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "FETCH_ERR": {
                        const notification = {
                            type: "warning",
                            message:
                                "There's been a network error whilst trying to get all datasets. Please check you internet connection and try again in a few moments.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "UNEXPECTED_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error has occurred whilst trying to get all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    default: {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error's occurred whilst trying to get all datasets.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                }
                this.setState({ isFetchingData: false });
            });
    }

    handleNewVersionClick(event) {
        const recipeID = event.target.getAttribute("data-recipe-id");
        this.setState({ disabledDataset: recipeID });
        datasetImport
            .create(recipeID)
            .then(newJob => {
                this.props.dispatch(addNewJob(newJob));
                this.props.dispatch(push(`${location.pathname}/${newJob.id}`));
            })
            .catch(error => {
                this.setState({ disabledDataset: "" });
                switch (error.status) {
                    case 403: {
                        const notification = {
                            type: "warning",
                            message: "You do not have permission to upload a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case 404: {
                        const notification = {
                            type: "warning",
                            message: "This dataset was not recognised.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "RESPONSE_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An error's occurred whilst trying to create a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "FETCH_ERR": {
                        const notification = {
                            type: "warning",
                            message:
                                "There's been a network error whilst trying to create a new version of this dataset. Please check you internet connection and try again in a few moments.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    case "UNEXPECTED_ERR": {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error has occurred whilst trying to create a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                    default: {
                        const notification = {
                            type: "warning",
                            message: "An unexpected error's occurred whilst trying to create a new version of this dataset.",
                            isDismissable: true
                        };
                        notifications.add(notification);
                        break;
                    }
                }
                console.error("Error creating new import job: ", error);
            });
    }

    mapDatasetsToState = datasets => {
        try {
            datasets.sort((a, b) => a.alias.localeCompare(b.alias));
            return datasets.map(dataset => {
                return {
                    alias: dataset.alias || dataset.id,
                    id: dataset.id
                };
            });
        } catch (error) {
            log.event("Error mapping datasets to to state.", log.error(error));
            const notification = {
                type: "warning",
                message:
                    "An unexpected error occurred when trying to get datasets, so some functionality in Florence may not work as expected. Try refreshing the page",
                isDismissable: true
            };
            notifications.add(notification);
            console.error("Error mapping datasets to state:\n", error);
{'fraction_non_alphanumeric': 0.08173549655850541, 'fraction_numerical': 0.0011061946902654867, 'mean_word_length': 1.0229985082048731, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 16, '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': '1690703', 'n_tokens_mistral': 3400, 'n_tokens_neox': 3237, 'n_words': 893}