<commit_before>'use strict';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
import _ from 'underscore';
import moment from 'moment';
import { curator_page, history_views, userMatch, queryKeyValue, external_url_map } from './globals';
import { RestMixin } from './rest';
import { Form, FormMixin, Input } from '../libs/bootstrap/form';
import { parseAndLogError } from './mixins';
import { parsePubmed } from '../libs/parse-pubmed';
import { AddResourceId } from './add_external_resource';
import * as CuratorHistory from './curator_history';
import * as curator from './curator';
const CurationMixin = curator.CurationMixin;
const PmidDoiButtons = curator.PmidDoiButtons;
const RecordHeader = curator.RecordHeader;
const CurationPalette = curator.CurationPalette;
const VariantHeader = curator.VariantHeader;
const PmidSummary = curator.PmidSummary;

// Curator page content
var CurationCentral = createReactClass({
    mixins: [RestMixin, CurationMixin, CuratorHistory],

    propTypes: {
        href_url: PropTypes.object,
        session: PropTypes.object,
        affiliation: PropTypes.object,
        href: PropTypes.string
    },

    getInitialState: function() {
        return {
            currPmid: queryKeyValue('pmid', this.props.href),
            currGdm: null
        };
    },

    // Called when currently selected PMID changes
    currPmidChange: function(pmid) {
        if (pmid !== undefined) {
            var gdm = this.state.currGdm;

            if (gdm) {
                // Find the annotation in the GDM matching the given pmid
                var currAnnotation = curator.pmidToAnnotation(gdm,pmid);

                if (currAnnotation) {
                    this.setState({currPmid: currAnnotation.article.pmid});
                }
            }
            if (this.state.currGdm) {
                window.history.replaceState(window.state, '', '/curation-central/?gdm=' + this.state.currGdm.uuid + '&pmid=' + pmid);
            }
        }
    },

    // Retrieve the GDM object from the DB with the given uuid
    getGdm: function(uuid, pmid) {
        return this.getRestData('/gdm/' + uuid, null, true).then(gdm => {
            // The GDM object successfully retrieved; set the Curator Central component
            this.setState({currGdm: gdm, currOmimId: gdm.omimId});
            // If a PMID isn't pre-selected from the URL and PMIDs exist, select the first one in the PMID list by default
            if (pmid == undefined && gdm.annotations && gdm.annotations.length > 0) {
                var annotations = _(gdm.annotations).sortBy(function(annotation) {
                    // Sort list of articles by first author
                    return annotation.article.authors[0].toLowerCase();
                });
                pmid = annotations[0].article.pmid;
            }
            this.currPmidChange(pmid);

            // Focus the current PMID selection in left PMID column
            var userPmidList = document.getElementById('user-pmid-list');
            var selectedPmid = document.getElementById('selected-pmid');
            userPmidList.scrollTop = 0;
            if (selectedPmid && userPmidList.scrollHeight > userPmidList.clientHeight) {
                userPmidList.scrollTop += selectedPmid.offsetTop - 50;
            }

            return gdm;
        }).catch(function(e) {
            console.log('GETGDM ERROR=: %o', e);
        });
    },

    // After the Curator Central page component mounts, grab the uuid from the query string and
    // retrieve the corresponding GDM from the DB.
    componentDidMount: function() {
        var gdmUuid = queryKeyValue('gdm', this.props.href);
        var pmid = queryKeyValue('pmid', this.props.href);
        if (gdmUuid) {
            this.getGdm(gdmUuid, pmid);
        }
    },

    // Add an article whose object is given to the current GDM
    updateGdmArticles: function(article) {
        var currGdm = this.state.currGdm;

        // Put together a new annotation object with the article reference
        var newAnnotationObj = {
            article: article.pmid,
            active: true
        };

        // Post new annotation to the DB. fetch returns a JS promise.
        this.postRestData('/evidence/', newAnnotationObj).then(data => {
            // Save the new annotation; fetch the currently displayed GDM as an object without its embedded
            // objects; basically the object as it exists in the DB. We'll update that and write it back to the DB.
            return (data['@graph'][0]);
        }).then(newAnnotation => {
            return this.getRestData('/gdm/' + currGdm.uuid, null, true).then(freshGdm => {
                var gdmObj = curator.flatten(freshGdm);
                // Add our new annotation reference to the array of annotations in the GDM.
                if (!gdmObj.annotations) {
                    gdmObj.annotations = [];
                }
                gdmObj.annotations.push(newAnnotation['@id']);

                return this.putRestData('/gdm/' + currGdm.uuid, gdmObj).then(data => {
                    return data['@graph'][0];
                });
            });
        }).then(gdm => {
            // Record history of adding a PMID to a GDM
            var meta = {
                article: {
                    gdm: gdm['@id']
                }
            };
            this.recordHistory('add', article, meta);

            // Retrieve the updated GDM and set it as the new state GDM to force a rerendering.
            return this.getGdm(gdm.uuid, article.pmid);<commit_msg>Added handling to set affiliation to a newly created annotation<commit_after>'use strict';
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
import _ from 'underscore';
import moment from 'moment';
import { curator_page, history_views, userMatch, queryKeyValue, external_url_map } from './globals';
import { RestMixin } from './rest';
import { Form, FormMixin, Input } from '../libs/bootstrap/form';
import { parseAndLogError } from './mixins';
import { parsePubmed } from '../libs/parse-pubmed';
import { AddResourceId } from './add_external_resource';
import * as CuratorHistory from './curator_history';
import * as curator from './curator';
const CurationMixin = curator.CurationMixin;
const PmidDoiButtons = curator.PmidDoiButtons;
const RecordHeader = curator.RecordHeader;
const CurationPalette = curator.CurationPalette;
const VariantHeader = curator.VariantHeader;
const PmidSummary = curator.PmidSummary;

// Curator page content
var CurationCentral = createReactClass({
    mixins: [RestMixin, CurationMixin, CuratorHistory],

    propTypes: {
        href_url: PropTypes.object,
        session: PropTypes.object,
        affiliation: PropTypes.object,
        href: PropTypes.string
    },

    getInitialState: function() {
        return {
            currPmid: queryKeyValue('pmid', this.props.href),
            currGdm: null
        };
    },

    // Called when currently selected PMID changes
    currPmidChange: function(pmid) {
        if (pmid !== undefined) {
            var gdm = this.state.currGdm;

            if (gdm) {
                // Find the annotation in the GDM matching the given pmid
                var currAnnotation = curator.pmidToAnnotation(gdm,pmid);

                if (currAnnotation) {
                    this.setState({currPmid: currAnnotation.article.pmid});
                }
            }
            if (this.state.currGdm) {
                window.history.replaceState(window.state, '', '/curation-central/?gdm=' + this.state.currGdm.uuid + '&pmid=' + pmid);
            }
        }
    },

    // Retrieve the GDM object from the DB with the given uuid
    getGdm: function(uuid, pmid) {
        return this.getRestData('/gdm/' + uuid, null, true).then(gdm => {
            // The GDM object successfully retrieved; set the Curator Central component
            this.setState({currGdm: gdm, currOmimId: gdm.omimId});
            // If a PMID isn't pre-selected from the URL and PMIDs exist, select the first one in the PMID list by default
            if (pmid == undefined && gdm.annotations && gdm.annotations.length > 0) {
                var annotations = _(gdm.annotations).sortBy(function(annotation) {
                    // Sort list of articles by first author
                    return annotation.article.authors[0].toLowerCase();
                });
                pmid = annotations[0].article.pmid;
            }
            this.currPmidChange(pmid);

            // Focus the current PMID selection in left PMID column
            var userPmidList = document.getElementById('user-pmid-list');
            var selectedPmid = document.getElementById('selected-pmid');
            userPmidList.scrollTop = 0;
            if (selectedPmid && userPmidList.scrollHeight > userPmidList.clientHeight) {
                userPmidList.scrollTop += selectedPmid.offsetTop - 50;
            }

            return gdm;
        }).catch(function(e) {
            console.log('GETGDM ERROR=: %o', e);
        });
    },

    // After the Curator Central page component mounts, grab the uuid from the query string and
    // retrieve the corresponding GDM from the DB.
    componentDidMount: function() {
        var gdmUuid = queryKeyValue('gdm', this.props.href);
        var pmid = queryKeyValue('pmid', this.props.href);
        if (gdmUuid) {
            this.getGdm(gdmUuid, pmid);
        }
    },

    // Add an article whose object is given to the current GDM
    updateGdmArticles: function(article) {
        var currGdm = this.state.currGdm;

        // Put together a new annotation object with the article reference
        var newAnnotationObj = {
            article: article.pmid,
            active: true
        };

        // Add affiliation if the user is associated with an affiliation
        // and if the data object has no affiliation
        if (this.props.affiliation && Object.keys(this.props.affiliation).length) {
            if (!newAnnotationObj.affiliation) {
                newAnnotationObj.affiliation = this.props.affiliation.affiliation_id;
            }
        }

        // Post new annotation to the DB. fetch returns a JS promise.
        this.postRestData('/evidence/', newAnnotationObj).then(data => {
            // Save the new annotation; fetch the currently displayed GDM as an object without its embedded
            // objects; basically the object as it exists in the DB. We'll update that and write it back to the DB.
            return (data['@graph'][0]);
        }).then(newAnnotation => {
            return this.getRestData('/gdm/' + currGdm.uuid, null, true).then(freshGdm => {
                var gdmObj = curator.flatten(freshGdm);
                // Add our new annotation reference to the array of annotations in the GDM.
                if (!gdmObj.annotations) {
                    gdmObj.annotations = [];
                }
                gdmObj.annotations.push(newAnnotation['@id']);

                return this.putRestData('/gdm/' + currGdm.uuid, gdmObj).then(data => {
                    return data['@graph'][0];
                });
            });
        }).then(gdm => {
            // Record history of adding a PMID to a GDM
            var meta = {
                article: {
                    gdm: gdm['@id']
                }
            };
            this.recordHistory('add', article, meta);

            // Retrieve the updated GDM and set it as the new state GDM to force a rerendering.
            return this.getGdm(gdm.uuid, article.pmid);
{'fraction_non_alphanumeric': 0.10409556313993173, 'fraction_numerical': 0.0013651877133105802, 'mean_word_length': 2.10654651470978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '1785239', 'n_tokens_mistral': 3253, 'n_tokens_neox': 3159, 'n_words': 913}