Manual Reference Source Test

src/utilities/mixins/Commentable.js

import DiscussService from '../../services/DiscussService';
import Client from '../../Client';
/**
 * A mixin for threads and comments.  Currently the following types are commentable:
 * data_collection__captured_values, data_collection__linked_steps, dynamic_content__content,
 * profile, question
 */
class Commentable {
  /**
   * Initializer for the mixin class
   */
  initializer() { }

  /**
   * This is a quick way of creating a thread and a comment for this particular object
   *
   * @param  {String} text        - The text used for the comment
   * @param  {User}   author      - The user making the coment
   * @param  {String} cacheMode   - The cache method used
   * @return {Promise<Thread>}    - The thread created with an attached comment
   *
   * @example
   * // obj is an instantiated Object inheriting the mixin Commentable
   * obj.addComment('hello world');
   *
   * // using other options
   * import { User } from 'clinical6';
   *
   * const someOtherUser = new User();
   * obj.addComment('hello world', someOtherUser, 'networkFirst');
   */
  addComment(text, author = undefined, cacheMode = undefined) {
    return (new DiscussService()).insert(this, cacheMode)
      .then(thread => this.reply(text, thread, author, cacheMode).then(() => {
        thread.syncRelationships();
        return thread;
      }));
  }

  /**
   * Returns comments based on the current object.
   *
   * @param {String} cacheMode    - The cache mode for this call
   * @return {Promise<Thread[]>}  - Promise a thread
   */
  getComments(cacheMode = undefined) {
    return (new DiscussService()).get(this, cacheMode);
  }

  reply(text, thread, author = undefined, cacheMode = undefined) {
    const user = author || Client.instance.user;
    return (new DiscussService()).insertComment(text, thread, user, cacheMode);
  }
}

export default Commentable;