Ensure all feed views can refresh

zio/stable
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>
)
})

View File

@ -4,11 +4,12 @@ import {View, FlatList, StyleProp, ViewStyle} from 'react-native'
import {PostFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
import {EmptyState} from '../util/EmptyState'
import {ErrorMessage} from '../util/ErrorMessage'
import {FeedModel, FeedItemModel} from '../../../state/models/feed-view'
import {FeedModel} from '../../../state/models/feed-view'
import {FeedItem} from './FeedItem'
import {ComposePrompt} from '../composer/Prompt'
const COMPOSE_PROMPT_ITEM = {_reactKey: '__prompt__'}
const EMPTY_FEED_ITEM = {_reactKey: '__empty__'}
export const Feed = observer(function Feed({
feed,
@ -27,9 +28,17 @@ 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: FeedItemModel}) => {
const renderItem = ({item}: {item: any}) => {
if (item === COMPOSE_PROMPT_ITEM) {
return <ComposePrompt onPressCompose={onPressCompose} />
} else if (item === EMPTY_FEED_ITEM) {
return (
<EmptyState
icon="bars"
message="This feed is empty!"
style={{paddingVertical: 40}}
/>
)
} else {
return <FeedItem item={item} />
}
@ -40,11 +49,18 @@ export const Feed = observer(function Feed({
const onEndReached = () => {
feed.loadMore().catch(err => console.error('Failed to load more', err))
}
let data
if (feed.hasLoaded) {
if (feed.isEmpty) {
data = [COMPOSE_PROMPT_ITEM, EMPTY_FEED_ITEM]
} else {
data = [COMPOSE_PROMPT_ITEM].concat(feed.feed)
}
}
return (
<View style={style}>
{feed.isLoading && !feed.isRefreshing && !feed.hasContent && (
<PostFeedLoadingPlaceholder />
)}
{!data && <ComposePrompt onPressCompose={onPressCompose} />}
{feed.isLoading && !data && <PostFeedLoadingPlaceholder />}
{feed.hasError && (
<ErrorMessage
dark
@ -53,10 +69,10 @@ export const Feed = observer(function Feed({
onPressTryAgain={onPressTryAgain}
/>
)}
{feed.hasContent && (
{feed.hasLoaded && data && (
<FlatList
ref={scrollElRef}
data={[COMPOSE_PROMPT_ITEM].concat(feed.feed.slice())}
data={data}
keyExtractor={item => item._reactKey}
renderItem={renderItem}
refreshing={feed.isRefreshing}
@ -65,12 +81,6 @@ export const Feed = observer(function Feed({
onEndReached={onEndReached}
/>
)}
{feed.isEmpty && (
<View>
<ComposePrompt onPressCompose={onPressCompose} />
<EmptyState icon="bars" message="This feed is empty!" />
</View>
)}
</View>
)
})