import SyncController from '../../Controllers/SyncController'; // Database import { SendBrochure, Setting } from '../../../Database'; const initialState = { isSetup: false, locale: 'fr', currentUser: {}, logged: false, forceSync: false, currentOpenedDoc: {}, HomeBtns: {}, mainColor: '#D4021D', }; const reducer = (state = initialState, action) => { let saved; switch (action.type) { case 'INIT': let MainColor = Setting.get('MainColor'); saved = Setting.get('isSetup'); return { ...state, isSetup: saved === undefined ? false : saved?.value === 'true' ? true : false, mainColor: MainColor === undefined ? initialState.mainColor : MainColor === null ? initialState.mainColor : MainColor?.value, }; case 'INIT_HOME_BTNS': { let HomeFolders = Setting.get('HomeFolders'); let Btns = HomeFolders ? 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 { let userObj = Setting.get('isLogged'); user = userObj ? JSON.parse(userObj?.value) : false; 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 'SEND_BROCHURE': SendBrochure.add(action.data); return state; default: return state; } }; export default reducer;