const initialState = { locale: 'fr', users: {}, directories: {}, documents: {}, contacts: {}, syncVersion: 0, logged: undefined, currentUser: {}, currentOpenedDoc: {}, ShowIsActive: 0, }; const reducer = (state = initialState, action) => { switch (action.type) { case 'INIT': let docs = DocumentsModel.getAll(DirectoriesModel.getAll( state.currentUser.role, state.currentUser.authorizedFolders, false, state.locale )); return { ...state, users: UsersModel.getAll(), directories: DirectoriesModel.getAll( state.currentUser.role, state.currentUser.authorizedFolders, true, state.locale ), documents: docs.filter(item => item.lang === state.locale), contacts: ContactsModel.getByUser(state.currentUser.id), ShowIsActive: CommonModel.get('ShowIsActive')['value'], }; case 'INIT_USERS': return { ...state, users: UsersModel.getAll(), }; case 'SET_CURRENT_USER': return { ...state, currentUser: JSON.parse(action.data), }; case 'SET_LOGGED': if (action.data === false) { CommonModel.delete('isLogged'); } return { ...state, logged: action.data, }; 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; CommonModel.update({ key: 'locale', value: nxt }); let docsL = DocumentsModel.getAll(DirectoriesModel.getAll( state.currentUser.role, state.currentUser.authorizedFolders, false, nxt )); return { ...state, locale: nxt, directories: DirectoriesModel.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': Sync.updateShowIsActive(action.data); return { ...state, ShowIsActive: action.data, }; case 'UPDATE_CONTACT': ContactsModel.update(action.data); return { ...state, contacts: ContactsModel.getByUser(state.currentUser.id), }; case 'ADD_CONTACT': ContactsModel.add(action.data); return { ...state, contacts: ContactsModel.getByUser(state.currentUser.id), }; case 'DELETE_CONTACT': ContactsModel.delete(action.data); return { ...state, contacts: ContactsModel.getByUser(state.currentUser.id), }; case 'SEND_BROCHURE': SendBrochureModel.add(action.data); return state; default: return state; } } export default reducer;