<commit_before>import AppDispatcher from './AppDispatcher'
import EventEmitter from 'events'
import $ from 'jQuery'

export const COURSES_UPDATE_EVENT = 'update'
export const COURSE_SELECT_EVENT = 'select'
export const COURSE_UNSELECT_EVENT = 'unselect'

// Exported for testing only
export const FILTER_ACTION = 'filter'
export const FETCH_ACTION  = 'fetch'
export const SELECT_ACTION  = 'select'
export const UNSELECT_ACTION  = 'unselect'

export class CourseActions {
  static filter(filter) {
    AppDispatcher.dispatch({
      actionType: FILTER_ACTION,
      text: filter
    })
  }

  static fetch() {
    AppDispatcher.dispatch({
      actionType: FETCH_ACTION
    })
  }

  static select(course) {
    AppDispatcher.dispatch({
      actionType: SELECT_ACTION,
      course: course
    })
  }

  static unselect(course) {
    AppDispatcher.dispatch({
      actionType: UNSELECT_ACTION,
      course: course
    })
  }
}

let courses = null
let filterText = null
let selectedCourses = []

function fetch(callback) {
  $.getJSON('/data/courses.json', (data) => {
    // TODO Handle error
    courses = data.courses
    callback(data.courses)
  })
}

function filter(text) {
  let filter = text.toLowerCase()
  // FIXME naive filter. make it more robust
  return courses.filter(function(item) {
    let name = item.name.toLowerCase()
    if (name.includes(filter)) {
      return true
    }
    return false
  })
}

function select(course) {
  if (selectedCourses.includes( c => c.slug == course.slug)) {
    return
  }

  selectedCourses.push(course)
}

function unselect(course) {
  let idx = selectedCourses.findIndex( (c) => c.slug == course.slug)
  if (idx != -1) {
    selectedCourses.splice(idx, 1)
  }
}

class CourseStore extends EventEmitter {
  getCourses() {
    if (filterText) {
      return filter(filterText)
    }
    return courses
  }

  getFilterText() {
    return filterText
  }

  getSelected() {
    return selectedCourses
  }
}

const courseStore = new CourseStore()

AppDispatcher.register((action) => {
  switch(action.actionType) {
    case FILTER_ACTION:
      filterText = action.text.trim()
      courseStore.emit(COURSES_UPDATE_EVENT, courseStore.getCourses())
      break

    case FETCH_ACTION:
      fetch(() => courseStore.emit(COURSES_UPDATE_EVENT, courseStore.getCourses()))
      break

    case SELECT_ACTION:
      select(action.course)
      courseStore.emit(COURSE_SELECT_EVENT, action.course)<commit_msg>Refactor CourseStore to add a selected attribute to each course<commit_after>import AppDispatcher from './AppDispatcher'
import EventEmitter from 'events'
import $ from 'jQuery'

export const COURSES_UPDATE_EVENT = 'update'
export const COURSE_SELECT_EVENT = 'select'
export const COURSE_UNSELECT_EVENT = 'unselect'

// Exported for testing only
export const FILTER_ACTION = 'filter'
export const FETCH_ACTION  = 'fetch'
export const SELECT_ACTION  = 'select'
export const UNSELECT_ACTION  = 'unselect'

export class CourseActions {
  static filter(filter) {
    AppDispatcher.dispatch({
      actionType: FILTER_ACTION,
      text: filter
    })
  }

  static fetch() {
    AppDispatcher.dispatch({
      actionType: FETCH_ACTION
    })
  }

  static select(course) {
    AppDispatcher.dispatch({
      actionType: SELECT_ACTION,
      course: course
    })
  }

  static unselect(course) {
    AppDispatcher.dispatch({
      actionType: UNSELECT_ACTION,
      course: course
    })
  }
}

let courses = null
let filterText = null

function fetch(callback) {
  $.getJSON('/data/courses.json', (data) => {
    // TODO Handle error
    courses = data.courses
    callback(data.courses)
  })
}

function filter(text) {
  let filter = text.toLowerCase()
  // FIXME naive filter. make it more robust
  return courses.filter(function(item) {
    let name = item.name.toLowerCase()
    if (name.includes(filter)) {
      return true
    }
    return false
  })
}

function selected() {
  return courses.filter( (c) => c.selected)
}

function select(course) {
  course.selected = true
}

function unselect(course) {
  course.selected = false
}

class CourseStore extends EventEmitter {
  getCourses() {
    if (filterText) {
      return filter(filterText)
    }
    return courses
  }

  getFilterText() {
    return filterText
  }

  getSelected() {
    return selected()
  }
}

const courseStore = new CourseStore()

AppDispatcher.register((action) => {
  switch(action.actionType) {
    case FILTER_ACTION:
      filterText = action.text.trim()
      courseStore.emit(COURSES_UPDATE_EVENT, courseStore.getCourses())
      break

    case FETCH_ACTION:
      fetch(() => courseStore.emit(COURSES_UPDATE_EVENT, courseStore.getCourses()))
      break

    case SELECT_ACTION:
      select(action.course)
      courseStore.emit(COURSE_SELECT_EVENT, action.course)
{'fraction_non_alphanumeric': 0.10397171969224371, 'fraction_numerical': 0.0004158868787689748, 'mean_word_length': 3.364791288566243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6770628', 'n_tokens_mistral': 1516, 'n_tokens_neox': 1490, 'n_words': 350}