pl-fe: improve types

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-11-19 14:19:01 +01:00
parent 16a50885f3
commit 5de47881b7
7 changed files with 110 additions and 34 deletions

View file

@ -495,7 +495,7 @@ const fetchPinnedAccountsRequest = (accountId: string) => ({
accountId,
});
const fetchPinnedAccountsSuccess = (accountId: string, accounts: Array<Account>, next: string | null) => ({
const fetchPinnedAccountsSuccess = (accountId: string, accounts: Array<Account>, next: null) => ({
type: PINNED_ACCOUNTS_FETCH_SUCCESS,
accountId,
accounts,
@ -576,7 +576,7 @@ interface BirthdayRemindersFetchSuccessAction {
type: typeof BIRTHDAY_REMINDERS_FETCH_SUCCESS;
day: number;
month: number;
accountId: Me;
accountId: string;
accounts: Array<Account>;
}
@ -591,7 +591,7 @@ const fetchBirthdayReminders = (month: number, day: number) =>
(dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return;
const me = getState().me;
const me = getState().me as string;
dispatch<BirthdayRemindersFetchRequestAction>({ type: BIRTHDAY_REMINDERS_FETCH_REQUEST, day, month, accountId: me });

View file

@ -489,6 +489,35 @@ const fetchJoinedEvents = () =>
};
type EventsAction =
| ReturnType<typeof submitEventRequest>
| ReturnType<typeof submitEventSuccess>
| ReturnType<typeof submitEventFail>
| ReturnType<typeof joinEventRequest>
| ReturnType<typeof joinEventSuccess>
| ReturnType<typeof joinEventFail>
| ReturnType<typeof leaveEventRequest>
| ReturnType<typeof leaveEventSuccess>
| ReturnType<typeof leaveEventFail>
| ReturnType<typeof fetchEventParticipationsRequest>
| ReturnType<typeof fetchEventParticipationsSuccess>
| ReturnType<typeof fetchEventParticipationsFail>
| ReturnType<typeof expandEventParticipationsRequest>
| ReturnType<typeof expandEventParticipationsSuccess>
| ReturnType<typeof expandEventParticipationsFail>
| ReturnType<typeof fetchEventParticipationRequestsRequest>
| ReturnType<typeof fetchEventParticipationRequestsSuccess>
| ReturnType<typeof fetchEventParticipationRequestsFail>
| ReturnType<typeof expandEventParticipationRequestsRequest>
| ReturnType<typeof expandEventParticipationRequestsSuccess>
| ReturnType<typeof expandEventParticipationRequestsFail>
| ReturnType<typeof expandEventParticipationRequestsSuccess>
| ReturnType<typeof expandEventParticipationRequestsFail>
| ReturnType<typeof authorizeEventParticipationRequestRequest>
| ReturnType<typeof authorizeEventParticipationRequestSuccess>
| ReturnType<typeof authorizeEventParticipationRequestFail>
| ReturnType<typeof rejectEventParticipationRequestRequest>
| ReturnType<typeof rejectEventParticipationRequestSuccess>
| ReturnType<typeof rejectEventParticipationRequestFail>
| ReturnType<typeof cancelEventCompose>
| EventFormSetAction;

View file

@ -5,12 +5,14 @@ import { getClient } from '../api';
import { fetchRelationships } from './accounts';
import { importEntities } from './importer';
import type { Account } from 'pl-api';
const FAMILIAR_FOLLOWERS_FETCH_REQUEST = 'FAMILIAR_FOLLOWERS_FETCH_REQUEST' as const;
const FAMILIAR_FOLLOWERS_FETCH_SUCCESS = 'FAMILIAR_FOLLOWERS_FETCH_SUCCESS' as const;
const FAMILIAR_FOLLOWERS_FETCH_FAIL = 'FAMILIAR_FOLLOWERS_FETCH_FAIL' as const;
const fetchAccountFamiliarFollowers = (accountId: string) => (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({
dispatch<FamiliarFollowersAction>({
type: FAMILIAR_FOLLOWERS_FETCH_REQUEST,
accountId,
});
@ -21,13 +23,13 @@ const fetchAccountFamiliarFollowers = (accountId: string) => (dispatch: AppDispa
dispatch(importEntities({ accounts }));
dispatch(fetchRelationships(accounts.map((item) => item.id)));
dispatch({
dispatch<FamiliarFollowersAction>({
type: FAMILIAR_FOLLOWERS_FETCH_SUCCESS,
accountId,
accounts,
});
})
.catch(error => dispatch({
.catch(error => dispatch<FamiliarFollowersAction>({
type: FAMILIAR_FOLLOWERS_FETCH_FAIL,
accountId,
error,
@ -35,9 +37,27 @@ const fetchAccountFamiliarFollowers = (accountId: string) => (dispatch: AppDispa
}));
};
type FamiliarFollowersAction =
| {
type: typeof FAMILIAR_FOLLOWERS_FETCH_REQUEST;
accountId: string;
}
| {
type: typeof FAMILIAR_FOLLOWERS_FETCH_SUCCESS;
accountId: string;
accounts: Array<Account>;
}
| {
type: typeof FAMILIAR_FOLLOWERS_FETCH_FAIL;
accountId: string;
error: unknown;
skipAlert: true;
}
export {
FAMILIAR_FOLLOWERS_FETCH_REQUEST,
FAMILIAR_FOLLOWERS_FETCH_SUCCESS,
FAMILIAR_FOLLOWERS_FETCH_FAIL,
fetchAccountFamiliarFollowers,
type FamiliarFollowersAction,
};

View file

@ -698,9 +698,13 @@ type InteractionsAction =
| ReturnType<typeof fetchReblogsRequest>
| ReturnType<typeof fetchReblogsSuccess>
| ReturnType<typeof fetchReblogsFail>
| ReturnType<typeof expandReblogsSuccess>
| ReturnType<typeof expandReblogsFail>
| ReturnType<typeof fetchFavouritesRequest>
| ReturnType<typeof fetchFavouritesSuccess>
| ReturnType<typeof fetchFavouritesFail>
| ReturnType<typeof expandFavouritesSuccess>
| ReturnType<typeof expandFavouritesFail>
| ReturnType<typeof fetchDislikesRequest>
| ReturnType<typeof fetchDislikesSuccess>
| ReturnType<typeof fetchDislikesFail>

View file

@ -1,6 +1,6 @@
import { getClient } from '../api';
import type { SaveMarkersParams } from 'pl-api';
import type { Markers, SaveMarkersParams } from 'pl-api';
import type { AppDispatch, RootState } from 'pl-fe/store';
const MARKER_FETCH_REQUEST = 'MARKER_FETCH_REQUEST' as const;
@ -13,24 +13,49 @@ const MARKER_SAVE_FAIL = 'MARKER_SAVE_FAIL' as const;
const fetchMarker = (timeline: Array<string>) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: MARKER_FETCH_REQUEST });
dispatch<MarkersAction>({ type: MARKER_FETCH_REQUEST });
return getClient(getState).timelines.getMarkers(timeline).then((marker) => {
dispatch({ type: MARKER_FETCH_SUCCESS, marker });
dispatch<MarkersAction>({ type: MARKER_FETCH_SUCCESS, marker });
}).catch(error => {
dispatch({ type: MARKER_FETCH_FAIL, error });
dispatch<MarkersAction>({ type: MARKER_FETCH_FAIL, error });
});
};
const saveMarker = (marker: SaveMarkersParams) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: MARKER_SAVE_REQUEST, marker });
dispatch<MarkersAction>({ type: MARKER_SAVE_REQUEST, marker });
return getClient(getState).timelines.saveMarkers(marker).then((marker) => {
dispatch({ type: MARKER_SAVE_SUCCESS, marker });
dispatch<MarkersAction>({ type: MARKER_SAVE_SUCCESS, marker });
}).catch(error => {
dispatch({ type: MARKER_SAVE_FAIL, error });
dispatch<MarkersAction>({ type: MARKER_SAVE_FAIL, error });
});
};
type MarkersAction =
| {
type: typeof MARKER_FETCH_REQUEST;
}
| {
type: typeof MARKER_FETCH_SUCCESS;
marker: Markers;
}
| {
type: typeof MARKER_FETCH_FAIL;
error: unknown;
}
| {
type: typeof MARKER_SAVE_REQUEST;
marker: SaveMarkersParams;
}
| {
type: typeof MARKER_SAVE_SUCCESS;
marker: Markers;
}
| {
type: typeof MARKER_SAVE_FAIL;
error: unknown;
}
export {
MARKER_FETCH_REQUEST,
MARKER_FETCH_SUCCESS,
@ -40,4 +65,5 @@ export {
MARKER_SAVE_FAIL,
fetchMarker,
saveMarker,
type MarkersAction,
};

View file

@ -9,8 +9,8 @@ import {
} from '../actions/accounts';
import {
MARKER_FETCH_SUCCESS,
MARKER_SAVE_REQUEST,
MARKER_SAVE_SUCCESS,
type MarkersAction,
} from '../actions/markers';
import {
NOTIFICATIONS_UPDATE,
@ -23,8 +23,7 @@ import {
} from '../actions/notifications';
import { TIMELINE_DELETE, type TimelineAction } from '../actions/timelines';
import type { Notification as BaseNotification, Markers, NotificationGroup, PaginatedResponse, Relationship } from 'pl-api';
import type { AnyAction } from 'redux';
import type { GroupedNotificationsResults, Markers, NotificationGroup, PaginatedResponse, Relationship } from 'pl-api';
interface State {
items: Array<NotificationGroup>;
@ -75,7 +74,7 @@ const importNotification = (state: State, notification: NotificationGroup) =>
draft.items = [notification, ...draft.items].toSorted(comparator).filter(filterUnique);
});
const expandNormalizedNotifications = (state: State, notifications: NotificationGroup[], next: (() => Promise<PaginatedResponse<BaseNotification>>) | null) =>
const expandNormalizedNotifications = (state: State, notifications: NotificationGroup[], next: (() => Promise<PaginatedResponse<GroupedNotificationsResults, false>>) | null) =>
create(state, (draft) => {
draft.items = [...notifications, ...draft.items].toSorted(comparator).filter(filterUnique);
@ -122,14 +121,14 @@ const importMarker = (state: State, marker: Markers) => {
});
};
const notifications = (state: State = initialState, action: AccountsAction | AnyAction | NotificationsAction | TimelineAction): State => {
const notifications = (state: State = initialState, action: AccountsAction | MarkersAction | NotificationsAction | TimelineAction): State => {
switch (action.type) {
case NOTIFICATIONS_EXPAND_REQUEST:
return create(state, (draft) => {
draft.isLoading = true;
});
case NOTIFICATIONS_EXPAND_FAIL:
if (action.error?.message === 'canceled') return state;
if ((action.error as any)?.message === 'canceled') return state;
return create(state, (draft) => {
draft.isLoading = false;
});
@ -152,7 +151,6 @@ const notifications = (state: State = initialState, action: AccountsAction | Any
case FOLLOW_REQUEST_REJECT_SUCCESS:
return filterNotificationIds(state, [action.accountId], 'follow_request');
case MARKER_FETCH_SUCCESS:
case MARKER_SAVE_REQUEST:
case MARKER_SAVE_SUCCESS:
return importMarker(state, action.marker);
case TIMELINE_DELETE:

View file

@ -1,5 +1,4 @@
import { create } from 'mutative';
import { AnyAction } from 'redux';
import {
FOLLOW_REQUESTS_FETCH_SUCCESS,
@ -26,8 +25,9 @@ import {
EVENT_PARTICIPATION_REQUESTS_FETCH_SUCCESS,
EVENT_PARTICIPATION_REQUEST_AUTHORIZE_SUCCESS,
EVENT_PARTICIPATION_REQUEST_REJECT_SUCCESS,
type EventsAction,
} from 'pl-fe/actions/events';
import { FAMILIAR_FOLLOWERS_FETCH_SUCCESS } from 'pl-fe/actions/familiar-followers';
import { FAMILIAR_FOLLOWERS_FETCH_SUCCESS, type FamiliarFollowersAction } from 'pl-fe/actions/familiar-followers';
import {
GROUP_BLOCKS_FETCH_REQUEST,
GROUP_BLOCKS_FETCH_SUCCESS,
@ -46,8 +46,7 @@ import {
} from 'pl-fe/actions/interactions';
import { NOTIFICATIONS_UPDATE, type NotificationsAction } from 'pl-fe/actions/notifications';
import type { Account, EmojiReaction, Notification, PaginatedResponse } from 'pl-api';
import type { APIEntity } from 'pl-fe/types/entities';
import type { Account, NotificationGroup, PaginatedResponse } from 'pl-api';
interface List {
next: (() => Promise<PaginatedResponse<Account>>) | null;
@ -57,9 +56,9 @@ interface List {
interface Reaction {
accounts: Array<string>;
count: number;
count: number | null;
name: string;
url: string | null;
url: string | undefined;
}
interface ReactionList {
@ -153,12 +152,12 @@ const removeFromList = (state: State, path: NestedListPath | ListPath, accountId
list.items = list.items.filter(item => item !== accountId);
});
const normalizeFollowRequest = (state: State, notification: Notification) =>
const normalizeFollowRequest = (state: State, notification: NotificationGroup) =>
create(state, (draft) => {
draft.follow_requests.items = [...new Set([notification.account.id, ...draft.follow_requests.items])];
draft.follow_requests.items = [...new Set([...notification.sample_account_ids, ...draft.follow_requests.items])];
});
const userLists = (state = initialState, action: AccountsAction | DirectoryAction | GroupsAction | InteractionsAction | NotificationsAction | AnyAction): State => {
const userLists = (state = initialState, action: AccountsAction | DirectoryAction | EventsAction | FamiliarFollowersAction | GroupsAction | InteractionsAction | NotificationsAction): State => {
switch (action.type) {
case REBLOGS_FETCH_SUCCESS:
return normalizeList(state, ['reblogged_by', action.statusId], action.accounts, action.next);
@ -173,7 +172,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio
case REACTIONS_FETCH_SUCCESS:
return create(state, (draft) => {
draft.reactions[action.statusId] = {
items: action.reactions.map((reaction: EmojiReaction) => ({ ...reaction, accounts: reaction.accounts.map(({ id }) => id) })),
items: action.reactions.map((reaction) => ({ ...reaction, accounts: reaction.accounts.map(({ id }) => id) })),
next: null,
isLoading: false,
};
@ -206,7 +205,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio
case BIRTHDAY_REMINDERS_FETCH_SUCCESS:
return normalizeList(state, ['birthday_reminders', action.accountId], action.accounts);
case FAMILIAR_FOLLOWERS_FETCH_SUCCESS:
return normalizeList(state, ['familiar_followers', action.accountId], action.accounts, action.next);
return normalizeList(state, ['familiar_followers', action.accountId], action.accounts);
case EVENT_PARTICIPATIONS_FETCH_SUCCESS:
return normalizeList(state, ['event_participations', action.statusId], action.accounts, action.next);
case EVENT_PARTICIPATIONS_EXPAND_SUCCESS:
@ -215,7 +214,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio
return create(state, (draft) => {
draft.event_participation_requests[action.statusId] = {
next: action.next,
items: action.participations.map(({ account, participation_message }: APIEntity) => ({
items: action.participations.map(({ account, participation_message }) => ({
account: account.id,
participation_message,
})),
@ -226,7 +225,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio
return create(state, (draft) => {
const list = draft.event_participation_requests[action.statusId];
list.next = action.next;
list.items = [...list.items, ...action.participations.map(({ account, participation_message }: APIEntity) => ({
list.items = [...list.items, ...action.participations.map(({ account, participation_message }) => ({
account: account.id,
participation_message,
}))];
@ -236,7 +235,7 @@ const userLists = (state = initialState, action: AccountsAction | DirectoryActio
case EVENT_PARTICIPATION_REQUEST_REJECT_SUCCESS:
return create(state, (draft) => {
const list = draft.event_participation_requests[action.statusId];
if (list.items) list.items = list.items.filter(item => item !== action.accountId);
if (list.items) list.items = list.items.filter(item => item.account !== action.accountId);
});
case GROUP_BLOCKS_FETCH_SUCCESS:
return normalizeList(state, ['group_blocks', action.groupId], action.accounts, action.next);