client/src/components/Note.js
author Alexandre Segura <mex.zktk@gmail.com>
Fri, 23 Jun 2017 11:16:34 +0200
changeset 80 b3a02ea6d097
parent 79 772b73e31069
child 82 6f2999873343
permissions -rw-r--r--
Implement note edition.
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
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    45
  renderNoteContent() {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    46
    if (this.state.edit) {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    47
      return (
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    48
        <div className="note-content">
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    49
          <SlateEditor ref="editor"
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    50
            onButtonClick={ this.onClickButton }
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    51
            note={ this.props.note } />
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    52
        </div>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    53
      )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    54
    }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    55
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    56
    return (
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    57
      <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
    58
    )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    59
  }
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
  render() {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    62
    return (
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    63
      <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
    64
        <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
    65
        <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
    66
        { this.renderNoteContent() }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    67
        <div className="note-margin-comment">
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    68
          <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
    69
        </div>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    70
        <a onClick={this.onClickDelete}>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    71
          <span className="material-icons">delete</span>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    72
        </a>
78
49c5ea36d0a4 Store margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
    73
      </div>
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    74
    );
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    75
  };
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    76
}
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
Note.propTypes = {
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
  note: PropTypes.object.isRequired
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
};
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    82
function mapStateToProps(state, props) {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    83
  return props;
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    84
}
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    85
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    86
function mapDispatchToProps(dispatch) {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    87
  return {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    88
    notesActions: bindActionCreators(notesActions, dispatch),
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    89
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    90
}
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    91
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    92
export default connect(mapStateToProps, mapDispatchToProps)(Note);