Skip to content

Commit

Permalink
Use and update following state on detailed event object
Browse files Browse the repository at this point in the history
  • Loading branch information
eikhr committed Feb 10, 2023
1 parent 61a1c03 commit f5605d6
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/actions/EventActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ export function unfollow(
method: 'DELETE',
meta: {
id: followId,
eventId,
errorMessage: 'Avregistering fra interesse feilet',
},
})
Expand Down
1 change: 1 addition & 0 deletions app/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export type Event = EventBase & {
actionGrant: ActionGrant;
activationTime: Dateish | null | undefined;
isAdmitted: boolean | null | undefined;
following: false | ID;
activeCapacity: number;
eventType: EventType;
eventStatusType: EventStatusType;
Expand Down
21 changes: 19 additions & 2 deletions app/reducers/__tests__/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,22 @@ describe('reducers', () => {
const action = {
type: Event.FOLLOW.SUCCESS,
payload: {
target: 1,
id: 3,
entities: {
followerEvents: {
3: {
target: 1,
follower: 2,
id: 3,
},
},
},
result: 3,
},
meta: {
body: {
target: 1,
follower: 2,
},
},
};
expect(events(prevState, action)).toEqual({
Expand All @@ -471,6 +485,7 @@ describe('reducers', () => {
byId: {
1: {
id: 1,
following: 3,
name: 'evt',
},
},
Expand All @@ -485,6 +500,7 @@ describe('reducers', () => {
byId: {
1: {
id: 1,
following: 3,
name: 'evt',
},
},
Expand All @@ -503,6 +519,7 @@ describe('reducers', () => {
byId: {
1: {
id: 1,
following: false,
name: 'evt',
},
},
Expand Down
13 changes: 13 additions & 0 deletions app/reducers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ const mutateEvent = produce((newState: State, action: any): void => {

break;

case Event.FOLLOW.SUCCESS:
if (newState.byId[action.meta.body.target]) {
newState.byId[action.meta.body.target].following =
action.payload.result;
}
break;

case Event.UNFOLLOW.SUCCESS:
if (newState.byId[action.meta.eventId]) {
newState.byId[action.meta.eventId].following = false;
}
break;

default:
break;
}
Expand Down
13 changes: 1 addition & 12 deletions app/routes/events/EventDetailRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
selectWaitingRegistrationsForEvent,
selectRegistrationForEventByUserId,
} from 'app/reducers/events';
import { selectFollowersCurrentUser } from 'app/reducers/followers';
import { selectPenaltyByUserId } from 'app/reducers/penalties';
import { selectUserWithGroups } from 'app/reducers/users';
import helmet from 'app/utils/helmet';
Expand Down Expand Up @@ -53,10 +52,6 @@ const mapStateToProps = (state, props) => {
userId: user.id,
})
: [];
const currentUserFollowing = selectFollowersCurrentUser(state, {
target: eventId,
type: 'event',
});

if (!hasFullAccess) {
const normalPools = event.isMerged
Expand All @@ -81,7 +76,6 @@ const mapStateToProps = (state, props) => {
eventId,
pools,
comments: [],
currentUserFollowing,
};
}

Expand Down Expand Up @@ -143,7 +137,6 @@ const mapStateToProps = (state, props) => {
pendingRegistration,
hasSimpleWaitingList,
penalties,
currentUserFollowing,
};
};

Expand Down Expand Up @@ -211,11 +204,7 @@ const propertyGenerator = (props, config) => {
export default compose(
withPreparedDispatch(
'fetchEventDetail',
(props, dispatch) =>
Promise.all([
dispatch(fetchEvent(props.match.params.eventId)),
props.loggedIn && dispatch(isUserFollowing(props.match.params.eventId)),
]),
(props, dispatch) => dispatch(fetchEvent(props.match.params.eventId)),
(props) => [props.match.params.eventId]
),
connect(mapStateToProps, mapDispatchToProps),
Expand Down
10 changes: 3 additions & 7 deletions app/routes/events/components/EventDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ type Props = {
) => Promise<any>;
deleteEvent: (eventId: ID) => Promise<any>;
deleteComment: (id: ID, contentTarget: string) => Promise<any>;
currentUserFollowing: FollowerItem | null | undefined;
};
type State = {
mapIsOpen: boolean;
Expand Down Expand Up @@ -200,7 +199,6 @@ export default class EventDetail extends Component<Props, State> {
follow,
unfollow,
deleteComment,
currentUserFollowing,
} = this.props;

if (!event.id) {
Expand All @@ -213,8 +211,8 @@ export default class EventDetail extends Component<Props, State> {

const color = colorForEvent(event.eventType);

const onRegisterClick = currentUserFollowing
? () => unfollow(currentUserFollowing.id, event.id)
const onRegisterClick = event.following
? () => unfollow(event.following, event.id)
: () => follow(currentUser.id, event.id);

const currentMoment = moment();
Expand Down Expand Up @@ -370,9 +368,7 @@ export default class EventDetail extends Component<Props, State> {
className={styles.title}
event={event}
>
{loggedIn && (
<InterestedButton isInterested={!!currentUserFollowing} />
)}
{loggedIn && <InterestedButton isInterested={!!event.following} />}
{event.title}
</ContentHeader>

Expand Down
2 changes: 2 additions & 0 deletions app/store/models/Event.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ interface Event {
price: number;
activationTime: Dateish;
isAdmitted: boolean;
following: false | ID;
spotsLeft: number;
pendingRegistration: boolean;
photoConsents: ID[];
Expand Down Expand Up @@ -174,6 +175,7 @@ export type UserDetailedEvent = Pick<
| 'price'
| 'activationTime'
| 'isAdmitted'
| 'following'
| 'spotsLeft'
| 'pendingRegistration'
| 'photoConsents'
Expand Down

0 comments on commit f5605d6

Please sign in to comment.