Add subscription to purse

This commit is contained in:
Bill 2021-07-03 22:07:29 -04:00
parent 7780eb6592
commit 5ca61ece5e
2 changed files with 32 additions and 4 deletions

View File

@ -1,4 +1,4 @@
import { UserCreditsEvent, UserCurrencyEvent, UserCurrencyUpdateEvent } from 'nitro-renderer'; import { UserCreditsEvent, UserCurrencyEvent, UserCurrencyUpdateEvent, UserSubscriptionEvent } from 'nitro-renderer';
import { FC, useCallback } from 'react'; import { FC, useCallback } from 'react';
import { CreateMessageHook } from '../../hooks/messages/message-event'; import { CreateMessageHook } from '../../hooks/messages/message-event';
import { Currency } from './common/Currency'; import { Currency } from './common/Currency';
@ -48,9 +48,27 @@ export const PurseMessageHandler: FC<PurseMessageHandlerProps> = props =>
}); });
}, [ dispatchPurseState ]); }, [ dispatchPurseState ]);
const onUserSubscriptionEvent = useCallback((event: UserSubscriptionEvent) =>
{
const parser = event.getParser();
switch(parser.name)
{
case 'habbo_club':
dispatchPurseState({
type: PurseActions.SET_CLUB_SUBSCRIPTION,
payload: {
clubSubscription: parser
}
});
return;
}
}, [ dispatchPurseState ]);
CreateMessageHook(UserCreditsEvent, onUserCreditsEvent); CreateMessageHook(UserCreditsEvent, onUserCreditsEvent);
CreateMessageHook(UserCurrencyEvent, onUserCurrencyEvent); CreateMessageHook(UserCurrencyEvent, onUserCurrencyEvent);
CreateMessageHook(UserCurrencyUpdateEvent, onUserCurrencyUpdateEvent); CreateMessageHook(UserCurrencyUpdateEvent, onUserCurrencyUpdateEvent);
CreateMessageHook(UserSubscriptionEvent, onUserSubscriptionEvent);
return null; return null;
} }

View File

@ -1,9 +1,11 @@
import { UserSubscriptionParser } from 'nitro-renderer';
import { Reducer } from 'react'; import { Reducer } from 'react';
import { Currency } from '../common/Currency'; import { Currency } from '../common/Currency';
export interface IPurseState export interface IPurseState
{ {
currencies: Currency[]; currencies: Currency[];
clubSubscription: UserSubscriptionParser;
} }
export interface IPurseAction export interface IPurseAction
@ -12,6 +14,7 @@ export interface IPurseAction
payload: { payload: {
currency?: Currency; currency?: Currency;
currencies?: Currency[]; currencies?: Currency[];
clubSubscription?: UserSubscriptionParser;
} }
} }
@ -19,10 +22,12 @@ export class PurseActions
{ {
public static SET_CURRENCY: string = 'PA_SET_CURRENCY'; public static SET_CURRENCY: string = 'PA_SET_CURRENCY';
public static SET_CURRENCIES: string = 'PA_SET_CURRENCIES'; public static SET_CURRENCIES: string = 'PA_SET_CURRENCIES';
public static SET_CLUB_SUBSCRIPTION: string = 'PA_SET_CLUB_SUBSCRIPTION';
} }
export const initialPurse: IPurseState = { export const initialPurse: IPurseState = {
currencies: [] currencies: [],
clubSubscription: null
} }
export const PurseReducer: Reducer<IPurseState, IPurseAction> = (state, action) => export const PurseReducer: Reducer<IPurseState, IPurseAction> = (state, action) =>
@ -34,7 +39,7 @@ export const PurseReducer: Reducer<IPurseState, IPurseAction> = (state, action)
let didSet = false; let didSet = false;
const currencies = state.currencies.map((existing, index) => const currencies = state.currencies.map(existing =>
{ {
if(existing.type !== updated.type) return existing; if(existing.type !== updated.type) return existing;
@ -50,7 +55,7 @@ export const PurseReducer: Reducer<IPurseState, IPurseAction> = (state, action)
case PurseActions.SET_CURRENCIES: { case PurseActions.SET_CURRENCIES: {
const updated = action.payload.currencies; const updated = action.payload.currencies;
const currencies = state.currencies.filter((existing, index) => const currencies = state.currencies.filter(existing =>
{ {
if(existing.type !== -1) return null; if(existing.type !== -1) return null;
@ -61,6 +66,11 @@ export const PurseReducer: Reducer<IPurseState, IPurseAction> = (state, action)
return { ...state, currencies }; return { ...state, currencies };
} }
case PurseActions.SET_CLUB_SUBSCRIPTION: {
const clubSubscription = action.payload.clubSubscription;
return { ...state, clubSubscription };
}
default: default:
return state; return state;
} }