<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 date from '../../../utilities/date'

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

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

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

        this.state = {
            isFetchingDataset: false,
            dataset: {},
            isFetchingEdition: false,
            edition: {},
            isFetchingVersions: false,
            versions: [
                {
                    title: "Create new version", 
                    id: "create-new-version",
                    url:  this.props.location.pathname + "/instances",
                }
            ]
        }

    }

    async componentWillMount() {
        const datasetID = this.props.params.datasetID;
        const editionID = this.props.params.editionID;

        this.getAllVersions(datasetID, editionID);
        this.getDataset(datasetID);
        this.getEdition(datasetID, editionID)
    }

    getDataset = datasetID => {
        this.setState({isFetchingDataset: true});
        return datasets.get(datasetID).then(dataset => {
            this.setState({isFetchingDataset: false, dataset: this.mapDatasetToState(dataset)});
            return this.mapDatasetToState(dataset);
        }).catch(error => {
            switch (error.status) {
                case(404): {
                    const notification = {
                        "type": "warning",
                        "message": "No API route available for a list of datasets. You should still be able to use this page, or you can refresh.",
                        isDismissable: true
                    }
                    notifications.add(notification)
                    break;
                }
                case("RESPONSE_ERR"):{
                    const notification = {
                        "type": "warning",
                        "message": "An error's occurred whilst trying to get a list of 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;
                }
                case("UNEXPECTED_ERR"): {
                    const notification = {
                        type: "warning",
                        message: "An unexpected error has occurred whilst trying to get a list of datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
                default: {
                    const notification = {
                        type: "warning",
                        message: "An unexpected error's occurred whilst trying to get a list of datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
            }
            console.error(`Error getting dataset (${datasetID}):\n`, error);
            this.setState({isFetchingDataset: false});
        })
    }

    mapDatasetToState = datasetResponse => {
        try {
            const dataset = datasetResponse.current || datasetResponse.next || datasetResponse;
            return {
                title: dataset.title
            }
        } catch (error) {
            const notification = {
                type: "warning",
                message: "An unexpected error occurred when trying to get dataset details, so some functionality in Florence may not work as expected. Try refreshing the page",
                isDismissable: true
            }
            notifications.add(notification);
            console.error("Error getting dataset details to state:\n", error);
        }
    }

    getEdition = (datasetID, editionID) => {
        this.setState({isFetchingEdition: true});
        return datasets.getEdition(datasetID, editionID).then(edition => {
            this.setState({isFetchingEdition: false, edition: this.mapDatasetEditionToState(edition)});
            return this.mapDatasetEditionToState(edition)
        }).catch(error => {
            console.error(error);
            this.setState({isFetchingEdition: false});
        })
    }

    mapDatasetEditionToState = editionResponse => {
        try {
            const edition = editionResponse.current || editionResponse.next || editionResponse;
            return {
                title: edition.edition 
            }
        } catch (error) {
            const notification = {
                type: "warning",
                message: "An unexpected error occurred when trying to get edition details, so some functionality in Florence may not work as expected. Try refreshing the page",
                isDismissable: true
            }
            notifications.add(notification);
            console.error("Error getting mapping edition to state:\n", error);
        }
    }

    getAllVersions = (datasetID, editions) => {
        this.setState({isFetchingVersions: true});
        return datasets.getVersions(datasetID, editions).then(versions => {
            this.setState({isFetchingVersions: false, versions: [...this.state.versions, ...this.mapDatasetVersionsToState(versions.items)]});
            return this.mapDatasetVersionsToState(versions.items);
        }).catch(error => {
            console.error(error)
            this.setState({isFetchingVersions: false});
        })
    }

    mapDatasetVersionsToState = versions => {
        try {
            const versionsList =  versions.map(version => {
                const published = version.state === "published" ? "(published)" : null;
                return {
                    id: version.id,
                    title: `Version: ${version.version} ${published ? published : ""}`,
                    url: this.props.location.pathname + "/versions/" + version.version,
                    version: version.version,
                    details: [
                        `Release date: ${date.format(version.release_date, "dd mmmm yyyy")}`
                    ]
                }    
            });
            versionsList.sort((this.sortByVersionNumber))
            return versionsList.reverse();
        } catch(error) {
            console.error(error);
        }
    }

    sortByVersionNumber = (a ,b) => {
        return a.version - b.version;
    }
    
    handleBackButton = () => {
        const previousUrl = url.resolve("../../");
        this.props.dispatch(push(previousUrl));
    }

    render() {
        return (
            <div className="grid grid--justify-center">
                <div className="grid__col-9">
                    <div className="margin-top--2">
                        &#9664; <button type="button" className="btn btn--link" onClick={this.handleBackButton}>Back</button><commit_msg>Display not release date if one isn't set<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 date from '../../../utilities/date'

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

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

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

        this.state = {
            isFetchingDataset: false,
            dataset: {},
            isFetchingEdition: false,
            edition: {},
            isFetchingVersions: false,
            versions: [
                {
                    title: "Create new version", 
                    id: "create-new-version",
                    url:  this.props.location.pathname + "/instances",
                }
            ]
        }

    }

    async componentWillMount() {
        const datasetID = this.props.params.datasetID;
        const editionID = this.props.params.editionID;

        this.getAllVersions(datasetID, editionID);
        this.getDataset(datasetID);
        this.getEdition(datasetID, editionID)
    }

    getDataset = datasetID => {
        this.setState({isFetchingDataset: true});
        return datasets.get(datasetID).then(dataset => {
            this.setState({isFetchingDataset: false, dataset: this.mapDatasetToState(dataset)});
            return this.mapDatasetToState(dataset);
        }).catch(error => {
            switch (error.status) {
                case(404): {
                    const notification = {
                        "type": "warning",
                        "message": "No API route available for a list of datasets. You should still be able to use this page, or you can refresh.",
                        isDismissable: true
                    }
                    notifications.add(notification)
                    break;
                }
                case("RESPONSE_ERR"):{
                    const notification = {
                        "type": "warning",
                        "message": "An error's occurred whilst trying to get a list of 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;
                }
                case("UNEXPECTED_ERR"): {
                    const notification = {
                        type: "warning",
                        message: "An unexpected error has occurred whilst trying to get a list of datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
                default: {
                    const notification = {
                        type: "warning",
                        message: "An unexpected error's occurred whilst trying to get a list of datasets.",
                        isDismissable: true
                    }
                    notifications.add(notification);
                    break;
                }
            }
            console.error(`Error getting dataset (${datasetID}):\n`, error);
            this.setState({isFetchingDataset: false});
        })
    }

    mapDatasetToState = datasetResponse => {
        try {
            const dataset = datasetResponse.current || datasetResponse.next || datasetResponse;
            return {
                title: dataset.title
            }
        } catch (error) {
            const notification = {
                type: "warning",
                message: "An unexpected error occurred when trying to get dataset details, so some functionality in Florence may not work as expected. Try refreshing the page",
                isDismissable: true
            }
            notifications.add(notification);
            console.error("Error getting dataset details to state:\n", error);
        }
    }

    getEdition = (datasetID, editionID) => {
        this.setState({isFetchingEdition: true});
        return datasets.getEdition(datasetID, editionID).then(edition => {
            this.setState({isFetchingEdition: false, edition: this.mapDatasetEditionToState(edition)});
            return this.mapDatasetEditionToState(edition)
        }).catch(error => {
            console.error(error);
            this.setState({isFetchingEdition: false});
        })
    }

    mapDatasetEditionToState = editionResponse => {
        try {
            const edition = editionResponse.current || editionResponse.next || editionResponse;
            return {
                title: edition.edition 
            }
        } catch (error) {
            const notification = {
                type: "warning",
                message: "An unexpected error occurred when trying to get edition details, so some functionality in Florence may not work as expected. Try refreshing the page",
                isDismissable: true
            }
            notifications.add(notification);
            console.error("Error getting mapping edition to state:\n", error);
        }
    }

    getAllVersions = (datasetID, editions) => {
        this.setState({isFetchingVersions: true});
        return datasets.getVersions(datasetID, editions).then(versions => {
            this.setState({isFetchingVersions: false, versions: [...this.state.versions, ...this.mapDatasetVersionsToState(versions.items)]});
            return this.mapDatasetVersionsToState(versions.items);
        }).catch(error => {
            console.error(error)
            this.setState({isFetchingVersions: false});
        })
    }

    mapDatasetVersionsToState = versions => {
        try {
            const versionsList =  versions.map(version => {
                const published = version.state === "published" ? "(published)" : null;
                return {
                    id: version.id,
                    title: `Version: ${version.version} ${published ? published : ""}`,
                    url: this.props.location.pathname + "/versions/" + version.version,
                    version: version.version,
                    details: [
                        `Release date: ${version.release_date ? date.format(version.release_date, "dd mmmm yyyy") : "Not yet set"}`
                    ]
                }    
            });
            versionsList.sort((this.sortByVersionNumber))
            return versionsList.reverse();
        } catch(error) {
            console.error(error);
        }
    }

    sortByVersionNumber = (a ,b) => {
        return a.version - b.version;
    }
    
    handleBackButton = () => {
        const previousUrl = url.resolve("../../");
        this.props.dispatch(push(previousUrl));
    }

    render() {
        return (
            <div className="grid grid--justify-center">
                <div className="grid__col-9">
                    <div className="margin-top--2">
                        &#9664; <button type="button" className="btn btn--link" onClick={this.handleBackButton}>Back</button>
{'fraction_non_alphanumeric': 0.10257696407298263, 'fraction_numerical': 0.0011285974042259703, 'mean_word_length': 1.5754884547069272, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 43, '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': '2496675', 'n_tokens_mistral': 3829, 'n_tokens_neox': 3742, 'n_words': 904}