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

import datasets from '../../utilities/api-clients/datasets';
import notifications from '../../utilities/notifications';
import url from '../../utilities/url';
import log, {eventTypes} from '../../utilities/log';

import SimpleSelectableList from '../../components/simple-selectable-list/SimpleSelectableList';
import Input from '../../components/Input';

const propTypes = {
    dispatch: PropTypes.func.isRequired,
    location: PropTypes.shape({
        pathname: PropTypes.string.isRequired
    }).isRequired
}

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

        this.state = {
            isFetchingDatasets: false,
            datasets: [],
            filteredDatasets: [],
            searchTerm: ""
        }

    }

    componentWillMount() {
        return this.getAllDatasets();
    }

    getAllDatasets() {
        this.setState({isFetchingDatasets: true});
        return datasets.getAll().then(datasets => {
            this.setState({
                isFetchingDatasets: false, 
                datasets: this.mapDatasetsToState(datasets.items)
            })
        }).catch(error => {
            switch (error.status) {
                case(403):{
                    const notification = {
                        "type": "neutral",
                        "message": "You do not permission to view submitted datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification)
                    break;
                }
                case(404):{
                    const notification = {
                        "type": "warning",
                        "message": "No API route available to get all submitted datasets",
                        isDismissable: true
                    }
                    notifications.add(notification)
                    break;
                }
                case("RESPONSE_ERR"):{
                    const notification = {
                        "type": "warning",
                        "message": "An error's occurred whilst trying to get the submitted 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 the submitted datasets. Please check you internet connection and try again in a few moments.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
                default: {
                    const notification = {
                        type: "warning",
                        message: "An unexpected error's occurred whilst trying to get the submitted datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
            }
            console.error("Error getting datasets:\n", error);
            this.setState({isFetchingDatasets: false});
        });
    }

    handleBackButton = () => {
        const previousUrl = url.resolve("../");
        this.props.dispatch(push(previousUrl));
    }

    handleSearchInput = event => {
        const searchTerm = event.target.value.toLowerCase();
        const filteredDatasets = this.state.datasets.filter(dataset => {
            return (
                dataset.title.toLowerCase().search(searchTerm) !== -1
            );
        });
        this.setState({
            filteredDatasets,
            searchTerm
        });
    }

    mapDatasetsToState = datasets => {
        try {
            return datasets.map(dataset => {
                dataset = dataset.current || dataset.next || dataset;
                return {
                    title: dataset.title, 
                    id: dataset.id,
                    url: this.props.location.pathname + "/" + dataset.id
                }
            })
        } catch (error) {
            log.add(eventTypes.unexpectedRuntimeError, {message: `Error mapping datasets to to state. \n ${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
            }<commit_msg>If no title use ID as fallback<commit_after>import React, { Component } from 'react';
import { push } from 'react-router-redux';
import PropTypes from 'prop-types';

import datasets from '../../utilities/api-clients/datasets';
import notifications from '../../utilities/notifications';
import url from '../../utilities/url';
import log, {eventTypes} from '../../utilities/log';

import SimpleSelectableList from '../../components/simple-selectable-list/SimpleSelectableList';
import Input from '../../components/Input';

const propTypes = {
    dispatch: PropTypes.func.isRequired,
    location: PropTypes.shape({
        pathname: PropTypes.string.isRequired
    }).isRequired
}

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

        this.state = {
            isFetchingDatasets: false,
            datasets: [],
            filteredDatasets: [],
            searchTerm: ""
        }

    }

    componentWillMount() {
        return this.getAllDatasets();
    }

    getAllDatasets() {
        this.setState({isFetchingDatasets: true});
        return datasets.getAll().then(datasets => {
            this.setState({
                isFetchingDatasets: false, 
                datasets: this.mapDatasetsToState(datasets.items)
            })
        }).catch(error => {
            switch (error.status) {
                case(403):{
                    const notification = {
                        "type": "neutral",
                        "message": "You do not permission to view submitted datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification)
                    break;
                }
                case(404):{
                    const notification = {
                        "type": "warning",
                        "message": "No API route available to get all submitted datasets",
                        isDismissable: true
                    }
                    notifications.add(notification)
                    break;
                }
                case("RESPONSE_ERR"):{
                    const notification = {
                        "type": "warning",
                        "message": "An error's occurred whilst trying to get the submitted 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 the submitted datasets. Please check you internet connection and try again in a few moments.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
                default: {
                    const notification = {
                        type: "warning",
                        message: "An unexpected error's occurred whilst trying to get the submitted datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
            }
            console.error("Error getting datasets:\n", error);
            this.setState({isFetchingDatasets: false});
        });
    }

    handleBackButton = () => {
        const previousUrl = url.resolve("../");
        this.props.dispatch(push(previousUrl));
    }

    handleSearchInput = event => {
        const searchTerm = event.target.value.toLowerCase();
        const filteredDatasets = this.state.datasets.filter(dataset => {
            return (
                dataset.title.toLowerCase().search(searchTerm) !== -1
            );
        });
        this.setState({
            filteredDatasets,
            searchTerm
        });
    }

    mapDatasetsToState = datasets => {
        try {
            return datasets.map(dataset => {
                dataset = dataset.current || dataset.next || dataset;
                return {
                    title: dataset.title || dataset.id, 
                    id: dataset.id,
                    url: this.props.location.pathname + "/" + dataset.id
                }
            })
        } catch (error) {
            log.add(eventTypes.unexpectedRuntimeError, {message: `Error mapping datasets to to state. \n ${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
            }
{'fraction_non_alphanumeric': 0.09772562052134177, 'fraction_numerical': 0.0014539412192335654, 'mean_word_length': 1.3591376776090152, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 17, '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': '2973774', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2144, 'n_words': 551}