summaryrefslogtreecommitdiffstats
path: root/front/odiparpack/app/redux/modules/contact.js
diff options
context:
space:
mode:
authorgabrhr <[email protected]>2022-04-20 10:19:29 -0500
committergabrhr <[email protected]>2022-04-20 10:19:29 -0500
commite13e630cd6e4fc0b1ff92098a28a770794c7bb9a (patch)
treee68ad2f947d1b3ec454529b35f37ca2f223e5431 /front/odiparpack/app/redux/modules/contact.js
parent457816ac1129fcc6019d2fc795b6693ee6776d59 (diff)
downloadDP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.tar.gz
DP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.tar.bz2
DP1_project-e13e630cd6e4fc0b1ff92098a28a770794c7bb9a.zip
AƱadir plantilla
Base para front
Diffstat (limited to 'front/odiparpack/app/redux/modules/contact.js')
-rw-r--r--front/odiparpack/app/redux/modules/contact.js131
1 files changed, 131 insertions, 0 deletions
diff --git a/front/odiparpack/app/redux/modules/contact.js b/front/odiparpack/app/redux/modules/contact.js
new file mode 100644
index 0000000..bf34a8b
--- /dev/null
+++ b/front/odiparpack/app/redux/modules/contact.js
@@ -0,0 +1,131 @@
+import { fromJS, List, Map } from 'immutable';
+import notif from 'ba-api/notifMessage';
+import {
+ FETCH_CONTACT_DATA,
+ SEARCH_CONTACT,
+ SHOW_DETAIL_CONTACT,
+ HIDE_DETAIL,
+ EDIT_CONTACT,
+ SUBMIT_CONTACT,
+ DELETE_CONTACT,
+ TOGGLE_FAVORITE,
+ ADD_CONTACT,
+ CLOSE_CONTACT_FORM,
+ CLOSE_NOTIF
+} from 'ba-actions/actionTypes';
+
+const initialState = {
+ contactList: List([]),
+ formValues: Map(),
+ selectedIndex: 0,
+ selectedId: '',
+ keywordValue: '',
+ avatarInit: '',
+ openFrm: false,
+ showMobileDetail: false,
+ notifMsg: '',
+};
+let editingIndex = 0;
+
+const initialImmutableState = fromJS(initialState);
+
+export default function reducer(state = initialImmutableState, action = {}) {
+ switch (action.type) {
+ case FETCH_CONTACT_DATA:
+ return state.withMutations((mutableState) => {
+ const items = fromJS(action.items);
+ mutableState.set('contactList', items);
+ });
+ case SEARCH_CONTACT:
+ return state.withMutations((mutableState) => {
+ action.keyword.persist();
+ const keyword = action.keyword.target.value.toLowerCase();
+ mutableState.set('keywordValue', keyword);
+ });
+ case ADD_CONTACT:
+ return state.withMutations((mutableState) => {
+ mutableState
+ .set('openFrm', true)
+ .set('formValues', Map())
+ .set('avatarInit', '');
+ });
+ case CLOSE_CONTACT_FORM:
+ return state.withMutations((mutableState) => {
+ mutableState
+ .set('openFrm', false)
+ .set('formValues', Map())
+ .set('avatarInit', '')
+ .set('notifMsg', notif.discard);
+ });
+ case EDIT_CONTACT:
+ return state.withMutations((mutableState) => {
+ editingIndex = state.get('contactList').indexOf(action.item);
+ mutableState
+ .set('openFrm', true)
+ .set('selectedId', action.item.get('id'))
+ .set('formValues', action.item)
+ .set('avatarInit', action.item.get('avatar'));
+ });
+ case SUBMIT_CONTACT:
+ return state.withMutations((mutableState) => {
+ const initItem = Map(action.newData);
+ if (state.get('selectedId') === action.newData.get('id')) {
+ // Update data
+ const avatar = action.avatar !== '' ? action.avatar : state.get('avatarInit');
+ const newItem = initItem.update((initUpdated) => (initUpdated.set('avatar', avatar)));
+ mutableState
+ .update('contactList', contactList => contactList.setIn(
+ [editingIndex], newItem
+ ))
+ .set('notifMsg', notif.updated);
+ } else {
+ // Insert data
+ const avatar = action.avatar !== '' ? action.avatar : '/images/pp_boy.svg';
+ const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
+ const newItem = initItem
+ .update('id', (val = id) => val)
+ .update('avatar', (val = avatar) => val)
+ .update('favorited', (val = false) => val);
+ mutableState
+ .update('contactList', contactList => contactList.unshift(newItem))
+ .set('selectedIndex', 0)
+ .set('notifMsg', notif.saved);
+ }
+ mutableState
+ .set('formValues', null)
+ .set('avatarInit', '')
+ .set('openFrm', false);
+ });
+ case SHOW_DETAIL_CONTACT:
+ return state.withMutations((mutableState) => {
+ const index = state.get('contactList').indexOf(action.item);
+ mutableState
+ .set('selectedIndex', index)
+ .set('showMobileDetail', true);
+ });
+ case HIDE_DETAIL:
+ return state.withMutations((mutableState) => {
+ mutableState.set('showMobileDetail', false);
+ });
+ case DELETE_CONTACT:
+ return state.withMutations((mutableState) => {
+ const index = state.get('contactList').indexOf(action.item);
+ mutableState
+ .update('contactList', contactList => contactList.splice(index, 1))
+ .set('notifMsg', notif.removed);
+ });
+ case TOGGLE_FAVORITE:
+ return state.withMutations((mutableState) => {
+ const index = state.get('contactList').indexOf(action.item);
+ mutableState.update('contactList', contactList => contactList
+ .setIn([index, 'favorited'], !state.getIn(['contactList', index, 'favorited']))
+ );
+ });
+ case CLOSE_NOTIF:
+ return state.withMutations((mutableState) => {
+ mutableState.set('notifMsg', '');
+ });
+ default:
+ return state;
+ }
+}