client/src/components/Note.js
author salimr <riwad.salim@yahoo.fr>
Tue, 14 Aug 2018 20:34:50 +0200
changeset 143 cfcbf4bc66f1
parent 138 a1fb2ced3049
child 161 a642639dbc07
permissions -rw-r--r--
Remove react-bootstrap from components except Modal, Collapse and Dropdown
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';
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import PropTypes from 'prop-types';
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     3
import { formatTimestamp } from '../utils';
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     4
import SlateEditor from './SlateEditor';
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     5
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     6
class Note extends Component {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     7
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     8
  onClickDelete = (e) => {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     9
    e.preventDefault();
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    10
    e.stopPropagation();
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    11
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    12
    this.props.onDelete();
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    13
  }
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    15
  onClickButton = (e) => {
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    16
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    17
    const plain = this.refs.editor.asPlain();
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    18
    const raw = this.refs.editor.asRaw();
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    19
    const html = this.refs.editor.asHtml();
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    20
    const categories = this.refs.editor.asCategories();
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    21
    const marginComment = this.marginComment.value;
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    22
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    23
    const data = {
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    24
      plain,
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    25
      raw,
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    26
      html,
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    27
      categories,
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    28
      marginComment
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    29
    }
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    30
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    31
    this.props.onSave(this.props.note, data);
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    32
  }
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    33
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    34
  onClickClose = (e) => {
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    35
    e.preventDefault();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    36
    e.stopPropagation();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    37
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    38
    this.props.onClose();
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    39
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    40
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    41
  renderNoteContent() {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    42
    if (this.props.isEditing) {
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    43
      return (
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    44
        <div className="note-content">
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    45
          <SlateEditor ref="editor"
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    46
            onButtonClick={ this.onClickButton }
138
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    47
            note={ this.props.note }
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    48
            annotationCategories={ this.props.annotationCategories } />
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    49
        </div>
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    50
      )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    51
    }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    52
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    53
    return (
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    54
      <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
    55
    )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    56
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    57
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    58
  renderNoteMarginComment() {
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    59
    if (this.props.isEditing) {
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    60
      return (
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    61
        <div className="note-margin-comment">
143
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
    62
          <input type="text" className="form-control"
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    63
            name="margin"
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    64
            componentClass="textarea"
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    65
            placeholder="Enter a margin comment for your note"
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    66
            defaultValue={ this.props.note.marginComment }
143
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
    67
            // inputRef={ ref => { this.marginComment = ref; } } />
cfcbf4bc66f1 Remove react-bootstrap from components except Modal, Collapse and Dropdown
salimr <riwad.salim@yahoo.fr>
parents: 138
diff changeset
    68
            ref={(marginComment) => { this.marginComment = marginComment; }} />
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    69
        </div>
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    70
      )
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    71
    }
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    72
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    73
    return (
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    74
      <div className="note-margin-comment">
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    75
        <small className="text-muted">{ this.props.note.marginComment }</small>
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    76
      </div>
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    77
    )
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    78
  }
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    79
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    80
  renderNoteRight() {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    81
    if (this.props.isEditing) {
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    82
      return (
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    83
        <a onClick={this.onClickClose}>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    84
          <span className="material-icons">close</span>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    85
        </a>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    86
      );
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    87
    }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    88
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    89
    return (
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    90
      <a onClick={this.onClickDelete}>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    91
        <span className="material-icons">delete</span>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    92
      </a>
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    93
    )
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    94
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    95
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    96
  render() {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    97
    return (
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    98
      <div id={"note-" + this.props.note._id} className="note" onClick={ this.props.onClick }>
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    99
        <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
   100
        <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
   101
        { this.renderNoteContent() }
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
   102
        { this.renderNoteMarginComment() }
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
   103
        { this.renderNoteRight() }
78
49c5ea36d0a4 Store margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 74
diff changeset
   104
      </div>
79
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
}
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
Note.propTypes = {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
   110
  note: PropTypes.object.isRequired,
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
};
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
   113
export default Note;