diff options
| author | gabrhr <[email protected]> | 2022-04-20 10:19:29 -0500 |
|---|---|---|
| committer | gabrhr <[email protected]> | 2022-04-20 10:19:29 -0500 |
| commit | e13e630cd6e4fc0b1ff92098a28a770794c7bb9a (patch) | |
| tree | e68ad2f947d1b3ec454529b35f37ca2f223e5431 /front/odiparpack/app/redux/modules/socialMedia.js | |
| parent | 457816ac1129fcc6019d2fc795b6693ee6776d59 (diff) | |
| download | DP1_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/socialMedia.js')
| -rw-r--r-- | front/odiparpack/app/redux/modules/socialMedia.js | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/front/odiparpack/app/redux/modules/socialMedia.js b/front/odiparpack/app/redux/modules/socialMedia.js new file mode 100644 index 0000000..a775f49 --- /dev/null +++ b/front/odiparpack/app/redux/modules/socialMedia.js @@ -0,0 +1,110 @@ +import { fromJS, List, Map } from 'immutable'; +import notif from 'ba-api/notifMessage'; +import dummy from 'ba-api/dummyContents'; +import { + FETCH_TIMELINE_DATA, + POST, + TOGGLE_LIKE, + FETCH_COMMENT_DATA, + POST_COMMENT, + CLOSE_NOTIF +} from 'ba-actions/actionTypes'; +import { getDate, getTime } from '../helpers/dateTimeHelper'; + +const initialState = { + dataTimeline: List([]), + commentIndex: 0, + notifMsg: '', +}; + +const icon = privacyType => { + switch (privacyType) { + case 'public': + return 'language'; + case 'friends': + return 'people'; + default: + return 'lock'; + } +}; + +const buildTimeline = (text, image, privacy) => { + const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36); + const imageSrc = image !== undefined ? URL.createObjectURL(image[0]) : ''; + return Map({ + id, + name: 'John Doe', + date: getDate(), + time: getTime(), + icon: icon(privacy), + avatar: dummy.user.avatar, + image: imageSrc, + content: text, + liked: false, + comments: List([]) + }); +}; + +const buildComment = (message, curData) => { + const id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36); + const newData = Map({ + id, + from: 'John Doe', + avatar: dummy.user.avatar, + date: getDate(), + message, + }); + return curData.push(newData); +}; + +const initialImmutableState = fromJS(initialState); + +export default function reducer(state = initialImmutableState, action = {}) { + switch (action.type) { + case FETCH_TIMELINE_DATA: + return state.withMutations((mutableState) => { + const items = fromJS(action.items); + mutableState.set('dataTimeline', items); + }); + case POST: + return state.withMutations((mutableState) => { + mutableState + .update( + 'dataTimeline', + dataTimeline => dataTimeline.unshift( + buildTimeline(action.text, action.media, action.privacy) + ) + ) + .set('notifMsg', notif.posted); + }); + case TOGGLE_LIKE: + return state.withMutations((mutableState) => { + const index = state.get('dataTimeline').indexOf(action.item); + mutableState.update('dataTimeline', dataTimeline => dataTimeline + .setIn([index, 'liked'], !state.getIn(['dataTimeline', index, 'liked'])) + ); + }); + case FETCH_COMMENT_DATA: + return state.withMutations((mutableState) => { + const index = state.get('dataTimeline').indexOf(action.item); + mutableState.set('commentIndex', index); + }); + case POST_COMMENT: + return state.withMutations((mutableState) => { + mutableState + .update('dataTimeline', + dataTimeline => dataTimeline.setIn( + [state.get('commentIndex'), 'comments'], + buildComment(action.comment, state.getIn(['dataTimeline', state.get('commentIndex'), 'comments'])) + ) + ) + .set('notifMsg', notif.commented); + }); + case CLOSE_NOTIF: + return state.withMutations((mutableState) => { + mutableState.set('notifMsg', ''); + }); + default: + return state; + } +} |
