Skip to content

Commit

Permalink
[Fix] Channel Members' Roles fetching from ChannelStore instead of Us…
Browse files Browse the repository at this point in the history
…erStore (#518)

* useChannelStore for channel member roles

* fixed review changes
  • Loading branch information
thesynthax authored Apr 9, 2024
1 parent f31b805 commit ac9e900
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
8 changes: 4 additions & 4 deletions packages/react-native/src/views/ChatRoomView/ChatRoomView.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChatInput } from '../../components/ChatInput';
import { MessageActionsSheet } from '../../components/MessageActionsSheet';
import { MessageList } from '../../components/MessageList';
import { useRCContext } from '../../contexts/RCInstance';
import { useMessageStore, useUserStore } from '../../store';
import { useMessageStore, useMemberStore, useUserStore } from '../../store';

const styles = StyleSheet.create({
container: {
Expand All @@ -16,7 +16,7 @@ const ChatRoomView = () => {
const { RCInstance, ECOptions } = useRCContext();

const isUserAuthenticated = useUserStore((state) => state.isUserAuthenticated);
const setRoles = useUserStore((state) => state.setRoles);
const setMemberRoles = useMemberStore((state) => state.setMemberRoles);

const setMessages = useMessageStore((state) => state.setMessages);
const upsertMessage = useMessageStore((state) => state.upsertMessage);
Expand Down Expand Up @@ -56,7 +56,7 @@ const ChatRoomView = () => {
Object.assign(obj, { [item.u.username]: item }),
{}
);
setRoles(rolesObj);
setMemberRoles(rolesObj);
}
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -117,4 +117,4 @@ const ChatRoomView = () => {
)
}

export default ChatRoomView;
export default ChatRoomView;
3 changes: 3 additions & 0 deletions packages/react/src/components/EmbeddedChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const EmbeddedChat = ({
);
const setAuthenticatedUserId = useUserStore((state) => state.setUserId);
const setAuthenticatedName = useUserStore((state) => state.setName);
const setAuthenticatedUserRoles = useUserStore((state) => state.setRoles);

useEffect(() => {
RCInstance.auth.onAuthChange((user) => {
Expand All @@ -130,6 +131,7 @@ const EmbeddedChat = ({
setAuthenticatedUserUsername(me.username);
setAuthenticatedUserId(me._id);
setAuthenticatedName(me.name);
setAuthenticatedUserRoles(me.roles);
setIsUserAuthenticated(true);
})
.catch(console.error);
Expand All @@ -143,6 +145,7 @@ const EmbeddedChat = ({
setAuthenticatedUserAvatarUrl,
setAuthenticatedUserId,
setAuthenticatedUserUsername,
setAuthenticatedUserRoles,
setIsUserAuthenticated,
]);

Expand Down
13 changes: 9 additions & 4 deletions packages/react/src/hooks/useFetchChatData.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { useCallback, useContext } from 'react';
import RCContext from '../context/RCInstance';
import { useUserStore, useChannelStore, useMessageStore } from '../store';
import {
useUserStore,
useChannelStore,
useMemberStore,
useMessageStore,
} from '../store';

const useFetchChatData = (showRoles) => {
const { RCInstance, ECOptions } = useContext(RCContext);
const setRoles = useUserStore((state) => state.setRoles);
const setMemberRoles = useMemberStore((state) => state.setMemberRoles);
const isChannelPrivate = useChannelStore((state) => state.isChannelPrivate);
const setMessages = useMessageStore((state) => state.setMessages);
const isUserAuthenticated = useUserStore(
Expand Down Expand Up @@ -53,7 +58,7 @@ const useFetchChatData = (showRoles) => {
)
: {};

setRoles(rolesObj);
setMemberRoles(rolesObj);
}
} catch (e) {
console.error(e);
Expand All @@ -65,7 +70,7 @@ const useFetchChatData = (showRoles) => {
ECOptions?.enableThreads,
showRoles,
setMessages,
setRoles,
setMemberRoles,
isChannelPrivate,
]
);
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/store/memberStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { create } from 'zustand';
const useMemberStore = create((set) => ({
members: [],
showMembers: false,
memberRoles: {},
setMemberRoles: (memberRoles) => set((state) => ({ ...state, memberRoles })),
toggleShowMembers: () =>
set((state) => ({ showMembers: !state.showMembers })),
setMembersHandler: (memberList) => set(() => ({ members: memberList })),
Expand Down

0 comments on commit ac9e900

Please sign in to comment.