tooot/src/screens/Compose/utils/reducer.ts

114 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-01-01 23:10:47 +01:00
import { ComposeAction, ComposeState } from './types'
2020-12-30 00:56:25 +01:00
const composeReducer = (
state: ComposeState,
action: ComposeAction
): ComposeState => {
switch (action.type) {
2021-02-07 00:39:11 +01:00
case 'loadDraft':
const draft = action.payload
return {
...state,
...(draft.spoiler?.length && {
spoiler: {
...state.spoiler,
active: true,
raw: draft.spoiler
}
}),
...(draft.text?.length && {
text: { ...state.text, raw: draft.text }
}),
...(draft.poll && { poll: draft.poll }),
...(draft.attachments && { attachments: draft.attachments }),
visibility: draft.visibility,
visibilityLock: draft.visibilityLock,
replyToStatus: draft.replyToStatus
}
case 'dirty':
return { ...state, dirty: action.payload }
2021-01-14 22:53:01 +01:00
case 'posting':
return { ...state, posting: action.payload }
2020-12-30 00:56:25 +01:00
case 'spoiler':
return { ...state, spoiler: { ...state.spoiler, ...action.payload } }
case 'text':
return { ...state, text: { ...state.text, ...action.payload } }
case 'tag':
return { ...state, tag: action.payload }
case 'emoji':
return { ...state, emoji: action.payload }
case 'poll':
return { ...state, poll: { ...state.poll, ...action.payload } }
case 'attachments/sensitive':
return {
...state,
attachments: { ...state.attachments, ...action.payload }
}
case 'attachment/upload/start':
return {
...state,
attachments: {
...state.attachments,
uploads: state.attachments.uploads.concat([action.payload])
}
}
case 'attachment/upload/end':
return {
...state,
attachments: {
...state.attachments,
uploads: state.attachments.uploads.map(upload =>
2022-06-07 20:07:14 +02:00
upload.local?.path === action.payload.local?.path
2020-12-30 00:56:25 +01:00
? { ...upload, remote: action.payload.remote, uploading: false }
: upload
)
}
}
2021-01-04 14:55:34 +01:00
case 'attachment/upload/fail':
return {
...state,
attachments: {
...state.attachments,
uploads: state.attachments.uploads.filter(
2021-02-11 01:33:31 +01:00
upload => upload.local?.hash !== action.payload
2021-01-04 14:55:34 +01:00
)
}
}
2020-12-30 00:56:25 +01:00
case 'attachment/delete':
return {
...state,
attachments: {
...state.attachments,
uploads: state.attachments.uploads.filter(
2021-01-22 01:34:20 +01:00
upload => upload.remote?.id !== action.payload
2020-12-30 00:56:25 +01:00
)
}
}
case 'attachment/edit':
return {
...state,
attachments: {
...state.attachments,
uploads: state.attachments.uploads.map(upload =>
upload.remote!.id === action.payload!.id
? { ...upload, remote: action.payload }
: upload
)
}
}
case 'visibility':
return { ...state, visibility: action.payload }
case 'textInputFocus':
return {
...state,
textInputFocus: { ...state.textInputFocus, ...action.payload }
}
2021-03-09 00:47:40 +01:00
case 'removeReply':
return { ...state, replyToStatus: undefined }
2020-12-30 00:56:25 +01:00
default:
throw new Error('Unexpected action')
}
}
export default composeReducer