{"size": 1644, "ext": "js", "max_stars_repo_path": "src/stores/NotesStore.js", "max_stars_repo_name": "m4rEq/mykeep-client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/stores/NotesStore.js", "max_issues_repo_name": "m4rEq/mykeep-client", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/stores/NotesStore.js", "max_forks_repo_name": "m4rEq/mykeep-client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.8101265823, "max_line_length": 72, "alphanum_fraction": 0.6697080292}
import { EventEmitter } from 'events'

import AppDispatcher from '../dispatcher/AppDispatcher'
import AppConstants from '../constants/AppConstants'

const CHANGE_EVENT = 'change'

let _notes = []
let _loadingError = null
let _isLoading = true

function formatNote(note) {
  return {
    id: note._id,
    title: note.title,
    text: note.text,
    color: note.color || '#fff',
    createdAt: note.createdAt
  }
}

const TasksStore = Object.assign({}, EventEmitter.prototype, {
  isLoading() {
    return _isLoading
  },

  getNotes() {
    return _notes
  },

  // Произвести изменение с данными, что бы компноненты могли обновиться
  emitChange() {
    this.emit(CHANGE_EVENT)
  },

  // Подписка на событие
  addChangeListener(callback) {
    this.on(CHANGE_EVENT, callback)
  },

  // Отписка от события
  removeChangeListener(callback) {
    this.removeChangeListener(CHANGE_EVENT, callback)
  }
})

AppDispatcher.register(function(action) {
  switch(action.type) {
    case AppConstants.LOAD_NOTES_REQUEST: {
      console.log('Load Notes Request')
      _isLoading = true
      TasksStore.emitChange()
      break
    }

    case AppConstants.LOAD_NOTES_SUCCESS: {
      console.log('Load Notes Success')
      _isLoading = false
      _notes = action.notes.map(formatNote)
      _loadingError = null
      TasksStore.emitChange()
      break
    }

    case AppConstants.LOAD_NOTES_FAIL: {
      console.log('Load Notes fail')
      _loadingError = action.error 
      console.log(_loadingError)
      TasksStore.emitChange()
      break
    }

    default: {
      console.log('No such handler')
    }
  }
})

export default TasksStore