Append scheduled statuses to status list

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-01-01 23:46:47 +01:00
parent ea2bb53379
commit c7e140bf3f

View file

@ -12,6 +12,7 @@ import {
STATUS_QUOTES_FETCH_REQUEST, STATUS_QUOTES_FETCH_REQUEST,
STATUS_QUOTES_FETCH_SUCCESS, STATUS_QUOTES_FETCH_SUCCESS,
} from 'soapbox/actions/status-quotes'; } from 'soapbox/actions/status-quotes';
import { STATUS_CREATE_SUCCESS } from 'soapbox/actions/statuses';
import { import {
BOOKMARKED_STATUSES_FETCH_REQUEST, BOOKMARKED_STATUSES_FETCH_REQUEST,
@ -66,7 +67,7 @@ import {
} from '../actions/scheduled-statuses'; } from '../actions/scheduled-statuses';
import type { AnyAction } from 'redux'; 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({ export const StatusListRecord = ImmutableRecord({
next: null as string | null, next: null as string | null,
@ -77,8 +78,6 @@ export const StatusListRecord = ImmutableRecord({
type State = ImmutableMap<string, StatusList>; type State = ImmutableMap<string, StatusList>;
type StatusList = ReturnType<typeof StatusListRecord>; type StatusList = ReturnType<typeof StatusListRecord>;
type Status = string | StatusEntity;
type Statuses = Array<string | StatusEntity>;
const initialState: State = ImmutableMap({ const initialState: State = ImmutableMap({
favourites: StatusListRecord(), favourites: StatusListRecord(),
@ -89,15 +88,15 @@ const initialState: State = ImmutableMap({
joined_events: StatusListRecord(), 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)) ImmutableOrderedSet(statuses.map(getStatusId))
); );
const setLoading = (state: State, listType: string, loading: boolean) => state.setIn([listType, 'isLoading'], loading); 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 => { return state.update(listType, StatusListRecord(), listMap => listMap.withMutations(map => {
map.set('next', next); map.set('next', next);
map.set('loaded', true); 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); const newIds = getStatusIds(statuses);
return state.update(listType, StatusListRecord(), listMap => listMap.withMutations(map => { 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); const statusId = getStatusId(status);
return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => { return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => {
return ImmutableOrderedSet([statusId]).union(items as ImmutableOrderedSet<string>); return ImmutableOrderedSet([statusId]).union(items as ImmutableOrderedSet<string>);
}); });
}; };
const removeOneFromList = (state: State, listType: string, status: Status) => { const removeOneFromList = (state: State, listType: string, status: APIEntity) => {
const statusId = getStatusId(status); const statusId = getStatusId(status);
return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => (items as ImmutableOrderedSet<string>).delete(statusId)); return state.updateIn([listType, 'items'], ImmutableOrderedSet(), items => (items as ImmutableOrderedSet<string>).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) { export default function statusLists(state = initialState, action: AnyAction) {
switch (action.type) { switch (action.type) {
case FAVOURITED_STATUSES_FETCH_REQUEST: case FAVOURITED_STATUSES_FETCH_REQUEST:
@ -209,6 +213,8 @@ export default function statusLists(state = initialState, action: AnyAction) {
return setLoading(state, 'joined_events', false); return setLoading(state, 'joined_events', false);
case JOINED_EVENTS_FETCH_SUCCESS: case JOINED_EVENTS_FETCH_SUCCESS:
return normalizeList(state, 'joined_events', action.statuses, action.next); return normalizeList(state, 'joined_events', action.statuses, action.next);
case STATUS_CREATE_SUCCESS:
return maybeAppendScheduledStatus(state, action.status);
default: default:
return state; return state;
} }