<commit_before>
import { COMPONENT_NAMES } from 'ozaria/site/components/teacher-dashboard/common/constants.js'

export default {
  namespaced: true,

  state: {
    teacherId: '',
    classroomId: '', // current classrom id for single class page and projects page
    selectedCourseIdForClassroom: {}, // selectedCourse for each classroomId across single class and student projects page
    loading: false,
    pageTitle: '',
    componentName: '',
    trackCategory: 'Teachers' // used for tracking events on pages shared between DSA and DT
  },

  mutations: {
    setTeacherId (state, teacherId) {
      if (state.teacherId !== teacherId) {
        state.teacherId = teacherId
      }
    },
    setClassroomId (state, classroomId) {
      if (state.classroomId !== classroomId) {
        state.classroomId = classroomId
      }
    },
    startLoading (state) {
      state.loading = true
    },
    stopLoading (state) {
      state.loading = false
    },
    resetLoadingState (state) {
      state.loading = false
    },
    setSelectedCourseIdCurrentClassroom (state, { courseId }) {
      if (state.classroomId) {
        Vue.set(state.selectedCourseIdForClassroom, state.classroomId, courseId)
      }
    },
    setPageTitle (state, title) {
      state.pageTitle = title
    },
    setComponentName (state, componentName) {
      state.componentName = componentName
    },
    setTrackCategory (state, trackCategory) {
      state.trackCategory = trackCategory
    }
  },

  getters: {
    teacherId (state) {
      return state.teacherId
    },
    classroomId (state) {
      return state.classroomId
    },
    classroom (state, getters) {
      return getters.getActiveClassrooms.find((c) => c._id === state.classroomId) || {}
    },
    getPageTitle (state) {
      return Vue.t(`teacher_dashboard.${state.pageTitle}`) || state.pageTitle
    },
    getComponentName (state) {
      return state.componentName
    },
    getLoadingState (state) {
      return state.loading
    },
    getTrackCategory (state) {
      return state.trackCategory
    },
    getActiveClassrooms (state, _getters, _rootState, rootGetters) {
      if (state.teacherId) {
        return rootGetters['classrooms/getActiveClassroomsByTeacher'](state.teacherId) || []
      } else {
        return []
      }
    },
    getArchivedClassrooms (state, _getters, _rootState, rootGetters) {
      if (state.teacherId) {
        return rootGetters['classrooms/getArchivedClassroomsByTeacher'](state.teacherId) || []
      } else {
        return []
      }
    },
    getSharedClassrooms (state, _getters, _rootState, rootGetters) {
      if (state.teacherId) {
        return rootGetters['classrooms/getSharedClassroomsByTeacher'](state.teacherId) || []
      } else {
        return []
      }
    },
    getCurrentClassroom (state, _getters, _rootState, rootGetters) {
      if (state.teacherId && state.classroomId) {
        let classrooms = [
          ...(rootGetters['classrooms/getActiveClassroomsByTeacher'](state.teacherId) || []),
          ...(rootGetters['classrooms/getSharedClassroomsByTeacher'](state.teacherId) || [])
        ]
        if (!classrooms || classrooms.length === 0){
          return rootGetters['classrooms/getClassroomById'](state.classroomId) || {}
        }
        return classrooms.find((c) => c._id === state.classroomId) || {}
      } else {
        return {}
      }
    },
    getCoursesCurrentClassroom (state, getters, _rootState, rootGetters) {
      if (state.classroomId) {
        const classroom = getters['getCurrentClassroom']
        const classroomCourseIds = (classroom.courses || []).map((c) => c._id) || []
        const courses = rootGetters['courses/sorted'] || []
        return courses.filter((c) => classroomCourseIds.includes(c._id))
      }
      return []
    },
    getSelectedCourseIdCurrentClassroom (state, getters) {
      if (state.classroomId && state.selectedCourseIdForClassroom[state.classroomId]) {
        return state.selectedCourseIdForClassroom[state.classroomId]
      } else { // TODO default should be last assigned course
        const classroomCourses = getters['getCoursesCurrentClassroom'] || []
        if (classroomCourses.length > 0) {
          return (classroomCourses[0] || {})._id
        }
      }
    },
    getMembersCurrentClassroom (state, getters, _rootState, rootGetters) {
      if (state.classroomId) {
        const classroom = getters['getCurrentClassroom']
        return rootGetters['users/getClassroomMembers'](classroom) || []
      }
      return []
    },
    getLevelSessionsMapCurrentClassroom (state, _getters, _rootState, rootGetters) {
      if (state.classroomId) {
        return rootGetters['levelSessions/getSessionsMapForClassroom'](state.classroomId) || {}<commit_msg>DT: open to last-viewed chapter (#788)<commit_after>
import { COMPONENT_NAMES } from 'ozaria/site/components/teacher-dashboard/common/constants.js'

function getLastSelectedCourseKey (state) {
  return `courseId_${state.teacherId}_${state.classroomId}`
}

export default {
  namespaced: true,

  state: {
    teacherId: '',
    classroomId: '', // current classrom id for single class page and projects page
    selectedCourseIdForClassroom: {}, // selectedCourse for each classroomId across single class and student projects page
    loading: false,
    pageTitle: '',
    componentName: '',
    trackCategory: 'Teachers' // used for tracking events on pages shared between DSA and DT
  },

  mutations: {
    setTeacherId (state, teacherId) {
      if (state.teacherId !== teacherId) {
        state.teacherId = teacherId
      }
    },
    setClassroomId (state, classroomId) {
      if (state.classroomId !== classroomId) {
        state.classroomId = classroomId
      }
    },
    startLoading (state) {
      state.loading = true
    },
    stopLoading (state) {
      state.loading = false
    },
    resetLoadingState (state) {
      state.loading = false
    },
    setSelectedCourseIdCurrentClassroom (state, { courseId }) {
      if (state.classroomId) {
        localStorage.setItem(getLastSelectedCourseKey(state), courseId);
        Vue.set(state.selectedCourseIdForClassroom, state.classroomId, courseId)
      }
    },
    setPageTitle (state, title) {
      state.pageTitle = title
    },
    setComponentName (state, componentName) {
      state.componentName = componentName
    },
    setTrackCategory (state, trackCategory) {
      state.trackCategory = trackCategory
    }
  },

  getters: {
    teacherId (state) {
      return state.teacherId
    },
    classroomId (state) {
      return state.classroomId
    },
    classroom (state, getters) {
      return getters.getActiveClassrooms.find((c) => c._id === state.classroomId) || {}
    },
    getPageTitle (state) {
      return Vue.t(`teacher_dashboard.${state.pageTitle}`) || state.pageTitle
    },
    getComponentName (state) {
      return state.componentName
    },
    getLoadingState (state) {
      return state.loading
    },
    getTrackCategory (state) {
      return state.trackCategory
    },
    getActiveClassrooms (state, _getters, _rootState, rootGetters) {
      if (state.teacherId) {
        return rootGetters['classrooms/getActiveClassroomsByTeacher'](state.teacherId) || []
      } else {
        return []
      }
    },
    getArchivedClassrooms (state, _getters, _rootState, rootGetters) {
      if (state.teacherId) {
        return rootGetters['classrooms/getArchivedClassroomsByTeacher'](state.teacherId) || []
      } else {
        return []
      }
    },
    getSharedClassrooms (state, _getters, _rootState, rootGetters) {
      if (state.teacherId) {
        return rootGetters['classrooms/getSharedClassroomsByTeacher'](state.teacherId) || []
      } else {
        return []
      }
    },
    getCurrentClassroom (state, _getters, _rootState, rootGetters) {
      if (state.teacherId && state.classroomId) {
        let classrooms = [
          ...(rootGetters['classrooms/getActiveClassroomsByTeacher'](state.teacherId) || []),
          ...(rootGetters['classrooms/getSharedClassroomsByTeacher'](state.teacherId) || [])
        ]
        if (!classrooms || classrooms.length === 0){
          return rootGetters['classrooms/getClassroomById'](state.classroomId) || {}
        }
        return classrooms.find((c) => c._id === state.classroomId) || {}
      } else {
        return {}
      }
    },
    getCoursesCurrentClassroom (state, getters, _rootState, rootGetters) {
      if (state.classroomId) {
        const classroom = getters['getCurrentClassroom']
        const classroomCourseIds = (classroom.courses || []).map((c) => c._id) || []
        const courses = rootGetters['courses/sorted'] || []
        return courses.filter((c) => classroomCourseIds.includes(c._id))
      }
      return []
    },
    getSelectedCourseIdCurrentClassroom (state, getters) {
      if (state.classroomId && state.selectedCourseIdForClassroom[state.classroomId]) {
        return state.selectedCourseIdForClassroom[state.classroomId]
      } else {

        const savedCourseId = localStorage.getItem(getLastSelectedCourseKey(state));
        if(savedCourseId){
          return savedCourseId;
        }

        const classroomCourses = getters['getCoursesCurrentClassroom'] || []
        if (classroomCourses.length > 0) {
          return (classroomCourses[0] || {})._id
        }
      }
    },
    getMembersCurrentClassroom (state, getters, _rootState, rootGetters) {
      if (state.classroomId) {
        const classroom = getters['getCurrentClassroom']
        return rootGetters['users/getClassroomMembers'](classroom) || []
      }
      return []
    },
    getLevelSessionsMapCurrentClassroom (state, _getters, _rootState, rootGetters) {
      if (state.classroomId) {
        return rootGetters['levelSessions/getSessionsMapForClassroom'](state.classroomId) || {}
{'fraction_non_alphanumeric': 0.11962692619626926, 'fraction_numerical': 0.0009124087591240876, 'mean_word_length': 2.8762278978388998, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '5000273', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2925, 'n_words': 535}