export const SET_ACTIVITY_EXPANDED = 'SET_ACTIVITY_EXPANDED'
export const SET_STUDENT_EXPANDED = 'SET_STUDENT_EXPANDED'
export const SET_STUDENTS_EXPANDED = 'SET_STUDENTS_EXPANDED'
export const SET_STUDENT_SORT = 'SET_STUDENT_SORT'

export const SORT_BY_NAME = 'NAME'
export const SORT_BY_MOST_PROGRESS = 'MOST_PROGRESS'
export const SORT_BY_LEAST_PROGRESS = 'LEAST_PROGRESS'

export const SET_QUESTION_EXPANDED = 'SET_QUESTION_EXPANDED'
export const SELECT_QUESTION = 'SELECT_QUESTION'

export function setActivityExpanded (activityId, value) {
  return (dispatch, getState) => {
    dispatch({
      type: SET_ACTIVITY_EXPANDED,
      activityId,
      value
    })
    if (!value) {
      // When user collapses activity, check if anything else is expanded.
      // If not, collapse all the student rows too.
      const expandedActivities = getState().getIn(['dashboard', 'expandedActivities'])
      if (!expandedActivities.includes(true)) {
        getState().getIn(['dashboard', 'expandedStudents']).forEach((expanded, studentId) =>
          expanded && dispatch(setStudentExpanded(studentId, false))
        )
      }
    }
  }
}

export function setQuestionExpanded (questionId, value) {
  return {
    type: SET_QUESTION_EXPANDED,
    questionId,
    value: value
  }
}

function checkActivityExpanded (dispatch, getState) {
  const expandedActivities = getState().getIn(['dashboard', 'expandedActivities'])
  if (!expandedActivities.includes(true)) {
    const firstActivity = getState().getIn(['report', 'activities']).first()
    dispatch(setActivityExpanded(firstActivity.get('id'), true))
  }
}

export function setStudentExpanded (studentId, value) {
  return (dispatch, getState) => {
    dispatch({
      type: SET_STUDENT_EXPANDED,
      studentId,
      value
    })
    if (value) {
      // When user expands student row, check if there's at least one expanded activity.
      // If not, expand the fist one.
      checkActivityExpanded(dispatch, getState)
    }
  }
}

export function setStudentsExpanded (studentIds, value) {
  return (dispatch, getState) => {
    dispatch({
      type: SET_STUDENTS_EXPANDED,
      studentIds,
      value
    })
    if (value) {
      checkActivityExpanded(dispatch, getState)
    }
  }
}

export function setStudentSort (value) {
  return {
    type: SET_STUDENT_SORT,
    value
  }
}

export function selectQuestion (value) {
  return {
    type: SELECT_QUESTION,
    value
  }
}

@@ -481,16 +481,58 @@
 STION'%0A%0A
+export const TRACK_EVENT = 'TRACK_EVENT'%0A%0A
 export f
@@ -2463,24 +2463,447 @@
 ESTION,%0A    value%0A  %7D%0A%7D%0A
+%0Aexport function trackEvent(category, action, label) %7B%0A  return (dispatch, getState) =%3E %7B%0A    dispatch(%7B%0A      type: TRACK_EVENT,%0A      category,%0A      action,%0A      label%0A    %7D)%0A    const clazzName = getState().getIn(%5B'report', 'clazzName'%5D)%0A    let labelText = clazzName + ' - ' + label%0A    labelText = labelText.replace(/ - $/, '')%0A    gtag('event', action, %7B'event_category': category, 'event_label': labelText%7D)%0A  %7D%0A%7D%0A

Add function for tracking events