Add state updates after screen changes

This commit is contained in:
Paul Frazee 2022-07-25 14:21:48 -05:00
parent 7f04ac172e
commit 3794eca88e
7 changed files with 171 additions and 52 deletions

View file

@ -11,19 +11,32 @@ export const Profile = ({
route,
}: RootTabsScreenProps<'Profile'>) => {
const store = useStores()
const [hasSetup, setHasSetup] = useState<string>('')
const [feedView, setFeedView] = useState<FeedViewModel | undefined>()
useEffect(() => {
if (feedView?.params.author === route.params.name) {
console.log('Profile feed view')
const author = route.params.name
if (feedView?.params.author === author) {
return // no change needed? or trigger refresh?
}
console.log('Fetching profile feed view', route.params.name)
const newFeedView = new FeedViewModel(store, {author: route.params.name})
console.log('Fetching profile feed', author)
const newFeedView = new FeedViewModel(store, {author})
setFeedView(newFeedView)
newFeedView.setup().catch(err => console.error('Failed to fetch feed', err))
newFeedView
.setup()
.catch(err => console.error('Failed to fetch feed', err))
.then(() => setHasSetup(author))
}, [route.params.name, feedView?.params.author, store])
useEffect(() => {
return navigation.addListener('focus', () => {
if (hasSetup === feedView?.params.author) {
console.log('Updating profile feed', hasSetup)
feedView?.update()
}
})
}, [navigation, feedView, hasSetup])
const onNavigateContent = (screen: string, props: Record<string, string>) => {
// @ts-ignore it's up to the callers to supply correct params -prf
navigation.push(screen, props)

View file

@ -1,4 +1,4 @@
import React, {useEffect, useLayoutEffect} from 'react'
import React, {useState, useEffect, useLayoutEffect} from 'react'
import {Image, StyleSheet, TouchableOpacity, View} from 'react-native'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {Shell} from '../../shell'
@ -8,10 +8,11 @@ import {useStores} from '../../../state'
import {AVIS} from '../../lib/assets'
export function Home({navigation}: RootTabsScreenProps<'HomeTab'>) {
const [hasSetup, setHasSetup] = useState<boolean>(false)
const store = useStores()
useEffect(() => {
console.log('Fetching home feed')
store.homeFeed.setup()
store.homeFeed.setup().then(() => setHasSetup(true))
}, [store.homeFeed])
const onNavigateContent = (screen: string, props: Record<string, string>) => {
@ -19,6 +20,15 @@ export function Home({navigation}: RootTabsScreenProps<'HomeTab'>) {
navigation.navigate(screen, props)
}
useEffect(() => {
return navigation.addListener('focus', () => {
if (hasSetup) {
console.log('Updating home feed')
store.homeFeed.update()
}
})
}, [navigation, store.homeFeed, hasSetup])
useLayoutEffect(() => {
navigation.setOptions({
headerShown: true,