22 * Created by leon<[email protected] > on 22/2/22. 33 */
44import AsyncStorage from '@react-native-async-storage/async-storage'
5+ import { MEMBER_TOKEN_KEY } from '@src/config/constants'
6+ import { logError } from '@src/helper/logger'
7+ import NavigationService from '@src/navigation/NavigationService'
8+ import { RootState } from '@src/store'
9+ import { v2exLib } from '@src/v2ex'
510import { Dispatch } from 'redux'
611import {
7- MEMBER_PROFILE ,
812 APP_AUTH ,
9- APP_AUTH_LOADING ,
10- APP_LOGOUT ,
1113 APP_AUTH_ERROR ,
14+ APP_AUTH_LOADING ,
1215 APP_AUTH_SUCCESS ,
13- V2exObject ,
14- MEMBER_TOPICS ,
16+ APP_LOGOUT ,
1517 MEMBER_INSEREST_NODE ,
16- MEMBER_UNINTEREST_NODE
18+ MEMBER_PROFILE ,
19+ MEMBER_SATE_SETTING ,
20+ MEMBER_TOPICS ,
21+ MEMBER_UNINTEREST_NODE ,
22+ V2exObject
1723} from '../types'
18- import { v2exLib } from '@src/v2ex'
19- import { logError } from '@src/helper/logger'
20- import NavigationService from '@src/navigation/NavigationService'
21- import { MEMBER_TOKEN_KEY } from '@src/config/constants'
24+ import { cacheMemberInterestNodes } from './CacheAction'
2225
23- export const myProfile = ( ) => async ( dispatch : Dispatch ) => {
26+ export const myProfile = ( ) => async ( dispatch : Dispatch , getState : ( ) => RootState ) => {
2427 const _member = await v2exLib . member . myProfile ( )
2528
2629 dispatch ( {
2730 type : MEMBER_PROFILE ,
2831 payload : _member
2932 } )
33+
34+ dispatch ( {
35+ type : MEMBER_SATE_SETTING ,
36+ payload : {
37+ interestNodes : getState ( ) . cache . membersInterestNodes [ _member . id ] ,
38+ followPeoples : getState ( ) . cache . membersFollowing [ _member . id ]
39+ }
40+ } )
3041}
3142
3243export const getToken = ( ) => async ( dispatch : Dispatch ) => {
@@ -41,15 +52,21 @@ export const setMyTopics = (topics: V2exObject.Topic[]) => ({
4152 payload : topics
4253} )
4354
44- export const interestNode = ( node : V2exObject . Node ) => ( {
45- type : MEMBER_INSEREST_NODE ,
46- payload : node
47- } )
55+ export const interestNode = ( node : V2exObject . Node ) => async ( dispatch : Dispatch , getState : ( ) => RootState ) => {
56+ dispatch ( {
57+ type : MEMBER_INSEREST_NODE ,
58+ payload : node
59+ } )
60+ dispatch ( cacheMemberInterestNodes ( getState ( ) . member . interestNodes ) )
61+ }
4862
49- export const unInterestNode = ( node : V2exObject . Node ) => ( {
50- type : MEMBER_UNINTEREST_NODE ,
51- payload : node
52- } )
63+ export const unInterestNode = ( node : V2exObject . Node ) => async ( dispatch : Dispatch , getState : ( ) => RootState ) => {
64+ dispatch ( {
65+ type : MEMBER_UNINTEREST_NODE ,
66+ payload : node
67+ } )
68+ dispatch ( cacheMemberInterestNodes ( getState ( ) . member . interestNodes ) )
69+ }
5370
5471export const setCurrentToken = ( token ?: V2exObject . MToken ) => ( {
5572 type : APP_AUTH ,
@@ -60,14 +77,14 @@ export const loginByToken = (token: string) => async (dispatch: Dispatch) => {
6077 try {
6178 dispatch ( { type : APP_AUTH_LOADING } )
6279 const token_info = await v2exLib . member . token ( token )
63- loginByTokenSuccess ( dispatch , token_info )
80+ dispatch ( loginByTokenSuccess ( token_info ) as any )
6481 } catch ( e : any ) {
6582 logError ( e )
6683 loginByTokenFail ( dispatch , e . message )
6784 }
6885}
6986
70- const loginByTokenSuccess = async ( dispatch : Dispatch , token : V2exObject . MToken ) => {
87+ const loginByTokenSuccess = ( token : V2exObject . MToken ) => async ( dispatch : Dispatch , getState : ( ) => RootState ) => {
7188 await AsyncStorage . setItem ( MEMBER_TOKEN_KEY , token . token )
7289
7390 v2exLib . setToken ( token . token )
0 commit comments