Skip to content

Commit

Permalink
Fix types in event reducer cases
Browse files Browse the repository at this point in the history
  • Loading branch information
eikhr committed Sep 23, 2024
1 parent 1351134 commit c00ec5c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions app/reducers/__tests__/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe('reducers', () => {
registrationCount: 0,
waitingRegistrationCount: 1,
waitingRegistrations: [99],
following: 14,
} as unknown as UnknownEvent,
},
};
Expand Down Expand Up @@ -181,6 +182,7 @@ describe('reducers', () => {
registrationCount: 0,
waitingRegistrationCount: 1,
waitingRegistrations: [99],
following: 14,
} as unknown as UnknownEvent,
},
};
Expand Down Expand Up @@ -223,6 +225,7 @@ describe('reducers', () => {
registrationCount: 3,
waitingRegistrationCount: 0,
waitingRegistrations: [],
following: 14,
} as unknown as UnknownEvent,
},
};
Expand Down
34 changes: 22 additions & 12 deletions app/reducers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,34 @@ const eventsSlice = createSlice({
addCase(Event.SOCKET_REGISTRATION.SUCCESS, (state, action: AnyAction) => {
const eventId = action.meta.eventId;
const registration = action.payload;
const stateEvent = state.entities[eventId] as DetailedEvent;
const stateEvent = state.entities[eventId] as
| DetailedEvent
| UserDetailedEvent
| AuthUserDetailedEvent;

if (!stateEvent) {
return;
}

let registrationCount = stateEvent.registrationCount;
let waitingRegistrations = stateEvent.waitingRegistrations;
const hasRegistrationAccess = 'waitingRegistrations' in stateEvent;
let waitingRegistrationCount = stateEvent.waitingRegistrationCount ?? 0;

if (!registration.pool) {
waitingRegistrationCount = waitingRegistrationCount + 1;

if (waitingRegistrations) {
waitingRegistrations = [...waitingRegistrations, registration.id];
if (hasRegistrationAccess) {
stateEvent.waitingRegistrations = [
...(stateEvent.waitingRegistrations ?? []),
registration.id,
];
}
} else {
registrationCount++;
}

stateEvent.registrationCount = registrationCount;
stateEvent.waitingRegistrationCount = waitingRegistrationCount;

if (waitingRegistrations) {
stateEvent.waitingRegistrations = waitingRegistrations;
}
});
addCase(
Event.SOCKET_UNREGISTRATION.SUCCESS,
Expand All @@ -92,7 +94,10 @@ const eventsSlice = createSlice({
fromPool,
currentUser,
} = action.meta;
const stateEvent = state.entities[eventId] as DetailedEvent;
const stateEvent = state.entities[eventId] as
| DetailedEvent
| UserDetailedEvent
| AuthUserDetailedEvent;
const registration = action.payload;

if (!stateEvent) {
Expand All @@ -109,18 +114,23 @@ const eventsSlice = createSlice({

if (fromPool) {
stateEvent.registrationCount--;
} else {
} else if (stateEvent.waitingRegistrationCount !== undefined) {
stateEvent.waitingRegistrationCount--;
}

if (stateEvent.waitingRegistrations) {
if (
'waitingRegistrations' in stateEvent &&
stateEvent.waitingRegistrations
) {
stateEvent.waitingRegistrations =
stateEvent.waitingRegistrations.filter(
(id) => id !== action.payload.id,
);
}

stateEvent.following = false;
if ('following' in stateEvent) {
stateEvent.following = false;
}
},
);
addCase(Event.FOLLOW.SUCCESS, (state, action: AnyAction) => {
Expand Down

0 comments on commit c00ec5c

Please sign in to comment.