2017-03-18 17:33:08 +00:00
|
|
|
import Store from 'client/libs/store';
|
|
|
|
|
import deepFreeze from 'client/libs/deepFreeze';
|
|
|
|
|
import content from 'common/script/content/index';
|
2017-08-01 12:30:17 +00:00
|
|
|
import * as commonConstants from 'common/script/constants';
|
|
|
|
|
import { DAY_MAPPING } from 'common/script/cron';
|
2017-03-18 17:33:08 +00:00
|
|
|
import { asyncResourceFactory } from 'client/libs/asyncResource';
|
2017-06-29 18:49:05 +00:00
|
|
|
import axios from 'axios';
|
2017-03-18 17:33:08 +00:00
|
|
|
|
2016-12-07 01:11:40 +00:00
|
|
|
import actions from './actions';
|
2016-12-09 07:01:59 +00:00
|
|
|
import getters from './getters';
|
2016-12-06 21:17:23 +00:00
|
|
|
|
2017-06-29 18:49:05 +00:00
|
|
|
const IS_TEST = process.env.NODE_ENV === 'test'; // eslint-disable-line no-process-env
|
|
|
|
|
|
|
|
|
|
// Load user auth parameters and determine if it's logged in
|
|
|
|
|
// before trying to load data
|
|
|
|
|
let isUserLoggedIn = false;
|
2017-08-07 20:26:17 +00:00
|
|
|
axios.defaults.headers.common['x-client'] = 'habitica-web';
|
2017-06-29 18:49:05 +00:00
|
|
|
|
|
|
|
|
let AUTH_SETTINGS = localStorage.getItem('habit-mobile-settings');
|
|
|
|
|
|
|
|
|
|
if (AUTH_SETTINGS) {
|
|
|
|
|
AUTH_SETTINGS = JSON.parse(AUTH_SETTINGS);
|
|
|
|
|
axios.defaults.headers.common['x-api-user'] = AUTH_SETTINGS.auth.apiId;
|
|
|
|
|
axios.defaults.headers.common['x-api-key'] = AUTH_SETTINGS.auth.apiToken;
|
|
|
|
|
isUserLoggedIn = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-18 17:33:08 +00:00
|
|
|
// Export a function that generates the store and not the store directly
|
2017-06-29 18:49:05 +00:00
|
|
|
// so that we can regenerate it multiple times for testing, when not testing
|
|
|
|
|
// always export the same route
|
|
|
|
|
|
|
|
|
|
let existingStore;
|
2017-03-18 17:33:08 +00:00
|
|
|
export default function () {
|
2017-06-29 18:49:05 +00:00
|
|
|
if (!IS_TEST && existingStore) return existingStore;
|
|
|
|
|
|
|
|
|
|
existingStore = new Store({
|
2017-03-18 17:33:08 +00:00
|
|
|
actions,
|
|
|
|
|
getters,
|
|
|
|
|
state: {
|
|
|
|
|
title: 'Habitica',
|
2017-06-29 18:49:05 +00:00
|
|
|
isUserLoggedIn,
|
2017-03-18 17:33:08 +00:00
|
|
|
user: asyncResourceFactory(),
|
|
|
|
|
tasks: asyncResourceFactory(), // user tasks
|
2017-08-01 12:30:17 +00:00
|
|
|
completedTodosStatus: 'NOT_LOADED',
|
2017-06-08 21:33:23 +00:00
|
|
|
party: {
|
2017-06-27 20:02:55 +00:00
|
|
|
quest: {},
|
2017-06-08 21:33:23 +00:00
|
|
|
members: asyncResourceFactory(),
|
|
|
|
|
},
|
2017-07-27 17:41:23 +00:00
|
|
|
shops: {
|
|
|
|
|
market: asyncResourceFactory(),
|
2017-07-31 23:04:40 +00:00
|
|
|
quests: asyncResourceFactory(),
|
|
|
|
|
seasonal: asyncResourceFactory(),
|
|
|
|
|
'time-travelers': asyncResourceFactory(),
|
2017-07-27 17:41:23 +00:00
|
|
|
},
|
2017-06-02 20:55:02 +00:00
|
|
|
myGuilds: [],
|
2017-07-31 19:54:52 +00:00
|
|
|
publicGuilds: [],
|
|
|
|
|
groupFormOptions: {
|
|
|
|
|
creatingParty: false,
|
2017-08-01 18:52:49 +00:00
|
|
|
groupId: '',
|
|
|
|
|
},
|
|
|
|
|
avatarEditorOptions: {
|
|
|
|
|
editingUser: false,
|
2017-08-16 21:51:48 +00:00
|
|
|
startingPage: '',
|
|
|
|
|
subPage: '',
|
2017-07-31 19:54:52 +00:00
|
|
|
},
|
2017-08-03 20:04:03 +00:00
|
|
|
flagChatOptions: {
|
|
|
|
|
message: {},
|
|
|
|
|
groupId: '',
|
|
|
|
|
},
|
2017-06-08 21:33:23 +00:00
|
|
|
editingGroup: {}, // TODO move to local state
|
2017-03-18 17:33:08 +00:00
|
|
|
// content data, frozen to prevent Vue from modifying it since it's static and never changes
|
|
|
|
|
// TODO apply freezing to the entire codebase (the server) and not only to the client side?
|
|
|
|
|
// NOTE this takes about 10-15ms on a fast computer
|
|
|
|
|
content: deepFreeze(content),
|
2017-08-01 12:30:17 +00:00
|
|
|
constants: deepFreeze({...commonConstants, DAY_MAPPING}),
|
2017-07-31 19:54:52 +00:00
|
|
|
hideHeader: false,
|
|
|
|
|
viewingMembers: [],
|
2017-08-16 22:34:25 +00:00
|
|
|
openedItemRows: [],
|
2017-08-15 18:28:30 +00:00
|
|
|
castingSpell: false,
|
2017-03-18 17:33:08 +00:00
|
|
|
},
|
2016-12-06 21:17:23 +00:00
|
|
|
});
|
2017-06-29 18:49:05 +00:00
|
|
|
|
|
|
|
|
return existingStore;
|
2017-06-02 20:55:02 +00:00
|
|
|
}
|