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

import { errCodes } from '../../../utilities/errorCodes'
import datasets from '../../../utilities/api-clients/datasets';
import notifications from '../../../utilities/notifications';
import Modal from '../../../components/Modal';
import Select from '../../../components/Select';
import Checkbox from '../../../components/Checkbox';
import Input from '../../../components/Input';
import Card from '../../../components/Card';
import CardList from '../../../components/CardList';
import RelatedContentForm from './related-content/RelatedContentForm'
import {updateAllDatasets, updateActiveDataset} from '../../../config/actions';
import url from '../../../utilities/url';
import log, {eventTypes} from '../../../utilities/log'

const propTypes = {
    params: PropTypes.shape({
        datasetID: PropTypes.string
    }),
    dispatch: PropTypes.func.isRequired,
    rootPath: PropTypes.string.isRequired,
    routes: PropTypes.arrayOf(PropTypes.object).isRequired,
    datasets: PropTypes.arrayOf(PropTypes.shape({
        next: PropTypes.shape({
            title: PropTypes.string.isRequired
        }),
        current: PropTypes.shape({
            title: PropTypes.string.isRequired
        })
    })),
    dataset: PropTypes.shape({
      title: PropTypes.string.isRequired,
      description: PropTypes.string,
      keywords: PropTypes.array,
      national_statistic: PropTypes.bool,
      contacts: PropTypes.arrayOf(PropTypes.shape({
          name: PropTypes.string,
          email: PropTypes.string,
          telephone: PropTypes.string,
      })),
      qmi: PropTypes.shape({
          href: PropTypes.string,
          title: PropTypes.string,
      }),
      related_datasets: PropTypes.arrayOf(PropTypes.shape({
          href: PropTypes.string,
          title: PropTypes.string,
      })),
      publications: PropTypes.arrayOf(PropTypes.shape({
          href: PropTypes.string,
          title: PropTypes.string,
      })),
      release_frequency: PropTypes.string
    })
}

export class DatasetMetadata extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isFetchingDataset: false,
            isSubmittingData: false,
            hasChanges: false,
            error: null,
            showModal: false,
            modalType: "",
            title: "",
            description: "",
            relatedBulletins: [],
            relatedQMI: "",
            relatedLinks: [],
            keywords: "",
            titleInput: "",
            urlInput: "",
            editKey: "",
            contactName: "",
            contactEmail: "",
            contactPhone: "",
            releaseFrequency: "",
            isNationalStat: false,
            titleError: "",
            urlError: ""
        };

        this.originalState = null;

        this.handleSelectChange = this.handleSelectChange.bind(this);
        this.handleInputChange = this.handleInputChange.bind(this);
        this.handlePageSubmit = this.handlePageSubmit.bind(this);
        this.handleToggleChange = this.handleToggleChange.bind(this);
        this.handleBackButton = this.handleBackButton.bind(this);
        this.handleModalSubmit = this.handleModalSubmit.bind(this);
        this.handleCancel = this.handleCancel.bind(this);
        this.handleFormSubmit = this.handleFormSubmit.bind(this);
        this.handleAddRelatedClick = this.handleAddRelatedClick.bind(this);
        this.handleDeleteRelatedClick = this.handleDeleteRelatedClick.bind(this);
        this.handleEditRelatedClick = this.handleEditRelatedClick.bind(this);
        this.editRelatedLink = this.editRelatedLink.bind(this);
    }

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

        const APIRequests = [
            datasets.get(this.props.params.datasetID)
        ]

        if (this.props.datasets.length === 0) {
            APIRequests.push(datasets.getAll());
        }

        Promise.all(APIRequests).then(responses => {
            this.props.dispatch(updateActiveDataset(responses[0].next || responses[0].current));
            if (this.props.datasets.length === 0) {
                this.props.dispatch(updateAllDatasets(responses[1].items));
            }

            if (this.props.dataset && this.props.dataset.title) {
                this.setState({
                    title: this.props.dataset.title
                });
            }
            
            if (this.props.dataset && this.props.dataset.description) {
                this.setState({
                    description: this.props.dataset.description,
                });
            }

            if (this.props.dataset && this.props.dataset.release_frequency) {
                this.setState({
                    releaseFrequency: this.props.dataset.release_frequency
                });
            }
            
            if (this.props.dataset && this.props.dataset.national_statistic) {
                this.setState({
                    isNationalStat: this.props.dataset.national_statistic
                });
            }<commit_msg>Stop unneccessary fetch of all datasets on load of dataset metadata screen<commit_after>import React, { Component } from 'react';
import { connect } from 'react-redux';
import { push } from 'react-router-redux';
import PropTypes from 'prop-types';
import uuid from 'uuid/v4';

import { errCodes } from '../../../utilities/errorCodes'
import datasets from '../../../utilities/api-clients/datasets';
import notifications from '../../../utilities/notifications';
import Modal from '../../../components/Modal';
import Select from '../../../components/Select';
import Checkbox from '../../../components/Checkbox';
import Input from '../../../components/Input';
import Card from '../../../components/Card';
import CardList from '../../../components/CardList';
import RelatedContentForm from './related-content/RelatedContentForm'
import {updateAllDatasets, updateActiveDataset} from '../../../config/actions';
import url from '../../../utilities/url';
import log, {eventTypes} from '../../../utilities/log'

const propTypes = {
    params: PropTypes.shape({
        datasetID: PropTypes.string.isRequired
    }).isRequired,
    dispatch: PropTypes.func.isRequired,
    rootPath: PropTypes.string.isRequired,
    routes: PropTypes.arrayOf(PropTypes.object).isRequired,
    datasets: PropTypes.arrayOf(PropTypes.shape({
        next: PropTypes.shape({
            title: PropTypes.string
        }),
        current: PropTypes.shape({
            title: PropTypes.string
        })
    })),
    dataset: PropTypes.shape({
        title: PropTypes.string,
        description: PropTypes.string,
        keywords: PropTypes.array,
        national_statistic: PropTypes.bool,
        contacts: PropTypes.arrayOf(PropTypes.shape({
            name: PropTypes.string,
            email: PropTypes.string,
            telephone: PropTypes.string,
        })),
        qmi: PropTypes.shape({
            href: PropTypes.string,
            title: PropTypes.string,
        }),
        related_datasets: PropTypes.arrayOf(PropTypes.shape({
            href: PropTypes.string,
            title: PropTypes.string,
        })),
        publications: PropTypes.arrayOf(PropTypes.shape({
            href: PropTypes.string,
            title: PropTypes.string,
        })),
        release_frequency: PropTypes.string
    })
}

export class DatasetMetadata extends Component {
    constructor(props) {
        super(props);
        this.state = {
            isFetchingDataset: false,
            isSubmittingData: false,
            hasChanges: false,
            error: null,
            showModal: false,
            modalType: "",
            title: "",
            description: "",
            relatedBulletins: [],
            relatedQMI: "",
            relatedLinks: [],
            keywords: "",
            titleInput: "",
            urlInput: "",
            editKey: "",
            contactName: "",
            contactEmail: "",
            contactPhone: "",
            releaseFrequency: "",
            isNationalStat: false,
            titleError: "",
            urlError: ""
        };

        this.originalState = null;

        this.handleSelectChange = this.handleSelectChange.bind(this);
        this.handleInputChange = this.handleInputChange.bind(this);
        this.handlePageSubmit = this.handlePageSubmit.bind(this);
        this.handleToggleChange = this.handleToggleChange.bind(this);
        this.handleBackButton = this.handleBackButton.bind(this);
        this.handleModalSubmit = this.handleModalSubmit.bind(this);
        this.handleCancel = this.handleCancel.bind(this);
        this.handleFormSubmit = this.handleFormSubmit.bind(this);
        this.handleAddRelatedClick = this.handleAddRelatedClick.bind(this);
        this.handleDeleteRelatedClick = this.handleDeleteRelatedClick.bind(this);
        this.handleEditRelatedClick = this.handleEditRelatedClick.bind(this);
        this.editRelatedLink = this.editRelatedLink.bind(this);
    }

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

        datasets.get(this.props.params.datasetID).then(response => {
            this.props.dispatch(updateActiveDataset(response.next || response.current));

            if (this.props.dataset && this.props.dataset.title) {
                this.setState({
                    title: this.props.dataset.title
                });
            }
            
            if (this.props.dataset && this.props.dataset.description) {
                this.setState({
                    description: this.props.dataset.description,
                });
            }

            if (this.props.dataset && this.props.dataset.release_frequency) {
                this.setState({
                    releaseFrequency: this.props.dataset.release_frequency
                });
            }
            
            if (this.props.dataset && this.props.dataset.national_statistic) {
                this.setState({
                    isNationalStat: this.props.dataset.national_statistic
                });
            }
{'fraction_non_alphanumeric': 0.12684907667021172, 'fraction_numerical': 0.0006767862322343614, 'mean_word_length': 2.064888888888889, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '7805', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2664, 'n_words': 477}