Friends load

This commit is contained in:
Bill 2021-06-22 23:43:37 -04:00
parent 60465968ea
commit 4c4cb12be9
3 changed files with 71 additions and 4 deletions

View File

@ -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<FriendListMessageHandlerProps> = 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;
}

View File

@ -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;
}

View File

@ -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<IFriendListState, IFriendListAction> = (state, action) =>
@ -33,6 +39,36 @@ export const FriendListReducer: Reducer<IFriendListState, IFriendListAction> = (
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;
}