Ensure all feed views can refresh

This commit is contained in:
Paul Frazee 2022-12-06 13:04:40 -06:00
parent 588aface7c
commit e042bd33ef
2 changed files with 49 additions and 28 deletions

View file

@ -1,15 +1,14 @@
import React from 'react'
import {observer} from 'mobx-react-lite'
import {View, FlatList} from 'react-native'
import {
NotificationsViewModel,
NotificationsViewItemModel,
} from '../../../state/models/notifications-view'
import {NotificationsViewModel} from '../../../state/models/notifications-view'
import {FeedItem} from './FeedItem'
import {NotificationFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
import {ErrorMessage} from '../util/ErrorMessage'
import {EmptyState} from '../util/EmptyState'
const EMPTY_FEED_ITEM = {_reactKey: '__empty__'}
export const Feed = observer(function Feed({
view,
onPressTryAgain,
@ -21,20 +20,35 @@ export const Feed = observer(function Feed({
// VirtualizedList: You have a large list that is slow to update - make sure your
// renderItem function renders components that follow React performance best practices
// like PureComponent, shouldComponentUpdate, etc
const renderItem = ({item}: {item: NotificationsViewItemModel}) => (
<FeedItem item={item} />
)
const renderItem = ({item}: {item: any}) => {
if (item === EMPTY_FEED_ITEM) {
return (
<EmptyState
icon="bell"
message="No notifications yet!"
style={{paddingVertical: 40}}
/>
)
}
return <FeedItem item={item} />
}
const onRefresh = () => {
view.refresh().catch(err => console.error('Failed to refresh', err))
}
const onEndReached = () => {
view.loadMore().catch(err => console.error('Failed to load more', err))
}
let data
if (view.hasLoaded) {
if (view.isEmpty) {
data = [EMPTY_FEED_ITEM]
} else {
data = view.notifications
}
}
return (
<View style={{flex: 1}}>
{view.isLoading && !view.isRefreshing && !view.hasContent && (
<NotificationFeedLoadingPlaceholder />
)}
{view.isLoading && !data && <NotificationFeedLoadingPlaceholder />}
{view.hasError && (
<ErrorMessage
dark
@ -43,9 +57,9 @@ export const Feed = observer(function Feed({
onPressTryAgain={onPressTryAgain}
/>
)}
{view.hasLoaded && (
{data && (
<FlatList
data={view.notifications}
data={data}
keyExtractor={item => item._reactKey}
renderItem={renderItem}
refreshing={view.isRefreshing}
@ -53,9 +67,6 @@ export const Feed = observer(function Feed({
onEndReached={onEndReached}
/>
)}
{view.hasLoaded && view.isEmpty && (
<EmptyState icon="bell" message="No notifications yet!" />
)}
</View>
)
})