Skip to content

Commit ea1f6cd

Browse files
committed
refactor: needlogin for nodedetail
1 parent 5c03288 commit ea1f6cd

File tree

6 files changed

+70
-22
lines changed

6 files changed

+70
-22
lines changed

src/components/common/Placeholder.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const styles = {
5353
justifyContent: 'center',
5454
alignItems: 'center',
5555
flexDirection: 'column',
56-
marginVertical: _theme.spacing.small,
56+
paddingVertical: _theme.spacing.small,
5757
marginBottom: _theme.spacing.large
5858
}),
5959
iconStyle: (_theme: ITheme): ViewStyle => ({}),
File renamed without changes.

src/hooks/useSession.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Created by leon<[email protected]> on 22/4/20.
3+
*/
4+
import { RootState } from '@src/store'
5+
import { useAppSelector } from '.'
6+
7+
export const useSession = () => {
8+
const member = useAppSelector((_state: RootState) => _state.member)
9+
10+
return {
11+
logined: member.token,
12+
profile: member.profile,
13+
token: member.token
14+
}
15+
}

src/screens/components/common/General.tsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs'
55
import { Placeholder, Text } from '@src/components'
66
import { useAppSelector } from '@src/hooks'
7+
import { useSession } from '@src/hooks/useSession'
78
import { translate } from '@src/i18n'
89
import { NavigationService, ROUTES } from '@src/navigation'
910
import { RootState } from '@src/store'
@@ -138,19 +139,35 @@ const Footer = () => {
138139
* Need Login
139140
* @returns
140141
*/
141-
const NeedLogin = ({ loginAfterAction, children }: { loginAfterAction?: () => void; children?: React.ReactNode }) => {
142-
const token = useAppSelector((_state: RootState) => _state.member.token)
142+
const NeedLogin = ({
143+
containerStyle,
144+
placeholderBackground,
145+
mustLogin = true,
146+
onMount,
147+
children
148+
}: {
149+
/**
150+
* container style
151+
*/
152+
containerStyle?: StyleProp<ViewStyle>
153+
placeholderBackground?: string
154+
mustLogin?: boolean
155+
onMount?: () => void
156+
children?: React.ReactNode
157+
}) => {
158+
const { logined } = useSession()
143159

144160
useEffect(() => {
145-
if (token) {
146-
loginAfterAction && loginAfterAction()
161+
if (logined) {
162+
onMount && onMount()
147163
}
148-
}, [token])
164+
}, [logined])
149165

150166
return (
151-
<View style={{ flex: 1 }}>
152-
{!token ? (
167+
<View style={[{ flex: 1 }, containerStyle]}>
168+
{!logined && mustLogin ? (
153169
<Placeholder
170+
containerStyle={[{ backgroundColor: placeholderBackground }]}
154171
displayType="text"
155172
placeholderText={translate('placeholder.needToLogin')}
156173
buttonText={translate('label.goLogin')}

src/screens/components/topic/FetchTopicCardList.tsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
*/
44

55
import { useToast } from '@src/components'
6+
import { useMember } from '@src/hooks/useMember'
7+
import { useSession } from '@src/hooks/useSession'
68
import { NODE_TABS } from '@src/navigation'
9+
import { useTheme } from '@src/theme'
710
import { V2exObject } from '@src/types'
811
import { v2exLib } from '@src/v2ex'
912
import React, { useCallback, useEffect, useMemo, useState } from 'react'
1013
import { RefreshControl, StyleProp, ViewStyle } from 'react-native'
14+
import { NeedLogin } from '../common'
1115
import TopicCardList from './TopicCardList'
1216

1317
export interface FetchTopicCardListProps {
@@ -30,6 +34,8 @@ const FetchTopicCardList: React.FC<FetchTopicCardListProps> = ({
3034
containerStyle,
3135
displayStyle
3236
}: FetchTopicCardListProps) => {
37+
const { theme } = useTheme()
38+
const { logined } = useSession()
3339
const { showMessage } = useToast()
3440
const [page, setPage] = useState(1)
3541
const [mounted, setMounted] = useState<boolean>(false)
@@ -45,7 +51,10 @@ const FetchTopicCardList: React.FC<FetchTopicCardListProps> = ({
4551

4652
const fetchTopics = useCallback(
4753
(pageNum: number) => {
48-
console.log('page number', pageNum)
54+
if (v2API && !logined) {
55+
return
56+
}
57+
4958
if (pageNum > 1 && (!v2API || specialNode)) {
5059
setHasMore(false)
5160
return
@@ -61,7 +70,7 @@ const FetchTopicCardList: React.FC<FetchTopicCardListProps> = ({
6170
? nodeName === NODE_TABS.LATEST
6271
? v2exLib.topic.latestTopics()
6372
: v2exLib.topic.hotTopics()
64-
: v2API
73+
: v2API && logined
6574
? v2exLib.topic.pager(nodeName, pageNum)
6675
: v2exLib.topic.topics(nodeName, 'node_name')
6776
)
@@ -79,7 +88,7 @@ const FetchTopicCardList: React.FC<FetchTopicCardListProps> = ({
7988
showMessage(err.message)
8089
})
8190
},
82-
[nodeName, showMessage, page]
91+
[nodeName, showMessage, page, v2API, logined]
8392
) // eslint-disable-line react-hooks/exhaustive-deps
8493

8594
const onRefresh = () => {
@@ -108,16 +117,23 @@ const FetchTopicCardList: React.FC<FetchTopicCardListProps> = ({
108117
}
109118

110119
return (
111-
<TopicCardList
112-
containerStyle={containerStyle}
113-
topics={list}
114-
displayStyle={displayStyle}
115-
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}
116-
onEndReached={onReached}
117-
canLoadMoreContent={hasMore}
118-
searchIndicator={false}
119-
refreshCallback={onRefresh}
120-
/>
120+
<NeedLogin
121+
onMount={() => {
122+
fetchTopics(1)
123+
}}
124+
mustLogin={v2API}
125+
placeholderBackground={theme.colors.surface}>
126+
<TopicCardList
127+
containerStyle={containerStyle}
128+
topics={list}
129+
displayStyle={displayStyle}
130+
refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />}
131+
onEndReached={onReached}
132+
canLoadMoreContent={hasMore}
133+
searchIndicator={false}
134+
refreshCallback={onRefresh}
135+
/>
136+
</NeedLogin>
121137
)
122138
}
123139

src/screens/my/Topics.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const MyTopics = ({
3535
}, [])
3636

3737
return (
38-
<NeedLogin loginAfterAction={onRefresh}>
38+
<NeedLogin onMount={onRefresh}>
3939
<TopicCardList
4040
topics={list}
4141
displayStyle={'full'}

0 commit comments

Comments
 (0)