useUserProfile hook - ability to turn on/off cache

This commit is contained in:
Nuwan 2024-10-08 01:01:46 +05:30
parent b83dfb2114
commit 7ed94b5dc0
7 changed files with 30 additions and 16 deletions

View File

@ -13,7 +13,7 @@ function JKFriendRequestNotification(props) {
const { currentUser } = useAuth();
const dispatch = useDispatch();
const user = useSelector(state => state.people.people.find(person => person.id === source_user_id));
const { photoUrl } = useUserProfile(user); // user is the person who sent the message
const { photoUrl } = useUserProfile({ user: user }); // user is the person who sent the message
const handleClick = async event => {
event.stopPropagation();

View File

@ -9,7 +9,7 @@ const JKGenericNotification = (notification) => {
const {formatted_msg, created_at, source_user_id} = notification;
const user = useSelector(state => state.people.people.find(person => person.id === source_user_id));
const { photoUrl } = useUserProfile(user); // user is the person who sent the message
const { photoUrl } = useUserProfile({ user }); // user is the person who sent the message
return (
<>

View File

@ -11,7 +11,7 @@ function JKTextMessageNotification(props) {
const { source_user, source_user_id, message, created_at } = props.notification;
const { currentUser } = useAuth();
const user = useSelector(state => state.people.people.find(person => person.id === source_user_id));
const { photoUrl } = useUserProfile(user); // user is the person who sent the message
const { photoUrl } = useUserProfile({ user }); // user is the person who sent the message
return (
<>

View File

@ -27,8 +27,8 @@ const JKMessageModal = props => {
const messageTextBox = useRef();
const scrolledToBottom = useRef(false);
const { photoUrl: userPhotoUrl } = useUserProfile(user);
const { photoUrl: currentUserPhotoUrl } = useUserProfile(currentUser);
const { photoUrl: userPhotoUrl } = useUserProfile({ user: user});
const { photoUrl: currentUserPhotoUrl } = useUserProfile({ user: currentUser });
const messages = useSelector(state =>
state.textMessage.messages

View File

@ -29,7 +29,7 @@ function JKLobbyChat() {
const [messagesArrived, setMessagesArrived] = useState(false);
//const [offset, setOffset] = useState(0);
const userProfile = useUserProfile(currentUser);
const userProfile = useUserProfile({ user: currentUser });
const { t } = useTranslation('sessions');

View File

@ -9,7 +9,7 @@ const AppDataContext = React.createContext(null);
export const AppDataProvider = ({ children }) => {
const [appData, setAppData] = React.useState({});
const { currentUser } = useAuth();
const { userProfile, photoUrl } = useUserProfile(currentUser);
const { userProfile, photoUrl } = useUserProfile({ user: currentUser, useCache: false });
React.useEffect(() => {
setAppData({ userProfile, currentUserPhotoUrl: photoUrl });

View File

@ -2,25 +2,39 @@ import { useEffect, useState, useMemo } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { fetchPerson } from '../store/features/peopleSlice';
const useUserProfile = user => {
const useUserProfile = ({ user, useCache = true }) => {
const [userProfile, setUserProfile] = useState(null);
const people = useSelector(state => state.people.people);
const dispatch = useDispatch();
const dispatchFetchPerson = userId => {
dispatch(fetchPerson({ userId }))
.unwrap()
.then(resp => {
setUserProfile(resp);
});
};
useEffect(() => {
if (!user) {
setUserProfile(null);
return;
}
const person = people.find(person => person.id === user.id);
if (person) {
setUserProfile(person);
if (useCache) {
const person = people.find(person => person.id === user.id);
if (person) {
setUserProfile(person);
} else {
// dispatch(fetchPerson({ userId: user.id }))
// .unwrap()
// .then(resp => {
// setUserProfile(resp);
// });
dispatchFetchPerson(user.id);
}
} else {
dispatch(fetchPerson({ userId: user.id }))
.unwrap()
.then(resp => {
setUserProfile(resp);
});
dispatchFetchPerson(user.id);
}
return () => {