import SyncController from '../../Controllers/SyncController'; // Database import { SendBrochure, Setting } from '../../../Database'; const initialState = { isSetup: false, locale: 'fr', currentUser: {}, logged: false, syncVersion: 0, forceSync: false, currentOpenedDoc: {}, ShowIsActive: 0, HomeBtns: {}, mainColor: '#D4021D', FormSelect1: {}, FormSelect2: {}, FormCheck: [], }; const reducer = (state = initialState, action) => { let saved; switch (action.type) { case 'INIT': let ShowIsActive = Setting.get('ShowIsActive'); let MainColor = Setting.get('MainColor'); let FormSelect1 = Setting.get('FormSelect1'); let FormSelect2 = Setting.get('FormSelect2'); let FormCheck = Setting.get('FormCheck'); saved = Setting.get('isSetup'); return { ...state, isSetup: saved === undefined ? false : saved.value === 'true' ? true : false, ShowIsActive: ShowIsActive === undefined ? false : ShowIsActive.value, mainColor: MainColor === undefined ? initialState.mainColor : MainColor === false ? initialState.mainColor : MainColor.value, FormSelect1: FormSelect1 === undefined ? initialState.FormSelect1 : FormSelect1 === false ? initialState.FormSelect1 : JSON.parse(FormSelect1.value), FormSelect2: FormSelect2 === undefined ? initialState.FormSelect2 : FormSelect2 === false ? initialState.FormSelect2 : JSON.parse(FormSelect2.value), FormCheck: FormCheck === undefined ? initialState.FormCheck : FormCheck === false ? initialState.FormCheck : JSON.parse(FormCheck.value), }; case 'INIT_HOME_BTNS': { let HomeFolders = Setting.get('HomeFolders'); let Btns = JSON.parse(HomeFolders.value); return { ...state, HomeBtns: Btns, }; } case 'SET_ISSETUP': Setting.upsert({ key: 'isSetup', value: 'true' }); saved = Setting.get('isSetup'); return { ...state, isSetup: saved === undefined ? false : saved.value === 'true' ? true : false, }; case 'SET_CURRENT_USER': let user = false; let isLogged = false; if (action.data === false) { Setting.delete('isLogged'); } else { userObj = Setting.get('isLogged').value; user = JSON.parse(userObj); isLogged = true; } return { ...state, currentUser: user !== false ? user : initialState.currentUser, logged: isLogged, }; // case 'UPDATE_LOCALE': // let nxt; // if (action.data === undefined) { // const availableLocales = ['fr', 'en', 'de']; // if (availableLocales.indexOf(i18n.locale) === availableLocales.length - 1) { // nxt = availableLocales[0]; // } else { // nxt = availableLocales[availableLocales.indexOf(i18n.locale) + 1]; // } // } else { // nxt = action.data; // } // i18n.locale = nxt; // Setting.upsert({ key: 'locale', value: nxt }); // let docsL = Document.getAll(Directory.getAll( // state.currentUser.role, // state.currentUser.authorizedFolders, // false, // nxt // )); // return { // ...state, // locale: nxt, // directories: Directory.getAll( // state.currentUser.role, // state.currentUser.authorizedFolders, // true, // nxt // ), // documents: docsL.filter(item => item.lang === nxt), // }; case 'SET_CURRENT_OPENED_DOC': return { ...state, currentOpenedDoc: action.data, }; case 'UPDATE_VERSION': return { ...state, syncVersion: action.data, }; case 'UPDATE_SHOW_IS_ACTIVE': SyncController.updateShowIsActive(action.data); return { ...state, ShowIsActive: action.data, }; case 'SEND_BROCHURE': SendBrochure.add(action.data); return state; default: return state; } }; export default reducer;