ldc-com-app/ComApp/src/App/Store/Reducers/AppReducer.js
2022-10-06 16:25:15 +02:00

133 lines
No EOL
4.1 KiB
JavaScript

import SyncController from "../../Controllers/SyncController";
// Database
import { Setting, Contact, User, Directory, Document, SendBrochure } from '../../../Database';
// Translations
import i18n from "../../../I18n";
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 = Document.getAll(Directory.getAll(
state.currentUser.role,
state.currentUser.authorizedFolders,
false,
state.locale
));
return {
...state,
users: User.getAll(),
directories: Directory.getAll(
state.currentUser.role,
state.currentUser.authorizedFolders,
true,
state.locale
),
documents: docs.filter(item => item.lang === state.locale),
contacts: Contact.getByUser(state.currentUser.id),
ShowIsActive: Setting.get('ShowIsActive')['value'],
};
case 'INIT_USERS':
return {
...state,
users: User.getAll(),
};
case 'SET_CURRENT_USER':
return {
...state,
currentUser: JSON.parse(action.data),
};
case 'SET_LOGGED':
if (action.data === false) {
Setting.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;
Setting.update({ 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 'UPDATE_CONTACT':
Contact.update(action.data);
return {
...state,
contacts: Contact.getByUser(state.currentUser.id),
};
case 'ADD_CONTACT':
Contact.add(action.data);
return {
...state,
contacts: Contact.getByUser(state.currentUser.id),
};
case 'DELETE_CONTACT':
Contact.delete(action.data);
return {
...state,
contacts: Contact.getByUser(state.currentUser.id),
};
case 'SEND_BROCHURE':
SendBrochure.add(action.data);
return state;
default:
return state;
}
}
export default reducer;