<commit_before>import React, { Component, PropTypes } from "react";

import AccordianList from "metabase/components/AccordianList.jsx";
import Icon from "metabase/components/Icon.jsx";
import Tooltip from "metabase/components/Tooltip.jsx";
import PopoverWithTrigger from "metabase/components/PopoverWithTrigger.jsx";
import TimeGroupingPopover from "./TimeGroupingPopover.jsx";
import QueryDefinitionTooltip from "./QueryDefinitionTooltip.jsx";

import { isDate, getIconForField } from 'metabase/lib/schema_metadata';
import { parseFieldBucketing, parseFieldTarget } from "metabase/lib/query_time";
import { stripId, singularize } from "metabase/lib/formatting";
import Query from "metabase/lib/query";

import _ from "underscore";


export default class FieldList extends Component {
    constructor(props, context) {
        super(props, context);

        _.bindAll(this, "onChange", "itemIsSelected", "renderItemExtra", "renderItemIcon", "renderSectionIcon", "getItemClasses");
    }

    static propTypes = {
        field: PropTypes.oneOfType([PropTypes.number, PropTypes.array]),
        fieldOptions: PropTypes.object.isRequired,
        customFieldOptions: PropTypes.object,
        segmentOptions: PropTypes.array,
        tableName: PropTypes.string,
        onFieldChange: PropTypes.func.isRequired,
        onFilterChange: PropTypes.func,
        enableTimeGrouping: PropTypes.bool,
        tableMetadata: PropTypes.object.isRequired
    };

    componentWillMount() {
        this.componentWillReceiveProps(this.props);
    }

    componentWillReceiveProps(newProps) {
        let { tableMetadata, field, fieldOptions, customFieldOptions, segmentOptions } = newProps;
        let tableName = tableMetadata.display_name;

        let specialOptions = [];
        if (segmentOptions) {
            specialOptions = segmentOptions.map(segment => ({
                name: segment.name,
                value: ["SEGMENT", segment.id],
                segment: segment
            }));
        }

        if (customFieldOptions) {
            specialOptions = specialOptions.concat(Object.keys(customFieldOptions).map(name => ({
                name: name,
                value: ["expression", name],
                customField: customFieldOptions[name]
            })));
        }

        let mainSection = {
            name: singularize(tableName),
            items: specialOptions.concat(fieldOptions.fields.map(field => ({
                name: Query.getFieldPathName(field.id, tableMetadata),
                value: field.id,
                field: field
            })))
        };

        let fkSections = fieldOptions.fks.map(fk => ({
            name: stripId(fk.field.display_name),
            items: fk.fields.map(field => ({
                name: Query.getFieldPathName(field.id, tableMetadata),
                value: ["fk->", fk.field.id, field.id],
                field: field
            }))
        }));

        let sections = [mainSection].concat(fkSections);
        let fieldTarget = parseFieldTarget(field);

        this.setState({ sections, fieldTarget });
    }

    itemIsSelected(item) {
        return _.isEqual(this.state.fieldTarget, item.value);
    }
<commit_msg>Fix fk field names in field list<commit_after>import React, { Component, PropTypes } from "react";

import AccordianList from "metabase/components/AccordianList.jsx";
import Icon from "metabase/components/Icon.jsx";
import Tooltip from "metabase/components/Tooltip.jsx";
import PopoverWithTrigger from "metabase/components/PopoverWithTrigger.jsx";
import TimeGroupingPopover from "./TimeGroupingPopover.jsx";
import QueryDefinitionTooltip from "./QueryDefinitionTooltip.jsx";

import { isDate, getIconForField } from 'metabase/lib/schema_metadata';
import { parseFieldBucketing, parseFieldTarget } from "metabase/lib/query_time";
import { stripId, singularize } from "metabase/lib/formatting";
import Query from "metabase/lib/query";

import _ from "underscore";


export default class FieldList extends Component {
    constructor(props, context) {
        super(props, context);

        _.bindAll(this, "onChange", "itemIsSelected", "renderItemExtra", "renderItemIcon", "renderSectionIcon", "getItemClasses");
    }

    static propTypes = {
        field: PropTypes.oneOfType([PropTypes.number, PropTypes.array]),
        fieldOptions: PropTypes.object.isRequired,
        customFieldOptions: PropTypes.object,
        segmentOptions: PropTypes.array,
        tableName: PropTypes.string,
        onFieldChange: PropTypes.func.isRequired,
        onFilterChange: PropTypes.func,
        enableTimeGrouping: PropTypes.bool,
        tableMetadata: PropTypes.object.isRequired
    };

    componentWillMount() {
        this.componentWillReceiveProps(this.props);
    }

    componentWillReceiveProps(newProps) {
        let { tableMetadata, field, fieldOptions, customFieldOptions, segmentOptions } = newProps;
        let tableName = tableMetadata.display_name;

        let specialOptions = [];
        if (segmentOptions) {
            specialOptions = segmentOptions.map(segment => ({
                name: segment.name,
                value: ["SEGMENT", segment.id],
                segment: segment
            }));
        }

        if (customFieldOptions) {
            specialOptions = specialOptions.concat(Object.keys(customFieldOptions).map(name => ({
                name: name,
                value: ["expression", name],
                customField: customFieldOptions[name]
            })));
        }

        let mainSection = {
            name: singularize(tableName),
            items: specialOptions.concat(fieldOptions.fields.map(field => ({
                name: Query.getFieldPathName(field.id, tableMetadata),
                value: field.id,
                field: field
            })))
        };

        let fkSections = fieldOptions.fks.map(fk => ({
            name: stripId(fk.field.display_name),
            items: fk.fields.map(field => {
                const value = ["fk->", fk.field.id, field.id];
                const target = Query.getFieldTarget(value, tableMetadata);
                return {
                    name: Query.getFieldPathName(target.field.id, target.table),
                    value: value,
                    field: field
                };
            })
        }));

        let sections = [mainSection].concat(fkSections);
        let fieldTarget = parseFieldTarget(field);

        this.setState({ sections, fieldTarget });
    }

    itemIsSelected(item) {
        return _.isEqual(this.state.fieldTarget, item.value);
    }

{'fraction_non_alphanumeric': 0.10781793437169213, 'fraction_numerical': 0.0, 'mean_word_length': 2.552094522019334, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '4247639', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1742, 'n_words': 352}