2018-07-10 14:32:48 +02:00
|
|
|
import moment from 'moment';
|
2018-07-05 22:40:24 +02:00
|
|
|
import Vue from 'vue';
|
|
|
|
|
2018-07-10 14:32:48 +02:00
|
|
|
import { messages, getBrowserLocales } from '@/i18n';
|
|
|
|
import { storageAvailable } from '@/tools';
|
2018-07-17 16:32:34 +02:00
|
|
|
import { TILE_SERVERS, DEFAULT_TILE_SERVER } from '@/constants';
|
2018-06-26 11:39:43 +02:00
|
|
|
import * as types from './mutations-types';
|
|
|
|
|
2018-07-17 16:32:34 +02:00
|
|
|
function loadSettingFromStorage(name) {
|
|
|
|
try {
|
|
|
|
const value = localStorage.getItem(name);
|
|
|
|
if (value) {
|
|
|
|
return JSON.parse(value);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Unable to load setting ${name}: ${e}.`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:32:48 +02:00
|
|
|
// Load settings from storage
|
|
|
|
let locale = null;
|
|
|
|
let preventSuspend = null;
|
2018-07-16 17:26:10 +02:00
|
|
|
let skipOnboarding = null;
|
2018-07-17 16:32:34 +02:00
|
|
|
let tileServer = null;
|
2018-07-10 14:32:48 +02:00
|
|
|
if (storageAvailable('localStorage')) {
|
2018-07-17 16:32:34 +02:00
|
|
|
preventSuspend = loadSettingFromStorage('preventSuspend');
|
|
|
|
skipOnboarding = loadSettingFromStorage('skipOnboarding');
|
|
|
|
|
|
|
|
tileServer = loadSettingFromStorage('tileServer');
|
|
|
|
if (!TILE_SERVERS[tileServer]) {
|
|
|
|
tileServer = null;
|
2018-07-16 17:26:10 +02:00
|
|
|
}
|
2018-07-10 14:32:48 +02:00
|
|
|
|
|
|
|
locale = localStorage.getItem('locale');
|
|
|
|
if (!messages[locale]) {
|
|
|
|
locale = null;
|
|
|
|
}
|
|
|
|
if (!locale) {
|
|
|
|
// Get best matching locale from browser
|
|
|
|
const locales = getBrowserLocales();
|
|
|
|
for (let i = 0; i < locales.length; i += 1) {
|
|
|
|
if (messages[locales[i]]) {
|
|
|
|
locale = locales[i];
|
|
|
|
break; // Break at first matching locale
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set moment locale
|
|
|
|
moment.locale(locale);
|
|
|
|
}
|
|
|
|
|
2018-06-26 11:39:43 +02:00
|
|
|
export const initialState = {
|
2018-07-17 16:37:46 +02:00
|
|
|
hasGoneThroughIntro: false,
|
2018-07-04 18:20:28 +02:00
|
|
|
isLoading: false,
|
2018-07-12 17:48:26 +02:00
|
|
|
reportDetails: {
|
|
|
|
id: null,
|
|
|
|
userAsked: null,
|
|
|
|
},
|
2018-06-26 11:39:43 +02:00
|
|
|
reports: [],
|
2018-07-10 14:32:48 +02:00
|
|
|
settings: {
|
|
|
|
locale: locale || 'en',
|
|
|
|
preventSuspend: preventSuspend || true,
|
2018-07-16 17:26:10 +02:00
|
|
|
skipOnboarding: skipOnboarding || false,
|
2018-07-17 16:32:34 +02:00
|
|
|
tileServer: tileServer || DEFAULT_TILE_SERVER,
|
2018-07-10 14:32:48 +02:00
|
|
|
},
|
2018-06-26 11:39:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
2018-07-17 16:37:46 +02:00
|
|
|
[types.INTRO_WAS_SEEN](state) {
|
|
|
|
state.hasGoneThroughIntro = true;
|
|
|
|
},
|
2018-07-04 18:20:28 +02:00
|
|
|
[types.IS_LOADING](state) {
|
|
|
|
state.isLoading = true;
|
|
|
|
},
|
|
|
|
[types.IS_DONE_LOADING](state) {
|
|
|
|
state.isLoading = false;
|
|
|
|
},
|
2018-07-10 14:32:48 +02:00
|
|
|
[types.SET_SETTING](state, { setting, value }) {
|
|
|
|
if (storageAvailable('localStorage')) {
|
2018-07-17 16:32:34 +02:00
|
|
|
localStorage.setItem(setting, JSON.stringify(value));
|
2018-07-10 14:32:48 +02:00
|
|
|
}
|
|
|
|
state.settings[setting] = value;
|
|
|
|
},
|
2018-07-12 17:48:26 +02:00
|
|
|
[types.SHOW_REPORT_DETAILS](state, { id, userAsked }) {
|
|
|
|
Vue.set(state.reportDetails, 'id', id);
|
|
|
|
Vue.set(state.reportDetails, 'userAsked', userAsked);
|
2018-07-05 22:40:24 +02:00
|
|
|
},
|
2018-06-26 11:39:43 +02:00
|
|
|
[types.STORE_REPORTS](state, { reports }) {
|
|
|
|
state.reports = reports;
|
|
|
|
},
|
|
|
|
[types.PUSH_REPORT](state, { report }) {
|
2018-07-05 22:40:24 +02:00
|
|
|
const reportIndex = state.reports.findIndex(item => item.id === report.id);
|
|
|
|
if (reportIndex === -1) {
|
|
|
|
state.reports.push(report);
|
|
|
|
} else {
|
|
|
|
Vue.set(state.reports, reportIndex, report);
|
|
|
|
}
|
2018-06-26 11:39:43 +02:00
|
|
|
},
|
|
|
|
};
|