diff --git a/CHANGELOG.md b/CHANGELOG.md index 05162696f..5dfd256cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Layout: use accent color for "floating action button" (mobile compose button). - ServiceWorker: don't serve favicon, robots.txt, and others from ServiceWorker. - Datepicker: correctly default to the current year. +- Scheduled posts: fix page crashing on deleting a scheduled post. ## [3.0.0] - 2022-12-25 diff --git a/app/soapbox/features/scheduled-statuses/components/scheduled-status.tsx b/app/soapbox/features/scheduled-statuses/components/scheduled-status.tsx index 1d1a68c94..cca79ffd4 100644 --- a/app/soapbox/features/scheduled-statuses/components/scheduled-status.tsx +++ b/app/soapbox/features/scheduled-statuses/components/scheduled-status.tsx @@ -20,7 +20,12 @@ interface IScheduledStatus { } const ScheduledStatus: React.FC = ({ statusId, ...other }) => { - const status = useAppSelector((state) => buildStatus(state, state.scheduled_statuses.get(statusId)!)) as StatusEntity; + const status = useAppSelector((state) => { + const scheduledStatus = state.scheduled_statuses.get(statusId); + + if (!scheduledStatus) return null; + return buildStatus(state, scheduledStatus); + }) as StatusEntity | null; if (!status) return null; diff --git a/app/soapbox/reducers/compose.ts b/app/soapbox/reducers/compose.ts index d321ec05e..18a1fdac2 100644 --- a/app/soapbox/reducers/compose.ts +++ b/app/soapbox/reducers/compose.ts @@ -455,7 +455,7 @@ export default function compose(state = initialState, action: AnyAction) { case COMPOSE_POLL_REMOVE: return updateCompose(state, action.id, compose => compose.set('poll', null)); case COMPOSE_SCHEDULE_ADD: - return updateCompose(state, action.id, compose => compose.set('schedule', new Date())); + return updateCompose(state, action.id, compose => compose.set('schedule', new Date(Date.now() + 10 * 60 * 1000))); case COMPOSE_SCHEDULE_SET: return updateCompose(state, action.id, compose => compose.set('schedule', action.date)); case COMPOSE_SCHEDULE_REMOVE: diff --git a/app/soapbox/reducers/status-lists.ts b/app/soapbox/reducers/status-lists.ts index eea737a05..38e19b009 100644 --- a/app/soapbox/reducers/status-lists.ts +++ b/app/soapbox/reducers/status-lists.ts @@ -12,6 +12,7 @@ import { STATUS_QUOTES_FETCH_REQUEST, STATUS_QUOTES_FETCH_SUCCESS, } from 'soapbox/actions/status-quotes'; +import { STATUS_CREATE_SUCCESS } from 'soapbox/actions/statuses'; import { BOOKMARKED_STATUSES_FETCH_REQUEST, @@ -66,7 +67,7 @@ import { } from '../actions/scheduled-statuses'; import type { AnyAction } from 'redux'; -import type { Status as StatusEntity } from 'soapbox/types/entities'; +import type { APIEntity } from 'soapbox/types/entities'; export const StatusListRecord = ImmutableRecord({ next: null as string | null, @@ -77,8 +78,6 @@ export const StatusListRecord = ImmutableRecord({ type State = ImmutableMap; type StatusList = ReturnType; -type Status = string | StatusEntity; -type Statuses = Array; const initialState: State = ImmutableMap({ favourites: StatusListRecord(), @@ -89,15 +88,15 @@ const initialState: State = ImmutableMap({ joined_events: StatusListRecord(), }); -const getStatusId = (status: string | StatusEntity) => typeof status === 'string' ? status : status.id; +const getStatusId = (status: string | APIEntity) => typeof status === 'string' ? status : status.id; -const getStatusIds = (statuses: Statuses = []) => ( +const getStatusIds = (statuses: APIEntity[] = []) => ( ImmutableOrderedSet(statuses.map(getStatusId)) ); const setLoading = (state: State, listType: string, loading: boolean) => state.setIn([listType, 'isLoading'], loading); -const normalizeList = (state: State, listType: string, statuses: Statuses, next: string | null) => { +const normalizeList = (state: State, listType: string, statuses: APIEntity[], next: string | null) => { return state.update(listType, StatusListRecord(), listMap => listMap.withMutations(map => { map.set('next', next); map.set('loaded', true); @@ -106,7 +105,7 @@ const normalizeList = (state: State, listType: string, statuses: Statuses, next: })); }; -const appendToList = (state: State, listType: string, statuses: Statuses, next: string | null) => { +const appendToList = (state: State, listType: string, statuses: APIEntity[], next: string | null) => { const newIds = getStatusIds(statuses); return state.update(listType, StatusListRecord(), listMap => listMap.withMutations(map => { @@ -116,18 +115,23 @@ const appendToList = (state: State, listType: string, statuses: Statuses, next: })); }; -const prependOneToList = (state: State, listType: string, status: Status) => { +const prependOneToList = (state: State, listType: string, status: APIEntity) => { const statusId = getStatusId(status); return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => { return ImmutableOrderedSet([statusId]).union(items as ImmutableOrderedSet); }); }; -const removeOneFromList = (state: State, listType: string, status: Status) => { +const removeOneFromList = (state: State, listType: string, status: APIEntity) => { const statusId = getStatusId(status); return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => (items as ImmutableOrderedSet).delete(statusId)); }; +const maybeAppendScheduledStatus = (state: State, status: APIEntity) => { + if (!status.scheduled_at) return state; + return prependOneToList(state, 'scheduled_statuses', getStatusId(status)); +}; + export default function statusLists(state = initialState, action: AnyAction) { switch (action.type) { case FAVOURITED_STATUSES_FETCH_REQUEST: @@ -209,6 +213,8 @@ export default function statusLists(state = initialState, action: AnyAction) { return setLoading(state, 'joined_events', false); case JOINED_EVENTS_FETCH_SUCCESS: return normalizeList(state, 'joined_events', action.statuses, action.next); + case STATUS_CREATE_SUCCESS: + return maybeAppendScheduledStatus(state, action.status); default: return state; }