diff --git a/src/views/friend-list/FriendListMessageHandler.tsx b/src/views/friend-list/FriendListMessageHandler.tsx index 41101fda..68087f4f 100644 --- a/src/views/friend-list/FriendListMessageHandler.tsx +++ b/src/views/friend-list/FriendListMessageHandler.tsx @@ -1,4 +1,4 @@ -import { GetFriendRequestsComposer, MessengerInitEvent } from 'nitro-renderer'; +import { FriendListFragmentEvent, GetFriendRequestsComposer, MessengerInitEvent } from 'nitro-renderer'; import { FC, useCallback } from 'react'; import { CreateMessageHook, SendMessageHook } from '../../hooks/messages/message-event'; import { useFriendListContext } from './context/FriendListContext'; @@ -28,7 +28,20 @@ export const FriendListMessageHandler: FC = props SendMessageHook(new GetFriendRequestsComposer()); }, [ dispatchFriendListState ]); + const onFriendListFragmentEvent = useCallback((event: FriendListFragmentEvent) => + { + const parser = event.getParser(); + + dispatchFriendListState({ + type: FriendListActions.PROCESS_FRAGMENT, + payload: { + fragment: parser.fragment + } + }); + }, [ dispatchFriendListState ]); + CreateMessageHook(MessengerInitEvent, onMessengerInitEvent); + CreateMessageHook(FriendListFragmentEvent, onFriendListFragmentEvent); return null; } diff --git a/src/views/friend-list/common/MessengerFriend.ts b/src/views/friend-list/common/MessengerFriend.ts new file mode 100644 index 00000000..b7bb904a --- /dev/null +++ b/src/views/friend-list/common/MessengerFriend.ts @@ -0,0 +1,18 @@ +export class MessengerFriend +{ + public id: number = -1; + public name: string = null; + public gender: number = 0; + public online: boolean = false; + public followingAllowed: boolean = false; + public figure: string = null; + public categoryId: number = 0; + public motto: string = null; + public realName: string = null; + public lastAccess: string = null; + public persistedMessageUser: boolean = false; + public vipMember: boolean = false; + public pocketHabboUser: boolean = false; + public relationshipStatus: number = -1; + public unread: number = 0; +} diff --git a/src/views/friend-list/reducers/FriendListReducer.tsx b/src/views/friend-list/reducers/FriendListReducer.tsx index 803fbd84..c9b7f612 100644 --- a/src/views/friend-list/reducers/FriendListReducer.tsx +++ b/src/views/friend-list/reducers/FriendListReducer.tsx @@ -1,16 +1,20 @@ +import { FriendParser } from 'nitro-renderer'; import { Reducer } from 'react'; +import { MessengerFriend } from '../common/MessengerFriend'; import { MessengerSettings } from '../utils/MessengerSettings'; export interface IFriendListState { - settings: MessengerSettings + settings: MessengerSettings; + friends: MessengerFriend[]; } export interface IFriendListAction { type: string; payload: { - settings?: MessengerSettings + settings?: MessengerSettings; + fragment?: FriendParser[] } } @@ -18,10 +22,12 @@ export class FriendListActions { public static RESET_STATE: string = 'FLA_RESET_STATE'; public static UPDATE_SETTINGS: string = 'FLA_UPDATE_SETTINGS'; + public static PROCESS_FRAGMENT: string = 'FLA_PROCESS_FRAGMENT'; } export const initialFriendList: IFriendListState = { - settings: null + settings: null, + friends: [] } export const FriendListReducer: Reducer = (state, action) => @@ -33,6 +39,36 @@ export const FriendListReducer: Reducer = ( return { ...state, settings }; } + case FriendListActions.PROCESS_FRAGMENT: { + const fragment = (action.payload.fragment || null); + let friends = [ ...state.friends ]; + + for(const friend of fragment) + { + const index = friends.findIndex(existingFriend => (existingFriend.id === friend.id)); + const newFriend = new MessengerFriend(); + + newFriend.id = friend.id; + newFriend.name = friend.name; + newFriend.gender = friend.gender; + newFriend.online = friend.online; + newFriend.followingAllowed = friend.followingAllowed; + newFriend.figure = friend.figure; + newFriend.categoryId = friend.categoryId; + newFriend.motto = friend.motto; + newFriend.realName = friend.realName; + newFriend.lastAccess = friend.lastAccess; + newFriend.persistedMessageUser = friend.persistedMessageUser; + newFriend.vipMember = friend.vipMember; + newFriend.pocketHabboUser = friend.pocketHabboUser; + newFriend.relationshipStatus = friend.relationshipStatus; + + if(index > -1) friends[index] = newFriend; + else friends.push(newFriend); + } + + return { ...state, friends }; + } default: return state; }