1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

Draft basic done

#6
This commit is contained in:
Zhiyuan Zheng
2021-02-07 00:39:11 +01:00
parent 700b9ad433
commit 03dc94c7c9
32 changed files with 684 additions and 170 deletions

View File

@ -2,8 +2,10 @@ import client from '@api/client'
import analytics from '@components/analytics'
import { createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit'
import { RootState } from '@root/store'
import { ComposeStateDraft } from '@screens/Compose/utils/types'
import * as AuthSession from 'expo-auth-session'
import * as Localization from 'expo-localization'
import { findIndex } from 'lodash'
export type InstanceLocal = {
appData: {
@ -23,6 +25,7 @@ export type InstanceLocal = {
notification: {
latestTime?: Mastodon.Notification['created_at']
}
drafts: ComposeStateDraft[]
}
export type InstancesState = {
@ -36,8 +39,8 @@ export type InstancesState = {
}
}
export const localUpdateAccountPreferences = createAsyncThunk(
'instances/localUpdateAccountPreferences',
export const updateLocalAccountPreferences = createAsyncThunk(
'instances/updateLocalAccountPreferences',
async (): Promise<Mastodon.Preferences> => {
const preferences = await client<Mastodon.Preferences>({
method: 'get',
@ -119,7 +122,8 @@ export const localAddInstance = createAsyncThunk(
},
notification: {
latestTime: undefined
}
},
drafts: []
}
})
}
@ -179,7 +183,7 @@ const instancesSlice = createSlice({
name: 'instances',
initialState: instancesInitialState,
reducers: {
localUpdateActiveIndex: (state, action: PayloadAction<InstanceLocal>) => {
updateLocalActiveIndex: (state, action: PayloadAction<InstanceLocal>) => {
state.local.activeIndex = state.local.instances.findIndex(
instance =>
instance.url === action.payload.url &&
@ -187,7 +191,7 @@ const instancesSlice = createSlice({
instance.account.id === action.payload.account.id
)
},
localUpdateAccount: (
updateLocalAccount: (
state,
action: PayloadAction<
Pick<InstanceLocal['account'], 'acct' & 'avatarStatic'>
@ -200,18 +204,42 @@ const instancesSlice = createSlice({
}
}
},
localUpdateNotification: (
updateLocalNotification: (
state,
action: PayloadAction<Partial<InstanceLocal['notification']>>
) => {
state.local.instances[state.local.activeIndex!].notification =
action.payload
if (state.local.activeIndex !== null) {
state.local.instances[state.local.activeIndex].notification =
action.payload
}
},
remoteUpdate: (
updateLocalDraft: (state, action: PayloadAction<ComposeStateDraft>) => {
if (state.local.activeIndex !== null) {
const draftIndex = findIndex(
state.local.instances[state.local.activeIndex].drafts,
['timestamp', action.payload.timestamp]
)
if (draftIndex === -1) {
state.local.instances[state.local.activeIndex].drafts.unshift(
action.payload
)
} else {
state.local.instances[state.local.activeIndex].drafts[draftIndex] =
action.payload
}
}
},
removeLocalDraft: (
state,
action: PayloadAction<InstancesState['remote']['url']>
action: PayloadAction<ComposeStateDraft['timestamp']>
) => {
state.remote.url = action.payload
if (state.local.activeIndex !== null) {
state.local.instances[
state.local.activeIndex
].drafts = state.local.instances[
state.local.activeIndex
].drafts?.filter(draft => draft.timestamp !== action.payload)
}
}
},
extraReducers: builder => {
@ -255,11 +283,11 @@ const instancesSlice = createSlice({
console.error(action.error)
})
.addCase(localUpdateAccountPreferences.fulfilled, (state, action) => {
.addCase(updateLocalAccountPreferences.fulfilled, (state, action) => {
state.local.instances[state.local.activeIndex!].account.preferences =
action.payload
})
.addCase(localUpdateAccountPreferences.rejected, (_, action) => {
.addCase(updateLocalAccountPreferences.rejected, (_, action) => {
console.error(action.error)
})
}
@ -289,13 +317,18 @@ export const getLocalNotification = ({ instances: { local } }: RootState) =>
local.activeIndex !== null
? local.instances[local.activeIndex].notification
: undefined
export const getLocalDrafts = ({ instances: { local } }: RootState) =>
local.activeIndex !== null
? local.instances[local.activeIndex].drafts
: undefined
export const getRemoteUrl = ({ instances: { remote } }: RootState) => remote.url
export const {
localUpdateActiveIndex,
localUpdateAccount,
localUpdateNotification,
remoteUpdate
updateLocalActiveIndex,
updateLocalAccount,
updateLocalNotification,
updateLocalDraft,
removeLocalDraft
} = instancesSlice.actions
export default instancesSlice.reducer