tooot/src/utils/slices/instancesSlice.ts

121 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-11-21 13:19:05 +01:00
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'
2020-12-13 14:04:25 +01:00
import { RootState } from '@root/store'
import client from '@api/client'
2020-11-21 13:19:05 +01:00
export type InstancesState = {
local: {
url: string | undefined
token: string | undefined
account: {
id: Mastodon.Account['id'] | undefined
preferences: Mastodon.Preferences
}
}
remote: {
url: string
}
}
2020-11-24 00:18:47 +01:00
const initialStateLocal = {
url: undefined,
token: undefined,
account: {
id: undefined,
preferences: {
'posting:default:visibility': undefined,
'posting:default:sensitive': undefined,
'posting:default:language': undefined,
'reading:expand:media': undefined,
'reading:expand:spoilers': undefined
}
}
}
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']
}) => {
const {
body: { id }
} = await client({
method: 'get',
instance: 'remote',
instanceDomain: url,
url: `accounts/verify_credentials`,
headers: { Authorization: `Bearer ${token}` }
})
const { body: preferences } = await client({
method: 'get',
instance: 'remote',
instanceDomain: url,
url: `preferences`,
headers: { Authorization: `Bearer ${token}` }
})
return {
url,
token,
account: {
id,
preferences
}
} as InstancesState['local']
}
)
const instancesSlice = createSlice({
name: 'instances',
initialState: {
2020-11-24 00:18:47 +01:00
local: initialStateLocal,
remote: {
2020-11-24 00:18:47 +01:00
url: 'm.cmx.im'
}
} as InstancesState,
2020-12-22 00:10:55 +01:00
reducers: {
resetLocal: state => {
state.local = initialStateLocal
}
},
extraReducers: builder => {
builder
.addCase(loginLocal.fulfilled, (state, action) => {
state.local = action.payload
})
.addCase(updateLocalAccountPreferences.fulfilled, (state, action) => {
state.local.account.preferences = action.payload
})
}
})
export const getLocalUrl = (state: RootState) => state.instances.local.url
export const getLocalToken = (state: RootState) => state.instances.local.token
2020-11-24 00:18:47 +01:00
export const getRemoteUrl = (state: RootState) => state.instances.remote.url
export const getLocalAccountId = (state: RootState) =>
state.instances.local.account.id
export const getLocalAccountPreferences = (state: RootState) =>
state.instances.local.account.preferences
2020-12-22 00:10:55 +01:00
export const { resetLocal } = instancesSlice.actions
export default instancesSlice.reducer