Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
feat(lib): useMemberships working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Salet committed Jul 5, 2022
1 parent bfc8545 commit 1dfc382
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 54 deletions.
38 changes: 15 additions & 23 deletions lib/src/hooks/use-memberships.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { useState, useEffect, useMemo, useCallback } from "react";
import { GetMembershipsParametersv2 } from "pubnub";
import { usePubNub } from "pubnub-react";
import merge from "lodash.merge";
import cloneDeep from "lodash.clonedeep";
import {
VSPPubnub,
ChannelEntity,
UserEntity,
FetchMembershipsFromUserParametersUsers,
FetchMembershipsFromUserParameters,
FetchMembershipsFromSpaceParameters,
} from "../types";

export const useMemberships = <
T extends FetchMembershipsFromUserParametersUsers | FetchMembershipsFromSpaceParameters
T extends FetchMembershipsFromUserParameters | FetchMembershipsFromSpaceParameters
>(
options: T
): [
Expand All @@ -37,7 +36,7 @@ export const useMemberships = <
merge({}, JSON.parse(jsonOptions), {
page: { next: page },
include: { totalCount: true },
}) as GetMembershipsParametersv2,
}) as T,
[page, jsonOptions]
);

Expand All @@ -55,7 +54,6 @@ export const useMemberships = <
try {
if (totalCount && entities.length >= totalCount) return;
const response = await pubnub.fetchMemberships(paginatedOptions);
// console.log("response from new hook: ", response);
setEntities((entities) => [...entities, ...response.data.map((m) => m.user || m.space)]);
setTotalCount(response.totalCount);
setPage(response.next);
Expand All @@ -68,33 +66,27 @@ export const useMemberships = <

useEffect(() => {
const listener = {
space: (event) => {
console.log("Received SPACE event: ", event);
},
user: (event) => {
console.log("Received USER event: ", event);
},
membership: (event) => {
console.log("Received MEMBERSHIP event: ", event);
},
objects: (event) => {
console.log("Received OBJECT event: ", event);

const message = event.message;
if (message.type !== "membership") return;

setEntities((entities) => {
let membership;
const entitiesCopy = cloneDeep(entities);
const channel = entitiesCopy.find((u) => u.id === message.data.channel.id);
const currentUuid = paginatedOptions.uuid || pubnub.getUUID();

// Make sure the event is for the same uuid as the hook
if (message.data.uuid.id !== currentUuid) return entitiesCopy;
if (paginatedOptions.spaceId !== undefined) {
membership = entitiesCopy.find((u) => u.id === message.data.uuid.id);
if (message.data.channel.id !== paginatedOptions.spaceId) return entitiesCopy;
} else {
membership = entitiesCopy.find((u) => u.id === message.data.channel.id);
const hookUserId = paginatedOptions.userId || pubnub.getUUID();
if (message.data.uuid.id !== hookUserId) return entitiesCopy;
}

// Set events are not handled since there are no events fired for data updates
// New memberships are not handled in order to conform to filters and pagination
if (channel && message.event === "delete") {
entitiesCopy.splice(entitiesCopy.indexOf(channel), 1);
if (membership && message.event === "delete") {
entitiesCopy.splice(entitiesCopy.indexOf(membership), 1);
}

return entitiesCopy;
Expand All @@ -107,7 +99,7 @@ export const useMemberships = <
return () => {
pubnub.removeListener(listener);
};
}, [pubnub, paginatedOptions.uuid]);
}, [pubnub, paginatedOptions.userId, paginatedOptions.spaceId]);

useEffect(() => {
resetHook();
Expand Down
6 changes: 4 additions & 2 deletions lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ interface FetchMembershipsParametersBase {

export interface FetchMembershipsFromSpaceParameters extends FetchMembershipsParametersBase {
spaceId?: string;
readonly userId?: undefined;
include?:
| {
totalCount?: boolean | undefined;
Expand All @@ -66,8 +67,9 @@ export interface FetchMembershipsFromSpaceParameters extends FetchMembershipsPar
| undefined;
}

export interface FetchMembershipsFromUserParametersUsers extends FetchMembershipsParametersBase {
export interface FetchMembershipsFromUserParameters extends FetchMembershipsParametersBase {
userId?: string;
readonly spaceId?: undefined;
include?:
| {
totalCount?: boolean | undefined;
Expand Down Expand Up @@ -111,7 +113,7 @@ export interface VSPPubnub extends Pubnub {
createSpace: (options: CreateSpaceParameters) => Pubnub.SetChannelMetadataResponse<ObjectCustom>;
createUser: (options: CreateUserParameters) => Pubnub.SetUUIDMetadataResponse<ObjectCustom>;
fetchMemberships: (
options: FetchMembershipsFromSpaceParameters | FetchMembershipsFromUserParametersUsers
options: FetchMembershipsFromSpaceParameters | FetchMembershipsFromUserParameters
) => PagedObjectsResponse<SpaceMembershipObject[] | UserMembershipObject[]>;
}

Expand Down
31 changes: 2 additions & 29 deletions samples/group-chat/src/moderated-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import {
MessageInput,
MessageList,
TypingIndicator,
useChannelMembers,
useChannels,
usePresence,
useUser,
useUserMemberships,
useUsers,
useMemberships,
} from "@pubnub/react-chat-components";
Expand Down Expand Up @@ -59,38 +57,13 @@ export default function ModeratedChat(): JSX.Element {
const [currentUser] = useUser({ userId: uuid });
const [allUsers] = useUsers({ include: { customFields: true } });
const [allChannels] = useChannels({ include: { customFields: true } });
const [joinedChannels, , refetchJoinedChannels] = useUserMemberships({
include: { channelFields: true, customChannelFields: true },
});
const [channelMembers, , refetchChannelMemberships, totalChannelMembers] = useChannelMembers({
channel: currentChannel.id,
include: { customUUIDFields: true },
});

const [joinedChannels2, , refetchJoinedChannels2] = useMemberships({
const [joinedChannels, , refetchJoinedChannels] = useMemberships({
include: { spaceFields: true, customSpaceFields: true },
});
const [channelMembers2, , refetchChannelMemberships2, totalChannelMembers2] = useMemberships({
const [channelMembers, , refetchChannelMemberships, totalChannelMembers] = useMemberships({
spaceId: currentChannel.id,
include: { customUserFields: true },
});

// useEffect(() => {
// console.log("Channel members: ", channelMembers);
// }, [channelMembers]);
// useEffect(() => {
// if (joinedChannels.length) console.log("Joined channels old: ", joinedChannels);
// }, [joinedChannels]);
// useEffect(() => {
// if (joinedChannels2.length) console.log("Joined channels new: ", joinedChannels2);
// }, [joinedChannels2]);
// useEffect(() => {
// if (channelMembers.length) console.log("Channel members old: ", channelMembers);
// }, [channelMembers]);
// useEffect(() => {
// if (channelMembers2.length) console.log("Channel members new: ", channelMembers2);
// }, [channelMembers2]);

const [presenceData] = usePresence({
channels: joinedChannels.length ? joinedChannels.map((c) => c.id) : [currentChannel.id],
});
Expand Down

0 comments on commit 1dfc382

Please sign in to comment.