Created
November 1, 2018 22:27
-
-
Save atdrago/2c68b4d016e0ec8713923de6429e520d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| CLUB_FAILURE, | |
| CLUB_REQUEST, | |
| CLUB_SUCCESS, | |
| CLUB_WITH_MEMBERS_FAILURE, | |
| CLUB_WITH_MEMBERS_REQUEST, | |
| CLUB_WITH_MEMBERS_SUCCESS, | |
| CLUBS_REQUEST, | |
| CLUBS_SUCCESS, | |
| CLUBS_FAILURE, | |
| DEAUTH_USER, | |
| HIDE_CLUB_DETAILS_MODAL, | |
| HIDE_CLUB_TIP, | |
| INVALIDATE_CLUBS, | |
| SHOW_CLUB_DETAILS_MODAL | |
| } from '../actions'; | |
| const defaultItems = [ | |
| { isClubTip: true } | |
| ]; | |
| const defaultClubsState = { | |
| didInvalidate: false, | |
| isClubDetailsModalVisible: false, | |
| isClubTipVisible: true, | |
| isFetching: false, | |
| items: defaultItems, | |
| shouldShowRefreshControlWhileFetching: false | |
| }; | |
| export default function clubs(state = defaultClubsState, action) { | |
| switch (action.type) { | |
| case CLUBS_REQUEST: { | |
| const { shouldShowRefreshControlWhileFetching } = action; | |
| return { | |
| ...state, | |
| isFetching: true, | |
| didInvalidate: false, | |
| shouldShowRefreshControlWhileFetching | |
| }; | |
| } | |
| case CLUBS_SUCCESS: | |
| return { | |
| ...state, | |
| didInvalidate: false, | |
| fetchedAt: Date.now(), | |
| isFetching: false, | |
| items: [ | |
| ...defaultItems, | |
| ...action.clubs | |
| ], | |
| shouldShowRefreshControlWhileFetching: false | |
| }; | |
| case CLUBS_FAILURE: | |
| return { | |
| ...state, | |
| isFetching: false, | |
| didInvalidate: true, | |
| shouldShowRefreshControlWhileFetching: false | |
| }; | |
| case INVALIDATE_CLUBS: | |
| return { | |
| ...state, | |
| didInvalidate: true | |
| }; | |
| case SHOW_CLUB_DETAILS_MODAL: | |
| return { | |
| ...state, | |
| isClubDetailsModalVisible: true | |
| }; | |
| case HIDE_CLUB_DETAILS_MODAL: | |
| return { | |
| ...state, | |
| isClubDetailsModalVisible: false | |
| }; | |
| case CLUB_FAILURE: | |
| case CLUB_WITH_MEMBERS_FAILURE: | |
| return { | |
| ...state, | |
| items: state.items.map(club => { | |
| if (club.id === action.clubId) { | |
| return { | |
| ...club, | |
| didInvalidate: true, | |
| isFetching: false, | |
| shouldShowRefreshControlWhileFetching: false | |
| }; | |
| } | |
| return club; | |
| }) | |
| }; | |
| case CLUB_REQUEST: | |
| case CLUB_WITH_MEMBERS_REQUEST: | |
| return { | |
| ...state, | |
| items: state.items.map(club => { | |
| if (club.id === action.clubId) { | |
| return { | |
| ...club, | |
| isFetching: true, | |
| didInvalidate: false | |
| }; | |
| } | |
| return club; | |
| }) | |
| }; | |
| case CLUB_SUCCESS: | |
| case CLUB_WITH_MEMBERS_SUCCESS: | |
| return { | |
| ...state, | |
| items: state.items.map(club => { | |
| if (club.id === action.club.id) { | |
| return { | |
| ...action.club, | |
| didInvalidate: false, | |
| fetchedAt: Date.now(), | |
| isFetching: false, | |
| shouldShowRefreshControlWhileFetching: false | |
| }; | |
| } | |
| return club; | |
| }) | |
| }; | |
| case DEAUTH_USER: | |
| return { | |
| ...state, | |
| items: [ | |
| ...state.items.map(item => { | |
| if (item.isClubTip) { | |
| return { ...item }; | |
| } | |
| // Reset user-dependent properties after deauth | |
| return { | |
| ...item, | |
| isUserInLine: false, | |
| isUserMember: false, | |
| userMemberNumber: null, | |
| userPositionInLine: null | |
| }; | |
| }) | |
| ] | |
| }; | |
| case HIDE_CLUB_TIP: | |
| return { | |
| ...state, | |
| isClubTipVisible: false | |
| }; | |
| default: | |
| return state; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment