<commit_before>import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import router from '../router'

import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

const store = new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    problems: {},
    contest: {},
    scores: [],
    langs: [],
    alerts: [],
    user: null,
    sourceCodes: {},
    loginToken: ''
  },
  actions: {
    LOAD_PROBLEMS: function (context) {
      axios.get('/api/problems').then((response) => {
        context.commit('SET_PROBLEMS', { problems: response.data })
      })
    },
    LOAD_SCORES: function (context, contestId) {
      axios.get('/api/scores/' + contestId).then((response) => {
        context.commit('SET_SCORES', { scores: response.data })
      })
    },
    LOAD_USER: function (context) {
      axios.get('/api/current-user').then((response) => {
        context.commit('SET_USER', { user: response.data })
      })
    },
    LOAD_CONTEST: function (context) {
      axios.get('/api/get-contest-info').then((response) => {
        context.commit('SET_CONTEST', { contest: response.data })
        context.dispatch('LOAD_SCORES', response.data.id)
      })
    },
    LOAD_LANGS: function (context) {
      axios.get('/api/languages').then((response) => {
        context.commit('SET_LANGS', { langs: response.data })
      })
    },
    LOGIN: function (context, creds) {
      axios.post('/api/login', {
        email: creds.email,
        password: <PASSWORD>
      }).then((response) => {
        context.commit('SET_LOGIN_TOKEN', { token: response.data.access_token })

        axios.defaults.headers.common['Authorization'] = 'Bearer ' + store.state.loginToken

        context.dispatch('LOAD_USER')
        context.dispatch('LOAD_PROBLEMS')
        context.dispatch('LOAD_CONTEST')
        context.dispatch('DELETE_ALERTS')

        router.push({ path: '/' })
      }).catch(function (error) {
        console.log(error)
        context.commit('PUSH_ALERT', {text: 'Failed to login', severity: 'danger'})
      })
    },
    LOGOUT: function (context) {
      // add timeout to make logout feel more real
      setTimeout(function () {
        context.commit('SET_LOGIN_TOKEN', { token: '' })
        context.commit('SET_USER', { user: null })
        context.commit('SET_PROBLEMS', { problems: {} })
      }, 200)
      router.push({ path: '/' })
    }
  },
  mutations: {
    SET_PROBLEMS: (state, { problems }) => {
      state.problems = problems
    },
    SET_USER: (state, { user }) => {
      state.user = user
    },
    SET_SCORES: (state, { scores }) => {
      state.scores = scores
    },
    SET_LANGS: (state, { langs }) => {
      var langObj = {}
      for (var lang of langs) {
        langObj[lang.name] = lang
      }<commit_msg>redirect to login page on logout<commit_after>import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import router from '../router'

import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

const store = new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    problems: {},
    contest: {},
    scores: [],
    langs: [],
    alerts: [],
    user: null,
    sourceCodes: {},
    loginToken: ''
  },
  actions: {
    LOAD_PROBLEMS: function (context) {
      axios.get('/api/problems').then((response) => {
        context.commit('SET_PROBLEMS', { problems: response.data })
      })
    },
    LOAD_SCORES: function (context, contestId) {
      axios.get('/api/scores/' + contestId).then((response) => {
        context.commit('SET_SCORES', { scores: response.data })
      })
    },
    LOAD_USER: function (context) {
      axios.get('/api/current-user').then((response) => {
        context.commit('SET_USER', { user: response.data })
      })
    },
    LOAD_CONTEST: function (context) {
      axios.get('/api/get-contest-info').then((response) => {
        context.commit('SET_CONTEST', { contest: response.data })
        context.dispatch('LOAD_SCORES', response.data.id)
      })
    },
    LOAD_LANGS: function (context) {
      axios.get('/api/languages').then((response) => {
        context.commit('SET_LANGS', { langs: response.data })
      })
    },
    LOGIN: function (context, creds) {
      axios.post('/api/login', {
        email: creds.email,
        password: <PASSWORD>
      }).then((response) => {
        context.commit('SET_LOGIN_TOKEN', { token: response.data.access_token })

        axios.defaults.headers.common['Authorization'] = 'Bearer ' + store.state.loginToken

        context.dispatch('LOAD_USER')
        context.dispatch('LOAD_PROBLEMS')
        context.dispatch('LOAD_CONTEST')
        context.dispatch('DELETE_ALERTS')

        router.push({ path: '/' })
      }).catch(function (error) {
        console.log(error)
        context.commit('PUSH_ALERT', {text: 'Failed to login', severity: 'danger'})
      })
    },
    LOGOUT: function (context) {
      // add timeout to make logout feel more real
      setTimeout(function () {
        context.commit('SET_LOGIN_TOKEN', { token: '' })
        context.commit('SET_USER', { user: null })
        context.commit('SET_PROBLEMS', { problems: {} })
      }, 200)
      router.push({ path: '/login' })
    }
  },
  mutations: {
    SET_PROBLEMS: (state, { problems }) => {
      state.problems = problems
    },
    SET_USER: (state, { user }) => {
      state.user = user
    },
    SET_SCORES: (state, { scores }) => {
      state.scores = scores
    },
    SET_LANGS: (state, { langs }) => {
      var langObj = {}
      for (var lang of langs) {
        langObj[lang.name] = lang
      }
{'fraction_non_alphanumeric': 0.16571834992887624, 'fraction_numerical': 0.0010668563300142249, 'mean_word_length': 2.564638783269962, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '5443809', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1814, 'n_words': 324}