All files / factory/editableColumnFields editableSelectField.js

72.09% Statements 31/43
37.5% Branches 3/8
71.43% Functions 10/14
70.73% Lines 29/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126              1x   1x       1x 1x 1x 1x 1x 1x       1x 1x                                                     2x       2x   1x       1x 1x     1x 1x 1x                             2x 2x         1x       14x 12x 12x 12x                           2x 2x 2x                   3x          
import React, {Component} from 'react';
import Select from 'react-select';
import {sortBy, get} from "lodash";
 
class EditableSelectField extends Component {
 
    constructor(props) {
        super(props);
 
        this.state = {
            currentValue: props.value,
        };
 
        this.onValueSave = this.onValueSave.bind(this);
        this.onRowValueSave = this.onRowValueSave.bind(this);
        this.onChangeValue = this.onChangeValue.bind(this);
        this.onBlur = this.onBlur.bind(this);
        this.onClickEditRow = this.onClickEditRow.bind(this);
        this.closeEdit = this.closeEdit.bind(this);
    }
 
    onChangeValue(value) {
        this.props.onChangeEditRow(get(value, 'value', value));
        this.setState({
            currentValue: value
        });
    }
 
    async onRowValueSave() {
        this.setState({
            isSaving: true
        });
 
        const response = await this.props.onUpdateRow()
        .then(() => {
            this.setState({
                isSaving: false,
                value: this.state.currentValue,
            });
            this.props.onFinishEditRow(this.props.rowId, this.props.column.name);
            this.props.handleShowMessage('Selected row edited successfully', 'ok');
        })
        .catch(() => {
            this.setState({ isSaving: false, currentValue: this.props.value });
            this.props.onFinishEditRow(this.props.rowId, this.props.column.name);
            this.props.handleShowMessage('Selected row could not be saved', 'error');
        });
    }
 
    async onValueSave() {
        this.setState({
            isSaving: true
        });
 
        const response = await this.props.onUpdateField(this.props.rowId, this.props.column.value, this.state.currentValue)
        .then(() => {
            this.setState({
                isSaving: false,
                value: this.state.currentValue,
            });
            this.props.onFinishEditRow(this.props.rowId, this.props.column.name);
            this.props.handleShowMessage('Selected row edited successfully', 'ok');
        })
        .catch(() => {
            this.setState({ isSaving: false, currentValue: this.props.value });
            this.props.onFinishEditRow(this.props.rowId, this.props.column.name);
            this.props.handleShowMessage('Selected row could not be saved', 'error');
        });
    }
 
    closeEdit(event) {
        if (event.key == 'Enter') {
            if (this.props.onUpdateRow) {
                this.onRowValueSave();
            } else {
                this.onValueSave();
            }
        }
    }
 
    onBlur() {
        Eif (!this.props.onUpdateRow) {
            return this.onValueSave();
        }
    }
 
    onClickEditRow() {
        this.props.onClickEditRow(this.props.rowId, this.props.column.name);
    }
 
    render() {
        if (this.props.rowUnderEdit) {
            const optionId = get(this.state.currentValue, 'value', null);
            const value = this.props.selectOptions.find(option => option.value == optionId);
            return (
                <td key={this.props.column.name}>
                    <Select value={value}
                            name={this.props.column.name}
                            style={{height: "100%"}}
                            options={sortBy(this.props.selectOptions, 'label')}
                            onChange={this.onChangeValue}
                            onBlur={this.onBlur}
                            onInputKeyDown={this.closeEdit}
                            autoFocus={this.props.autoFocus}
                            backspaceRemoves={false}/>
                </td>
            );
        }
        const value = this.props.selectOptions.find(option => option.value == this.props.value);
        const label = get(value, 'label', value);
        return (
            <td key={this.props.column.name}
                onClick={this.onClickEditRow}>
                { label }
            </td>
        );
    }
 
}
 
EditableSelectField.defaultProps = {
    options: [],
};
 
export default EditableSelectField;