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-09-07 12:16:39 +00:00
|
|
|
import moment from 'moment';
|
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-09-12 16:39:33 +00:00
|
|
|
let browserTimezoneOffset = moment().zone(); // eg, 240 - this will be converted on server as -(offset/60)
|
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);
|
2017-09-07 12:16:39 +00:00
|
|
|
|
2017-10-01 04:15:24 +00:00
|
|
|
if (AUTH_SETTINGS.auth && AUTH_SETTINGS.auth.apiId && AUTH_SETTINGS.auth.apiToken) {
|
|
|
|
|
axios.defaults.headers.common['x-api-user'] = AUTH_SETTINGS.auth.apiId;
|
|
|
|
|
axios.defaults.headers.common['x-api-key'] = AUTH_SETTINGS.auth.apiToken;
|
2017-09-07 12:16:39 +00:00
|
|
|
|
2017-10-01 04:15:24 +00:00
|
|
|
axios.defaults.headers.common['x-user-timezoneOffset'] = browserTimezoneOffset;
|
|
|
|
|
|
|
|
|
|
isUserLoggedIn = true;
|
|
|
|
|
}
|
2017-06-29 18:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-22 16:26:53 +00:00
|
|
|
const i18nData = window && window['habitica-i18n'];
|
|
|
|
|
|
|
|
|
|
let availableLanguages = [];
|
|
|
|
|
let selectedLanguage = {};
|
|
|
|
|
|
|
|
|
|
if (i18nData) {
|
|
|
|
|
availableLanguages = i18nData.availableLanguages;
|
|
|
|
|
selectedLanguage = i18nData.language;
|
|
|
|
|
}
|
|
|
|
|
|
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-09-20 13:04:35 +00:00
|
|
|
isUserLoaded: false, // Means the user and the user's tasks are ready
|
2017-09-22 11:29:08 +00:00
|
|
|
isAmazonReady: false, // Whether the Amazon Payments lib can be used
|
2017-03-18 17:33:08 +00:00
|
|
|
user: asyncResourceFactory(),
|
2017-10-01 04:15:24 +00:00
|
|
|
credentials: isUserLoggedIn ? {
|
2017-09-20 13:04:35 +00:00
|
|
|
API_ID: AUTH_SETTINGS.auth.apiId,
|
|
|
|
|
API_TOKEN: AUTH_SETTINGS.auth.apiToken,
|
|
|
|
|
} : {},
|
2017-09-12 16:39:33 +00:00
|
|
|
// store the timezone offset in case it's different than the one in
|
|
|
|
|
// user.preferences.timezoneOffset and change it after the user is synced
|
|
|
|
|
// in app.vue
|
|
|
|
|
browserTimezoneOffset,
|
2017-03-18 17:33:08 +00:00
|
|
|
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-09-07 19:26:53 +00:00
|
|
|
challengeOptions: {
|
|
|
|
|
cloning: false,
|
|
|
|
|
tasksToClone: {},
|
2017-09-25 18:02:12 +00:00
|
|
|
workingChallenge: {},
|
2017-09-07 19:26:53 +00:00
|
|
|
},
|
2017-09-22 11:29:08 +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
|
2017-09-22 11:29:08 +00:00
|
|
|
// @TODO apply freezing to the entire codebase (the server) and not only to the client side?
|
2017-03-18 17:33:08 +00:00
|
|
|
// 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-08-22 16:26:53 +00:00
|
|
|
i18n: deepFreeze({
|
|
|
|
|
availableLanguages,
|
|
|
|
|
selectedLanguage,
|
|
|
|
|
}),
|
2017-07-31 19:54:52 +00:00
|
|
|
hideHeader: false,
|
2017-08-23 03:58:13 +00:00
|
|
|
memberModalOptions: {
|
|
|
|
|
viewingMembers: [],
|
|
|
|
|
groupId: '',
|
2017-09-22 21:47:16 +00:00
|
|
|
challengeId: '',
|
2017-08-23 03:58:13 +00:00
|
|
|
group: {},
|
|
|
|
|
},
|
2017-08-16 22:34:25 +00:00
|
|
|
openedItemRows: [],
|
2017-08-22 05:04:56 +00:00
|
|
|
spellOptions: {
|
|
|
|
|
castingSpell: false,
|
|
|
|
|
spellDrawOpen: true,
|
|
|
|
|
},
|
2017-08-30 21:19:17 +00:00
|
|
|
profileOptions: {
|
|
|
|
|
startingPage: '',
|
|
|
|
|
},
|
2017-09-15 18:11:58 +00:00
|
|
|
gemModalOptions: {
|
|
|
|
|
startingPage: '',
|
|
|
|
|
},
|
2017-08-26 02:56:21 +00:00
|
|
|
profileUser: {},
|
|
|
|
|
upgradingGroup: {},
|
|
|
|
|
notificationStore: [],
|
2017-09-05 18:34:00 +00:00
|
|
|
modalStack: [],
|
2017-09-19 20:35:32 +00:00
|
|
|
userIdToMessage: '',
|
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
|
|
|
}
|