update current user data on drawer open

This commit is contained in:
Nouman Tahir 2023-09-22 19:05:33 +05:00
parent 16545d8678
commit 1167022a2f

View File

@ -1,4 +1,5 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Alert } from 'react-native'
import { useDispatch, useSelector } from 'react-redux';
// Actions
@ -7,9 +8,16 @@ import { setInitPosts, setFeedPosts } from '../../../redux/actions/postsAction';
// Component
import SideMenuView from '../view/sideMenuView';
import { useDrawerStatus } from '@react-navigation/drawer';
import { updateCurrentAccount } from '../../../redux/actions/accountAction';
import { getUser } from '../../../providers/hive/dhive';
import bugsnapInstance from '../../../config/bugsnag';
const SideMenuContainer = ({ navigation }) => {
const dispatch = useDispatch();
const drawerStatus = useDrawerStatus();
const isLoggedIn = useSelector((state) => state.application.isLoggedIn);
const currentAccount = useSelector((state) => state.account.currentAccount);
@ -17,6 +25,34 @@ const SideMenuContainer = ({ navigation }) => {
(state) => state.ui.isVisibleAccountsBottomSheet,
);
useEffect(()=>{
if(drawerStatus === 'open'){
//update profile on drawer open
_updateUserData();
}
}, [drawerStatus])
//fetches and update user data
const _updateUserData = async () => {
try{
if(currentAccount?.username){
let accountData = await getUser(currentAccount.username);
if(accountData){
dispatch(updateCurrentAccount({...currentAccount, ...accountData}))
}
}
} catch(err){
console.warn("failed to update user data")
bugsnapInstance.notify(err);
}
}
const _navigateToRoute = (route = null) => {
if (route) {
navigation.navigate(route);