{"size": 3166, "ext": "js", "max_stars_repo_path": "src/store/index.js", "max_stars_repo_name": "fajnie-ze-wiesz/fzw-webapp", "max_stars_repo_stars_event_min_datetime": "2018-04-10T11:59:29.000Z", "max_stars_repo_stars_event_max_datetime": "2019-12-31T11:36:26.000Z", "max_issues_repo_path": "src/store/index.js", "max_issues_repo_name": "fajnie-ze-wiesz/fzw-webapp", "max_issues_repo_issues_event_min_datetime": "2019-07-06T22:57:04.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-06T12:19:19.000Z", "max_forks_repo_path": "src/store/index.js", "max_forks_repo_name": "fajnie-ze-wiesz/fzw-webapp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 17, "max_forks_count": null, "avg_line_length": 30.4423076923, "max_line_length": 88, "alphanum_fraction": 0.6626658244}
import Vue from 'vue';
import Vuex from 'vuex';
import { QUESTION_LANGUAGE } from '../consts';
import { generateQuiz, loadQuizImages } from '../services/quizes';
import { ping } from '../services/ping';
import ManipulationCategoryService from '../services/manipulation_category';

import ManipulationCategory from '../data/manipulation_category';
import Quiz from '../data/quiz';
import QuizSetupInfo from '../data/quiz_setup_info';
import User from '../data/user';
import ResultStats from '../data/result_stats';
import TopicCategory from '../data/topic_category';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    quiz: Quiz.create({id: null}),
    user: User.create({name: 'test'}),
    quizSetupInfo: QuizSetupInfo.create({}),
    manipulationCategories: [],
  },
  actions: {
    fetchManipulationCategories(context) {
      ManipulationCategoryService.fetchList().then((manipulationCategories) => {
        context.commit('loadManipulationCategories', {manipulationCategories});
      });
    },
    async generateQuiz(context) {
      const language = QUESTION_LANGUAGE;
      const topicCategory = QuizSetupInfo.getTopicCategory(context.state.quizSetupInfo);
      const quiz = await generateQuiz({topicCategory, language});
      const images = await loadQuizImages(quiz);
      context.commit('loadQuiz', {
        quiz, images,
      });
    },
    pingBackend() {
      ping();
    },
  },
  mutations: {
    loadQuiz(state, { quiz }) {
      state.quiz = quiz;
    },
    loadManipulationCategories(state, { manipulationCategories }) {
      state.manipulationCategories = manipulationCategories;
    },
    answerQuizQuestion(state, { answer }) {
      Quiz.answerQuestion(state.quiz, answer);
    },
    omitQuizQuestion(state) {
      Quiz.omitQuestion(state.quiz);
    },
    setUserInfo(state, userInfo) {
      User.setName(state.user, userInfo.name);
      let topicCategory = null;
      if (userInfo.category && userInfo.category !== 'all') {
        topicCategory = TopicCategory.fromName(userInfo.category);
      }
      QuizSetupInfo.setValues(state.quizSetupInfo, {
        topicCategory,
        correctnessEstimate: userInfo.prediction / 100,
      });
    },
  },
  getters: {
    isQuizLoaded(state) {
      return !!Quiz.getId(state.quiz);
    },
    manipulationCategories(state) {
      return state.manipulationCategories;
    },
    manipulationCategoriesMap(state) {
      return ManipulationCategory.mapFromList(state.manipulationCategories);
    },
    currentQuizQuestion(state) {
      return Quiz.getCurrentQuestion(state.quiz);
    },
    quizQuestions(state) {
      return Quiz.getQuestions(state.quiz);
    },
    quizQuestionIndex(state) {
      return Quiz.getQuestionIndex(state.quiz);
    },
    numOfQuizQuestions(state) {
      return Quiz.getQuestions(state.quiz).length;
    },
    isQuizFinished(state) {
      return Quiz.isFinished(state.quiz);
    },
    resultStats(state) {
      const { quiz, user, quizSetupInfo, manipulationCategories } = state;
      return ResultStats.create({
        quiz,
        user,
        quizSetupInfo,
        manipulationCategories,
      });
    },
  },
});
