1
0
mirror of https://github.com/tooot-app/app synced 2024-12-28 02:10:02 +01:00

Start up process of API calls

Prepare for announcement fetching
This commit is contained in:
Zhiyuan Zheng 2020-12-22 01:09:34 +01:00
parent 436b0ab4dd
commit 4d6cee643b
No known key found for this signature in database
GPG Key ID: 078A93AB607D85E0
5 changed files with 43 additions and 20 deletions

View File

@ -7,7 +7,7 @@ import { PersistGate } from 'redux-persist/integration/react'
import { Index } from '@root/Index'
import { persistor, store } from '@root/store'
import ThemeManager from '@utils/styles/ThemeManager'
import { resetLocal, updateLocal } from '@root/utils/slices/instancesSlice'
import { resetLocal } from '@root/utils/slices/instancesSlice'
import client from '@root/api/client'
const queryClient = new QueryClient()
@ -66,8 +66,8 @@ const App: React.FC = () => {
if (res.body.id !== store.getState().instances.local.account.id) {
store.dispatch(resetLocal())
setLocalCorrupt(true)
setAppLoaded(true)
}
setAppLoaded(true)
})
.catch(() => {
store.dispatch(resetLocal())

View File

@ -18,8 +18,11 @@ import { useTheme } from '@utils/styles/ThemeManager'
import getCurrentTab from '@utils/getCurrentTab'
import { toast, toastConfig } from '@components/toast'
import { useTranslation } from 'react-i18next'
import { useSelector } from 'react-redux'
import { getLocalUrl } from './utils/slices/instancesSlice'
import { useDispatch, useSelector } from 'react-redux'
import {
getLocalUrl,
updateLocalAccountPreferences
} from '@utils/slices/instancesSlice'
enableScreens()
const Tab = createBottomTabNavigator<RootStackParamList>()
@ -37,6 +40,7 @@ export interface Props {
}
export const Index: React.FC<Props> = ({ localCorrupt }) => {
const dispatch = useDispatch()
const localInstance = useSelector(getLocalUrl)
const { i18n } = useTranslation()
const { mode, theme } = useTheme()
@ -57,6 +61,12 @@ export const Index: React.FC<Props> = ({ localCorrupt }) => {
return showLocalCorrect
}, [localCorrupt])
useEffect(() => {
if (localInstance) {
dispatch(updateLocalAccountPreferences())
}
}, [])
return (
<>
<StatusBar barStyle={barStyle[mode]} />

View File

@ -13,7 +13,7 @@ import { debounce } from 'lodash'
import { instanceFetch } from '@utils/fetches/instanceFetch'
import * as AuthSession from 'expo-auth-session'
import { useDispatch } from 'react-redux'
import { updateLocal } from '@utils/slices/instancesSlice'
import { loginLocal } from '@utils/slices/instancesSlice'
import { useNavigation } from '@react-navigation/native'
import { useTheme } from '@utils/styles/ThemeManager'
@ -98,7 +98,7 @@ const Login: React.FC = () => {
tokenEndpoint: `https://${instanceDomain}/oauth/token`
}
)
dispatch(updateLocal({ url: instanceDomain, token: accessToken }))
dispatch(loginLocal({ url: instanceDomain, token: accessToken }))
navigation.navigate('Screen-Local')
}
})()

View File

@ -1,7 +1,7 @@
import React from 'react'
import { useDispatch } from 'react-redux'
import { updateLocal } from '@utils/slices/instancesSlice'
import { resetLocal } from '@utils/slices/instancesSlice'
import MenuButton from '@components/Menu/Button'
import { MenuContainer } from '@components/Menu'
import { useNavigation } from '@react-navigation/native'
@ -23,7 +23,7 @@ const Logout: React.FC = () => {
style: 'destructive' as const,
onPress: () => {
queryClient.clear()
dispatch(updateLocal({}))
dispatch(resetLocal())
navigation.navigate('Screen-Public', {
screen: 'Screen-Public-Root',
params: { publicTab: true }

View File

@ -32,19 +32,28 @@ const initialStateLocal = {
}
}
export const updateLocal = createAsyncThunk(
'instances/updateLocal',
export const updateLocalAccountPreferences = createAsyncThunk(
'instances/updateLocalAccountPreferences',
async () => {
const { body: preferences } = await client({
method: 'get',
instance: 'local',
url: `preferences`
})
return preferences as Mastodon.Preferences
}
)
export const loginLocal = createAsyncThunk(
'instances/loginLocal',
async ({
url,
token
}: {
url?: InstancesState['local']['url']
token?: InstancesState['local']['token']
url: InstancesState['local']['url']
token: InstancesState['local']['token']
}) => {
if (!url || !token) {
return initialStateLocal
}
const {
body: { id }
} = await client({
@ -70,7 +79,7 @@ export const updateLocal = createAsyncThunk(
id,
preferences
}
}
} as InstancesState['local']
}
)
@ -88,9 +97,13 @@ const instancesSlice = createSlice({
}
},
extraReducers: builder => {
builder.addCase(updateLocal.fulfilled, (state, action) => {
state.local = action.payload
})
builder
.addCase(loginLocal.fulfilled, (state, action) => {
state.local = action.payload
})
.addCase(updateLocalAccountPreferences.fulfilled, (state, action) => {
state.local.account.preferences = action.payload
})
}
})