All files / factory/editableColumnFields editableDateField.js

31.71% Statements 13/41
25% Branches 3/12
25% Functions 3/12
31.71% Lines 13/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                    1x   1x         1x 1x 1x 1x 1x 1x                                                                                                                               1x       2x 1x   1x                           1x                      
import React, {Component} from 'react';
import {sortBy, debounce} from "lodash";
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.min.css';
 
import moment from 'moment';
 
class EditableDateField extends Component {
 
    constructor(props) {
        super(props);
 
        this.state = {
            currentValue: this.props.value,
            isSaving: false
        };
 
        this.onValueSave = debounce(this.onValueSave.bind(this), 600);
        this.onRowValueSave = this.onRowValueSave.bind(this);
        this.onChangeValue = this.onChangeValue.bind(this);
        this.onClickEditRow = this.onClickEditRow.bind(this);
        this.closeEdit = this.closeEdit.bind(this);
        this.closeEditOnKeyDown = this.closeEditOnKeyDown.bind(this);
    }
 
    async onRowValueSave() {
        this.setState({
            isSaving: true
        });
 
        return 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');
            return Promise.resolve();
        }).catch(() => {
            this.setState({ isSaving: false, currentValue: this.props.value });
            this.props.handleShowMessage('Selected row could not be saved', 'error');
            return Promise.reject();
        });
    }
 
    async onValueSave() {
        this.setState({
            isSaving: true
        });
 
        return 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');
            return Promise.resolve();
        }).catch(() => {
            this.setState({ isSaving: false, currentValue: this.props.value });
            this.props.handleShowMessage('Selected row could not be saved', 'error');
            return Promise.reject();
        });
    }
 
    onChangeValue(date) {
        console.log(arguments);
        const dateValue = date.format('YYYY-MM-DD');
 
        this.props.onChangeEditRow(dateValue);
        this.setState({
            currentValue: dateValue
        });
    }
 
    closeEdit() {
        if (!this.props.onUpdateRow) {
            return this.onValueSave();
        }
    }
 
    closeEditOnKeyDown(event) {
        if (event.key == 'Escape' || event.key == 'Enter') {
            if (this.props.onUpdateRow) {
                this.onRowValueSave();
            } else {
                this.onValueSave();
            }
        }
    }
 
    onClickEditRow() {
        this.props.onClickEditRow(this.props.rowId, this.props.column.name);
    }
 
    render() {
        if (this.props.rowUnderEdit) {
            let datum = moment(this.state.currentValue, "YYYY-MM-DD");
 
            return (
                <td key={this.props.column.value}>
                    {this.state.isSaving && <span>Saving</span>}
                    <DatePicker
                        selected={datum}
                        onChange={this.onChangeValue}
                        dateFormat="YYYY.MM.DD"
                        onClickOutside={this.closeEdit}
                        autoFocus={this.props.autoFocus}
                        onKeyDown={this.closeEditOnKeyDown}
                    />
                </td>
            );
        }
        return (
            <td key={this.props.column.value}
                onClick={this.onClickEditRow}>
                {this.props.value}
            </td>
        );
    }
 
}
 
export default EditableDateField;