From 0d35a86298389ceddec196843f7f94ffb6d5ac2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eik=20Hvattum=20R=C3=B8geberg?= Date: Wed, 18 Sep 2024 15:18:22 +0200 Subject: [PATCH] Fix types in event reducer cases --- app/reducers/__tests__/events.spec.ts | 3 +++ app/reducers/events.ts | 34 +++++++++++++++++---------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/reducers/__tests__/events.spec.ts b/app/reducers/__tests__/events.spec.ts index 6a0fa4139d..49e00247c2 100644 --- a/app/reducers/__tests__/events.spec.ts +++ b/app/reducers/__tests__/events.spec.ts @@ -139,6 +139,7 @@ describe('reducers', () => { registrationCount: 0, waitingRegistrationCount: 1, waitingRegistrations: [99], + following: 14, } as unknown as UnknownEvent, }, }; @@ -181,6 +182,7 @@ describe('reducers', () => { registrationCount: 0, waitingRegistrationCount: 1, waitingRegistrations: [99], + following: 14, } as unknown as UnknownEvent, }, }; @@ -223,6 +225,7 @@ describe('reducers', () => { registrationCount: 3, waitingRegistrationCount: 0, waitingRegistrations: [], + following: 14, } as unknown as UnknownEvent, }, }; diff --git a/app/reducers/events.ts b/app/reducers/events.ts index 8fe650ed27..c5efb5ffca 100644 --- a/app/reducers/events.ts +++ b/app/reducers/events.ts @@ -56,21 +56,27 @@ 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++; @@ -78,10 +84,6 @@ const eventsSlice = createSlice({ stateEvent.registrationCount = registrationCount; stateEvent.waitingRegistrationCount = waitingRegistrationCount; - - if (waitingRegistrations) { - stateEvent.waitingRegistrations = waitingRegistrations; - } }); addCase( Event.SOCKET_UNREGISTRATION.SUCCESS, @@ -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) { @@ -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) => {