client/src/components/Note.js
author Alexandre Segura <mex.zktk@gmail.com>
Fri, 23 Jun 2017 15:57:35 +0200
changeset 82 6f2999873343
parent 80 b3a02ea6d097
child 84 bf35a7737f94
permissions -rw-r--r--
Add close icon when editing note.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     1
import React, { Component } from 'react';
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     2
import { connect } from 'react-redux';
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     3
import { bindActionCreators } from 'redux';
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
import PropTypes from 'prop-types';
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     5
import { formatTimestamp } from '../utils';
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     6
import SlateEditor from './SlateEditor';
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     7
import * as notesActions from '../actions/notesActions';
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     8
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     9
class Note extends Component {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    10
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    11
  state = {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    12
    edit: false
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    13
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    14
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    15
  enterEditMode = () => {
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    16
    const { edit } = this.state;
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    17
    if (edit) return;
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    18
    this.setState({ edit: true })
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    19
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    20
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    21
  onClickDelete = (e) => {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    22
    e.preventDefault();
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    23
    e.stopPropagation();
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    24
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    25
    this.props.notesActions.deleteNote(this.props.note);
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    26
  }
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    28
  onClickButton = (e) => {
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    29
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    30
    const plain = this.refs.editor.asPlain();
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    31
    const raw = this.refs.editor.asRaw();
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    32
    const html = this.refs.editor.asHtml();
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    33
    const categories = this.refs.editor.asCategories();
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    34
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    35
    this.props.notesActions.updateNote(this.props.note, {
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    36
      plain,
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    37
      raw,
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    38
      html,
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    39
      categories
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    40
    });
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    41
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    42
    this.setState({ edit: false })
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    43
  }
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    44
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    45
  onClickClose = (e) => {
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    46
    e.preventDefault();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    47
    e.stopPropagation();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    48
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    49
    this.setState({ edit: false })
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    50
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    51
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    52
  renderNoteContent() {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    53
    if (this.state.edit) {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    54
      return (
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    55
        <div className="note-content">
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    56
          <SlateEditor ref="editor"
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    57
            onButtonClick={ this.onClickButton }
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    58
            note={ this.props.note } />
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    59
        </div>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    60
      )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    61
    }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    62
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    63
    return (
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    64
      <div className="note-content" dangerouslySetInnerHTML={{ __html: this.props.note.html }} />
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    65
    )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    66
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    67
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    68
  renderNoteRight() {
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    69
    if (this.state.edit) {
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    70
      return (
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    71
        <a onClick={this.onClickClose}>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    72
          <span className="material-icons">close</span>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    73
        </a>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    74
      );
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    75
    }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    76
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    77
    return (
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    78
      <a onClick={this.onClickDelete}>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    79
        <span className="material-icons">delete</span>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    80
      </a>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    81
    )
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    82
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    83
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    84
  render() {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    85
    return (
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    86
      <div id={"note-" + this.props.note._id} className="note" onClick={ this.enterEditMode }>
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    87
        <span className="start">{ formatTimestamp(this.props.note.startedAt) }</span>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    88
        <span className="finish">{ formatTimestamp(this.props.note.finishedAt) }</span>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    89
        { this.renderNoteContent() }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    90
        <div className="note-margin-comment">
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    91
          <small className="text-muted">{ this.props.note.marginComment }</small>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    92
        </div>
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    93
        { this.renderNoteRight() }
78
49c5ea36d0a4 Store margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
    94
      </div>
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    95
    );
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    96
  };
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    97
}
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
Note.propTypes = {
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
  note: PropTypes.object.isRequired
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
};
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   103
function mapStateToProps(state, props) {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   104
  return props;
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   105
}
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   106
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   107
function mapDispatchToProps(dispatch) {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   108
  return {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   109
    notesActions: bindActionCreators(notesActions, dispatch),
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   110
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   111
}
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   112
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   113
export default connect(mapStateToProps, mapDispatchToProps)(Note);