client/src/components/Note.js
author ymh <ymh.work@gmail.com>
Fri, 30 Nov 2018 10:53:15 +0100
changeset 183 f8f3af9e5c83
parent 173 0e6703cd0968
child 191 3f71ad81a5a9
permissions -rw-r--r--
Change the settings to avoid using Session authentication for rest framework as it raise exceptions in case client and backend are on the same domain On the filter, adapt to take into account new version of django_filters
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';
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
     5
import './Note.css';
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     6
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     7
class Note extends Component {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
     8
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
     9
  constructor(props) {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    10
    super(props);
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    11
    this.editorInst = React.createRef();
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
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    14
  get editor() {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    15
    if(this.editorInst && this.editorInst.current) {
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    16
      return this.editorInst.current;
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    17
    }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    18
    return null;
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    19
  }
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    20
79
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
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    25
    this.props.onDelete();
79
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
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    28
  submitNote = () => {
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    29
171
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    30
    const plain = this.editor.asPlain();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    31
    const raw = this.editor.asRaw();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    32
    const html = this.editor.asHtml();
03334a31130a Add translation with react-i18next
ymh <ymh.work@gmail.com>
parents: 170
diff changeset
    33
    const categories = this.editor.asCategories();
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    34
    // const marginComment = this.marginComment.value;
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    35
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    36
    const data = {
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    37
      plain,
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    38
      raw,
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    39
      html,
109
ef62de545a8d Allow editing margin comment.
Alexandre Segura <mex.zktk@gmail.com>
parents: 84
diff changeset
    40
      categories,
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    41
      // marginComment
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    42
    }
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    43
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    44
    this.props.onSave(this.props.note, data);
80
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    45
  }
b3a02ea6d097 Implement note edition.
Alexandre Segura <mex.zktk@gmail.com>
parents: 79
diff changeset
    46
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    47
  onClickClose = (e) => {
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    48
    e.preventDefault();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    49
    e.stopPropagation();
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    50
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    51
    this.props.onClose();
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    52
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    53
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    54
  renderNoteContent() {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    55
    if (this.props.isEditing) {
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    56
      return (
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    57
        <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
    58
          <SlateEditor editorRef={this.editorInst}
173
0e6703cd0968 Correct the Note editor.
ymh <ymh.work@gmail.com>
parents: 172
diff changeset
    59
            submitNote={ this.submitNote }
138
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    60
            note={ this.props.note }
a1fb2ced3049 propagate annotations categories from session protocol definition
ymh <ymh.work@gmail.com>
parents: 109
diff changeset
    61
            annotationCategories={ this.props.annotationCategories } />
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    62
        </div>
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
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    66
    return (
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    67
      <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
    68
    )
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    69
  }
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    70
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    71
  renderNoteRight() {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
    72
    if (this.props.isEditing) {
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    73
      return (
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    74
        <span onClick={this.onClickClose}>
166
950a2350930f Add css to Note component
salimr <riwad.salim@yahoo.fr>
parents: 161
diff changeset
    75
          <span className="material-icons pl-2">close</span>
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    76
        </span>
82
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
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    80
    return (
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    81
      <span onClick={this.onClickDelete}>
166
950a2350930f Add css to Note component
salimr <riwad.salim@yahoo.fr>
parents: 161
diff changeset
    82
        <span className="material-icons delete-icon pl-2">delete</span>
172
4b780ebbedc6 - Upgrade libraries
ymh <ymh.work@gmail.com>
parents: 171
diff changeset
    83
      </span>
82
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
  }
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    86
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    87
  render() {
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    88
    return (
166
950a2350930f Add css to Note component
salimr <riwad.salim@yahoo.fr>
parents: 161
diff changeset
    89
    <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
    90
    onClick={ this.props.onClick }>
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    91
      <div className="mr-5">
170
7da1d5137b0b Upgrade dependencies and correct theme colors
ymh <ymh.work@gmail.com>
parents: 166
diff changeset
    92
        <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
    93
        <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
    94
      </div>
79
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
    95
        { this.renderNoteContent() }
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    96
        {/* { this.renderNoteMarginComment() } */}
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    97
        <div className="float-right">
82
6f2999873343 Add close icon when editing note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 80
diff changeset
    98
        { this.renderNoteRight() }
161
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
    99
        </div>
a642639dbc07 Split scss files and adapt session page design
salimr <riwad.salim@yahoo.fr>
parents: 143
diff changeset
   100
    </div>
79
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
  };
772b73e31069 Introduce note editing, allow deleting note.
Alexandre Segura <mex.zktk@gmail.com>
parents: 78
diff changeset
   103
}
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
Note.propTypes = {
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
   106
  note: PropTypes.object.isRequired,
1
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
};
431977d7c9a6 add first react application skeleton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
84
bf35a7737f94 Move logic to NotesList component.
Alexandre Segura <mex.zktk@gmail.com>
parents: 82
diff changeset
   109
export default Note;