Ensure all feed views can refresh
parent
588aface7c
commit
e042bd33ef
|
@ -1,15 +1,14 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {observer} from 'mobx-react-lite'
|
import {observer} from 'mobx-react-lite'
|
||||||
import {View, FlatList} from 'react-native'
|
import {View, FlatList} from 'react-native'
|
||||||
import {
|
import {NotificationsViewModel} from '../../../state/models/notifications-view'
|
||||||
NotificationsViewModel,
|
|
||||||
NotificationsViewItemModel,
|
|
||||||
} from '../../../state/models/notifications-view'
|
|
||||||
import {FeedItem} from './FeedItem'
|
import {FeedItem} from './FeedItem'
|
||||||
import {NotificationFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
|
import {NotificationFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
|
||||||
import {ErrorMessage} from '../util/ErrorMessage'
|
import {ErrorMessage} from '../util/ErrorMessage'
|
||||||
import {EmptyState} from '../util/EmptyState'
|
import {EmptyState} from '../util/EmptyState'
|
||||||
|
|
||||||
|
const EMPTY_FEED_ITEM = {_reactKey: '__empty__'}
|
||||||
|
|
||||||
export const Feed = observer(function Feed({
|
export const Feed = observer(function Feed({
|
||||||
view,
|
view,
|
||||||
onPressTryAgain,
|
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
|
// VirtualizedList: You have a large list that is slow to update - make sure your
|
||||||
// renderItem function renders components that follow React performance best practices
|
// renderItem function renders components that follow React performance best practices
|
||||||
// like PureComponent, shouldComponentUpdate, etc
|
// like PureComponent, shouldComponentUpdate, etc
|
||||||
const renderItem = ({item}: {item: NotificationsViewItemModel}) => (
|
const renderItem = ({item}: {item: any}) => {
|
||||||
<FeedItem item={item} />
|
if (item === EMPTY_FEED_ITEM) {
|
||||||
|
return (
|
||||||
|
<EmptyState
|
||||||
|
icon="bell"
|
||||||
|
message="No notifications yet!"
|
||||||
|
style={{paddingVertical: 40}}
|
||||||
|
/>
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
return <FeedItem item={item} />
|
||||||
|
}
|
||||||
const onRefresh = () => {
|
const onRefresh = () => {
|
||||||
view.refresh().catch(err => console.error('Failed to refresh', err))
|
view.refresh().catch(err => console.error('Failed to refresh', err))
|
||||||
}
|
}
|
||||||
const onEndReached = () => {
|
const onEndReached = () => {
|
||||||
view.loadMore().catch(err => console.error('Failed to load more', err))
|
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 (
|
return (
|
||||||
<View style={{flex: 1}}>
|
<View style={{flex: 1}}>
|
||||||
{view.isLoading && !view.isRefreshing && !view.hasContent && (
|
{view.isLoading && !data && <NotificationFeedLoadingPlaceholder />}
|
||||||
<NotificationFeedLoadingPlaceholder />
|
|
||||||
)}
|
|
||||||
{view.hasError && (
|
{view.hasError && (
|
||||||
<ErrorMessage
|
<ErrorMessage
|
||||||
dark
|
dark
|
||||||
|
@ -43,9 +57,9 @@ export const Feed = observer(function Feed({
|
||||||
onPressTryAgain={onPressTryAgain}
|
onPressTryAgain={onPressTryAgain}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{view.hasLoaded && (
|
{data && (
|
||||||
<FlatList
|
<FlatList
|
||||||
data={view.notifications}
|
data={data}
|
||||||
keyExtractor={item => item._reactKey}
|
keyExtractor={item => item._reactKey}
|
||||||
renderItem={renderItem}
|
renderItem={renderItem}
|
||||||
refreshing={view.isRefreshing}
|
refreshing={view.isRefreshing}
|
||||||
|
@ -53,9 +67,6 @@ export const Feed = observer(function Feed({
|
||||||
onEndReached={onEndReached}
|
onEndReached={onEndReached}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{view.hasLoaded && view.isEmpty && (
|
|
||||||
<EmptyState icon="bell" message="No notifications yet!" />
|
|
||||||
)}
|
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,11 +4,12 @@ import {View, FlatList, StyleProp, ViewStyle} from 'react-native'
|
||||||
import {PostFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
|
import {PostFeedLoadingPlaceholder} from '../util/LoadingPlaceholder'
|
||||||
import {EmptyState} from '../util/EmptyState'
|
import {EmptyState} from '../util/EmptyState'
|
||||||
import {ErrorMessage} from '../util/ErrorMessage'
|
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 {FeedItem} from './FeedItem'
|
||||||
import {ComposePrompt} from '../composer/Prompt'
|
import {ComposePrompt} from '../composer/Prompt'
|
||||||
|
|
||||||
const COMPOSE_PROMPT_ITEM = {_reactKey: '__prompt__'}
|
const COMPOSE_PROMPT_ITEM = {_reactKey: '__prompt__'}
|
||||||
|
const EMPTY_FEED_ITEM = {_reactKey: '__empty__'}
|
||||||
|
|
||||||
export const Feed = observer(function Feed({
|
export const Feed = observer(function Feed({
|
||||||
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
|
// VirtualizedList: You have a large list that is slow to update - make sure your
|
||||||
// renderItem function renders components that follow React performance best practices
|
// renderItem function renders components that follow React performance best practices
|
||||||
// like PureComponent, shouldComponentUpdate, etc
|
// like PureComponent, shouldComponentUpdate, etc
|
||||||
const renderItem = ({item}: {item: FeedItemModel}) => {
|
const renderItem = ({item}: {item: any}) => {
|
||||||
if (item === COMPOSE_PROMPT_ITEM) {
|
if (item === COMPOSE_PROMPT_ITEM) {
|
||||||
return <ComposePrompt onPressCompose={onPressCompose} />
|
return <ComposePrompt onPressCompose={onPressCompose} />
|
||||||
|
} else if (item === EMPTY_FEED_ITEM) {
|
||||||
|
return (
|
||||||
|
<EmptyState
|
||||||
|
icon="bars"
|
||||||
|
message="This feed is empty!"
|
||||||
|
style={{paddingVertical: 40}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
return <FeedItem item={item} />
|
return <FeedItem item={item} />
|
||||||
}
|
}
|
||||||
|
@ -40,11 +49,18 @@ export const Feed = observer(function Feed({
|
||||||
const onEndReached = () => {
|
const onEndReached = () => {
|
||||||
feed.loadMore().catch(err => console.error('Failed to load more', err))
|
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 (
|
return (
|
||||||
<View style={style}>
|
<View style={style}>
|
||||||
{feed.isLoading && !feed.isRefreshing && !feed.hasContent && (
|
{!data && <ComposePrompt onPressCompose={onPressCompose} />}
|
||||||
<PostFeedLoadingPlaceholder />
|
{feed.isLoading && !data && <PostFeedLoadingPlaceholder />}
|
||||||
)}
|
|
||||||
{feed.hasError && (
|
{feed.hasError && (
|
||||||
<ErrorMessage
|
<ErrorMessage
|
||||||
dark
|
dark
|
||||||
|
@ -53,10 +69,10 @@ export const Feed = observer(function Feed({
|
||||||
onPressTryAgain={onPressTryAgain}
|
onPressTryAgain={onPressTryAgain}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{feed.hasContent && (
|
{feed.hasLoaded && data && (
|
||||||
<FlatList
|
<FlatList
|
||||||
ref={scrollElRef}
|
ref={scrollElRef}
|
||||||
data={[COMPOSE_PROMPT_ITEM].concat(feed.feed.slice())}
|
data={data}
|
||||||
keyExtractor={item => item._reactKey}
|
keyExtractor={item => item._reactKey}
|
||||||
renderItem={renderItem}
|
renderItem={renderItem}
|
||||||
refreshing={feed.isRefreshing}
|
refreshing={feed.isRefreshing}
|
||||||
|
@ -65,12 +81,6 @@ export const Feed = observer(function Feed({
|
||||||
onEndReached={onEndReached}
|
onEndReached={onEndReached}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{feed.isEmpty && (
|
|
||||||
<View>
|
|
||||||
<ComposePrompt onPressCompose={onPressCompose} />
|
|
||||||
<EmptyState icon="bars" message="This feed is empty!" />
|
|
||||||
</View>
|
|
||||||
)}
|
|
||||||
</View>
|
</View>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue