client/src/components/Note.js
author ymh <ymh.work@gmail.com>
Tue, 04 Dec 2018 18:17:56 +0100
changeset 191 3f71ad81a5a9
parent 173 0e6703cd0968
permissions -rw-r--r--
reorganize scss to better us the bootstrap imports
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
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     8
  constructor(props) {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     9
    super(props);
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    10
    this.editorInst = React.createRef();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    11
  }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    12
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    13
  get editor() {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    14
    if(this.editorInst && this.editorInst.current) {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    15
      return this.editorInst.current;
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    16
    }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    17
    return null;
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    18
  }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    19
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    20
  onClickDelete = (e) => {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    21
    e.preventDefault();
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    22
    e.stopPropagation();
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    23
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    24
    this.props.onDelete();
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    25
  }
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    27
  submitNote = () => {
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    28
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    29
    const plain = this.editor.asPlain();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    30
    const raw = this.editor.asRaw();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    31
    const html = this.editor.asHtml();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    32
    const categories = this.editor.asCategories();
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    33
    // const marginComment = this.marginComment.value;
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    34
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    35
    const data = {
80
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,
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    39
      categories,
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    40
      // marginComment
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    41
    }
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    42
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    43
    this.props.onSave(this.props.note, data);
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    44
  }
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    45
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    46
  onClickClose = (e) => {
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    47
    e.preventDefault();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    48
    e.stopPropagation();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    49
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    50
    this.props.onClose();
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    51
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    52
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    53
  renderNoteContent() {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    54
    if (this.props.isEditing) {
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    55
      return (
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    56
        <div className="note-content w-100 pl-2 pt-2">
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    57
          <SlateEditor editorRef={this.editorInst}
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    58
            submitNote={ this.submitNote }
138
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    59
            note={ this.props.note }
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    60
            annotationCategories={ this.props.annotationCategories } />
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    61
        </div>
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
    }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    64
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    65
    return (
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    66
      <div className="note-content w-100 pl-2 pt-2" dangerouslySetInnerHTML={{ __html: this.props.note.html }} />
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    67
    )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    68
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    69
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    70
  renderNoteRight() {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    71
    if (this.props.isEditing) {
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    72
      return (
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    73
        <span onClick={this.onClickClose}>
166
950a2350930f Add css to Note component
salimr <riwad.salim@yahoo.fr>
parents: 161
diff changeset
    74
          <span className="material-icons pl-2">close</span>
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    75
        </span>
82
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
    }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    78
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    79
    return (
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    80
      <span onClick={this.onClickDelete}>
166
950a2350930f Add css to Note component
salimr <riwad.salim@yahoo.fr>
parents: 161
diff changeset
    81
        <span className="material-icons delete-icon pl-2">delete</span>
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    82
      </span>
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    83
    )
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    84
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    85
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    86
  render() {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    87
    return (
166
950a2350930f Add css to Note component
salimr <riwad.salim@yahoo.fr>
parents: 161
diff changeset
    88
    <div id={"note-" + this.props.note._id} className="note text-sm mr-5 pt-1"
950a2350930f Add css to Note component
salimr <riwad.salim@yahoo.fr>
parents: 161
diff changeset
    89
    onClick={ this.props.onClick }>
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    90
      <div className="mr-5">
170
7da1d5137b0b Upgrade dependencies and correct theme colors
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    91
        <small className="start text-irinotes-time pt-2">{ formatTimestamp(this.props.note.startedAt) }</small>
7da1d5137b0b Upgrade dependencies and correct theme colors
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    92
        <small className="finish text-irinotes-time pb-2">{ formatTimestamp(this.props.note.finishedAt) }</small>
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    93
      </div>
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    94
        { this.renderNoteContent() }
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    95
        {/* { this.renderNoteMarginComment() } */}
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    96
        <div className="float-right">
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    97
        { this.renderNoteRight() }
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    98
        </div>
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    99
    </div>
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   100
    );
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   101
  };
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   102
}
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
Note.propTypes = {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
   105
  note: PropTypes.object.isRequired,
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
};
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
   108
export default Note;