pl-fe: remove remaining AnyAction from reducers

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-11-27 21:12:37 +01:00
parent 5b07c47f68
commit 627a810503
5 changed files with 91 additions and 61 deletions

View file

@ -6,7 +6,7 @@ import { getClient } from '../api';
import { deleteFromTimelines } from './timelines';
import type { Account, AdminGetAccountsParams, AdminGetReportsParams, PleromaConfig, Status } from 'pl-api';
import type { Account, AdminAccount, AdminGetAccountsParams, AdminGetReportsParams, AdminReport, PaginatedResponse, PleromaConfig, Status } from 'pl-api';
import type { AppDispatch, RootState } from 'pl-fe/store';
const ADMIN_CONFIG_FETCH_REQUEST = 'ADMIN_CONFIG_FETCH_REQUEST' as const;
@ -106,16 +106,16 @@ const fetchReports = (params?: AdminGetReportsParams) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const state = getState();
dispatch({ type: ADMIN_REPORTS_FETCH_REQUEST, params });
dispatch<AdminActions>({ type: ADMIN_REPORTS_FETCH_REQUEST, params });
return getClient(state).admin.reports.getReports(params)
.then(({ items }) => {
items.forEach((report) => {
dispatch(importEntities({ statuses: report.statuses as Array<Status>, accounts: [report.account?.account, report.target_account?.account] }));
dispatch({ type: ADMIN_REPORTS_FETCH_SUCCESS, reports: items, params });
dispatch<AdminActions>({ type: ADMIN_REPORTS_FETCH_SUCCESS, reports: items, params });
});
}).catch(error => {
dispatch({ type: ADMIN_REPORTS_FETCH_FAIL, error, params });
dispatch<AdminActions>({ type: ADMIN_REPORTS_FETCH_FAIL, error, params });
});
};
@ -123,12 +123,12 @@ const closeReport = (reportId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const state = getState();
dispatch({ type: ADMIN_REPORT_PATCH_REQUEST, reportId });
dispatch<AdminActions>({ type: ADMIN_REPORT_PATCH_REQUEST, reportId });
return getClient(state).admin.reports.resolveReport(reportId).then((report) => {
dispatch({ type: ADMIN_REPORT_PATCH_SUCCESS, report, reportId });
dispatch<AdminActions>({ type: ADMIN_REPORT_PATCH_SUCCESS, report, reportId });
}).catch(error => {
dispatch({ type: ADMIN_REPORT_PATCH_FAIL, error, reportId });
dispatch<AdminActions>({ type: ADMIN_REPORT_PATCH_FAIL, error, reportId });
});
};
@ -136,15 +136,15 @@ const fetchUsers = (params?: AdminGetAccountsParams) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const state = getState();
dispatch({ type: ADMIN_USERS_FETCH_REQUEST, params });
dispatch<AdminActions>({ type: ADMIN_USERS_FETCH_REQUEST, params });
return getClient(state).admin.accounts.getAccounts(params).then((res) => {
dispatch(importEntities({ accounts: res.items.map(({ account }) => account).filter((account): account is Account => account !== null) }));
dispatch(fetchRelationships(res.items.map((account) => account.id)));
dispatch({ type: ADMIN_USERS_FETCH_SUCCESS, users: res.items, params, next: res.next });
dispatch<AdminActions>({ type: ADMIN_USERS_FETCH_SUCCESS, users: res.items, params, next: res.next });
return res;
}).catch(error => {
dispatch({ type: ADMIN_USERS_FETCH_FAIL, error, params });
dispatch<AdminActions>({ type: ADMIN_USERS_FETCH_FAIL, error, params });
throw error;
});
};
@ -153,20 +153,20 @@ const deactivateUser = (accountId: string, report_id?: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const state = getState();
dispatch({ type: ADMIN_USER_DEACTIVATE_REQUEST, accountId });
dispatch<AdminActions>({ type: ADMIN_USER_DEACTIVATE_REQUEST, accountId });
return getClient(state).admin.accounts.performAccountAction(accountId, 'suspend', { report_id });
};
const deleteUser = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: ADMIN_USER_DELETE_REQUEST, accountId });
dispatch<AdminActions>({ type: ADMIN_USER_DELETE_REQUEST, accountId });
return getClient(getState).admin.accounts.deleteAccount(accountId)
.then(() => {
dispatch({ type: ADMIN_USER_DELETE_SUCCESS, accountId });
dispatch<AdminActions>({ type: ADMIN_USER_DELETE_SUCCESS, accountId });
}).catch(error => {
dispatch({ type: ADMIN_USER_DELETE_FAIL, error, accountId });
dispatch<AdminActions>({ type: ADMIN_USER_DELETE_FAIL, error, accountId });
});
};
@ -174,57 +174,57 @@ const approveUser = (accountId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
const state = getState();
dispatch({ type: ADMIN_USER_APPROVE_REQUEST, accountId });
dispatch<AdminActions>({ type: ADMIN_USER_APPROVE_REQUEST, accountId });
return getClient(state).admin.accounts.approveAccount(accountId)
.then((user) => {
dispatch({ type: ADMIN_USER_APPROVE_SUCCESS, user, accountId });
dispatch<AdminActions>({ type: ADMIN_USER_APPROVE_SUCCESS, user, accountId });
}).catch(error => {
dispatch({ type: ADMIN_USER_APPROVE_FAIL, error, accountId });
dispatch<AdminActions>({ type: ADMIN_USER_APPROVE_FAIL, error, accountId });
});
};
const deleteStatus = (statusId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: ADMIN_STATUS_DELETE_REQUEST, statusId });
dispatch<AdminActions>({ type: ADMIN_STATUS_DELETE_REQUEST, statusId });
return getClient(getState).admin.statuses.deleteStatus(statusId)
.then(() => {
dispatch(deleteFromTimelines(statusId));
return dispatch({ type: ADMIN_STATUS_DELETE_SUCCESS, statusId });
return dispatch<AdminActions>({ type: ADMIN_STATUS_DELETE_SUCCESS, statusId });
}).catch(error => {
return dispatch({ type: ADMIN_STATUS_DELETE_FAIL, error, statusId });
return dispatch<AdminActions>({ type: ADMIN_STATUS_DELETE_FAIL, error, statusId });
});
};
const toggleStatusSensitivity = (statusId: string, sensitive: boolean) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_REQUEST, statusId });
dispatch<AdminActions>({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_REQUEST, statusId });
return getClient(getState).admin.statuses.updateStatus(statusId, { sensitive: !sensitive })
.then((status) => {
dispatch(importEntities({ statuses: [status] }));
dispatch({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_SUCCESS, statusId, status });
dispatch<AdminActions>({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_SUCCESS, statusId, status });
}).catch(error => {
dispatch({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_FAIL, error, statusId });
dispatch<AdminActions>({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_FAIL, error, statusId });
});
};
const tagUser = (accountId: string, tags: string[]) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: ADMIN_USER_TAG_REQUEST, accountId, tags });
dispatch<AdminActions>({ type: ADMIN_USER_TAG_REQUEST, accountId, tags });
return getClient(getState).admin.accounts.tagUser(accountId, tags).then(() => {
dispatch({ type: ADMIN_USER_TAG_SUCCESS, accountId, tags });
dispatch<AdminActions>({ type: ADMIN_USER_TAG_SUCCESS, accountId, tags });
}).catch(error => {
dispatch({ type: ADMIN_USER_TAG_FAIL, error, accountId, tags });
dispatch<AdminActions>({ type: ADMIN_USER_TAG_FAIL, error, accountId, tags });
});
};
const untagUser = (accountId: string, tags: string[]) =>
(dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: ADMIN_USER_UNTAG_REQUEST, accountId, tags });
dispatch<AdminActions>({ type: ADMIN_USER_UNTAG_REQUEST, accountId, tags });
return getClient(getState).admin.accounts.untagUser(accountId, tags).then(() => {
dispatch({ type: ADMIN_USER_UNTAG_SUCCESS, accountId, tags });
dispatch<AdminActions>({ type: ADMIN_USER_UNTAG_SUCCESS, accountId, tags });
}).catch(error => {
dispatch({ type: ADMIN_USER_UNTAG_FAIL, error, accountId, tags });
dispatch<AdminActions>({ type: ADMIN_USER_UNTAG_FAIL, error, accountId, tags });
});
};
@ -278,7 +278,7 @@ const fetchUserIndex = () =>
if (isLoading) return;
dispatch({ type: ADMIN_USER_INDEX_FETCH_REQUEST });
dispatch<AdminActions>({ type: ADMIN_USER_INDEX_FETCH_REQUEST });
const params: AdminGetAccountsParams = {
origin: 'local',
@ -289,9 +289,9 @@ const fetchUserIndex = () =>
dispatch(fetchUsers(params))
.then((data) => {
const { items, total, next } = data;
dispatch({ type: ADMIN_USER_INDEX_FETCH_SUCCESS, users: items, total, next, params });
dispatch<AdminActions>({ type: ADMIN_USER_INDEX_FETCH_SUCCESS, users: items, total, next, params });
}).catch(() => {
dispatch({ type: ADMIN_USER_INDEX_FETCH_FAIL });
dispatch<AdminActions>({ type: ADMIN_USER_INDEX_FETCH_FAIL });
});
};
@ -301,14 +301,14 @@ const expandUserIndex = () =>
if (!loaded || isLoading || !next) return;
dispatch({ type: ADMIN_USER_INDEX_EXPAND_REQUEST });
dispatch<AdminActions>({ type: ADMIN_USER_INDEX_EXPAND_REQUEST });
next()
.then((data) => {
const { items, total, next } = data;
dispatch({ type: ADMIN_USER_INDEX_EXPAND_SUCCESS, users: items, total, next, params });
dispatch<AdminActions>({ type: ADMIN_USER_INDEX_EXPAND_SUCCESS, users: items, total, next, params });
}).catch(() => {
dispatch({ type: ADMIN_USER_INDEX_EXPAND_FAIL });
dispatch<AdminActions>({ type: ADMIN_USER_INDEX_EXPAND_FAIL });
});
};
@ -319,6 +319,41 @@ type AdminActions =
| { type: typeof ADMIN_CONFIG_UPDATE_REQUEST; configs: PleromaConfig['configs'] }
| { type: typeof ADMIN_CONFIG_UPDATE_SUCCESS; configs: PleromaConfig['configs']; needsReboot: boolean }
| { type: typeof ADMIN_CONFIG_UPDATE_FAIL; error: unknown; configs: PleromaConfig['configs'] }
| { type: typeof ADMIN_REPORTS_FETCH_REQUEST; params?: AdminGetReportsParams }
| { type: typeof ADMIN_REPORTS_FETCH_SUCCESS; reports: Array<AdminReport>; params?: AdminGetReportsParams }
| { type: typeof ADMIN_REPORTS_FETCH_FAIL; error: unknown; params?: AdminGetReportsParams }
| { type: typeof ADMIN_REPORT_PATCH_REQUEST; reportId: string }
| { type: typeof ADMIN_REPORT_PATCH_SUCCESS; report: AdminReport; reportId: string }
| { type: typeof ADMIN_REPORT_PATCH_FAIL; error: unknown; reportId: string }
| { type: typeof ADMIN_USERS_FETCH_REQUEST; params?: AdminGetAccountsParams }
| { type: typeof ADMIN_USERS_FETCH_SUCCESS; users: Array<AdminAccount>; params?: AdminGetAccountsParams; next: (() => Promise<PaginatedResponse<AdminAccount>>) | null }
| { type: typeof ADMIN_USERS_FETCH_FAIL; error: unknown; params?: AdminGetAccountsParams }
| { type: typeof ADMIN_USER_DEACTIVATE_REQUEST; accountId: string }
| { type: typeof ADMIN_USER_DELETE_REQUEST; accountId: string }
| { type: typeof ADMIN_USER_DELETE_SUCCESS; accountId: string }
| { type: typeof ADMIN_USER_DELETE_FAIL; error: unknown; accountId: string }
| { type: typeof ADMIN_USER_APPROVE_REQUEST; accountId: string }
| { type: typeof ADMIN_USER_APPROVE_SUCCESS; user: AdminAccount; accountId: string }
| { type: typeof ADMIN_USER_APPROVE_FAIL; error: unknown; accountId: string }
| { type: typeof ADMIN_STATUS_DELETE_REQUEST; statusId: string }
| { type: typeof ADMIN_STATUS_DELETE_SUCCESS; statusId: string }
| { type: typeof ADMIN_STATUS_DELETE_FAIL; error: unknown; statusId: string }
| { type: typeof ADMIN_STATUS_TOGGLE_SENSITIVITY_REQUEST; statusId: string }
| { type: typeof ADMIN_STATUS_TOGGLE_SENSITIVITY_SUCCESS; statusId: string; status: Status }
| { type: typeof ADMIN_STATUS_TOGGLE_SENSITIVITY_FAIL; error: unknown; statusId: string }
| { type: typeof ADMIN_USER_TAG_REQUEST; accountId: string; tags: Array<string> }
| { type: typeof ADMIN_USER_TAG_SUCCESS; accountId: string; tags: Array<string> }
| { type: typeof ADMIN_USER_TAG_FAIL; error: unknown; accountId: string; tags: Array<string> }
| { type: typeof ADMIN_USER_UNTAG_REQUEST; accountId: string; tags: Array<string> }
| { type: typeof ADMIN_USER_UNTAG_SUCCESS; accountId: string; tags: Array<string> }
| { type: typeof ADMIN_USER_UNTAG_FAIL; error: unknown; accountId: string; tags: Array<string> }
| ReturnType<typeof setUserIndexQuery>
| { type: typeof ADMIN_USER_INDEX_FETCH_REQUEST }
| { type: typeof ADMIN_USER_INDEX_FETCH_SUCCESS; users: Array<AdminAccount>; total?: number; next: (() => Promise<PaginatedResponse<AdminAccount>>) | null; params?: AdminGetAccountsParams }
| { type: typeof ADMIN_USER_INDEX_FETCH_FAIL }
| { type: typeof ADMIN_USER_INDEX_EXPAND_REQUEST }
| { type: typeof ADMIN_USER_INDEX_EXPAND_SUCCESS; users: Array<AdminAccount>; total?: number; next: (() => Promise<PaginatedResponse<AdminAccount>>) | null; params: AdminGetAccountsParams | null }
| { type: typeof ADMIN_USER_INDEX_EXPAND_FAIL };
export {
ADMIN_CONFIG_FETCH_REQUEST,

View file

@ -39,7 +39,7 @@ const UserIndex: React.FC = () => {
updateQuery();
}, []);
const hasMore = items.length < total && !!next;
const hasMore = (total === undefined || items.length < total) && !!next;
const showLoading = isLoading && !items.length;

View file

@ -1,12 +1,14 @@
import type { AdminReport as BaseAdminReport } from 'pl-api';
const normalizeAdminReport = (report: BaseAdminReport) => ({
const normalizeAdminReport = ({
account, target_account, action_taken_by_account, assigned_account, statuses, ...report
}: BaseAdminReport) => ({
...report,
account_id: report.account?.id || null,
target_account_id: report.target_account?.id || null,
action_taken_by_account_id: report.action_taken_by_account?.id || null,
assigned_account_id: report.assigned_account?.id || null,
status_ids: report.statuses.map(status => status.id),
account_id: account?.id || null,
target_account_id: target_account?.id || null,
action_taken_by_account_id: action_taken_by_account?.id || null,
assigned_account_id: assigned_account?.id || null,
status_ids: statuses.map(status => status.id),
});
type AdminReport = ReturnType<typeof normalizeAdminReport>;

View file

@ -8,9 +8,9 @@ import {
ADMIN_USER_INDEX_FETCH_REQUEST,
ADMIN_USER_INDEX_FETCH_SUCCESS,
ADMIN_USER_INDEX_QUERY_SET,
type AdminActions,
} from 'pl-fe/actions/admin';
import type { AnyAction } from '@reduxjs/toolkit';
import type { AdminAccount, AdminGetAccountsParams, PaginatedResponse } from 'pl-api';
import type { APIEntity } from 'pl-fe/types/entities';
@ -18,7 +18,7 @@ type State = {
isLoading: boolean;
loaded: boolean;
items: Array<string>;
total: number;
total?: number;
page: number;
query: string;
next: (() => Promise<PaginatedResponse<AdminAccount>>) | null;
@ -36,7 +36,7 @@ const initialState: State = {
params: null,
};
const admin_user_index = (state: State = initialState, action: AnyAction): State => {
const admin_user_index = (state: State = initialState, action: AdminActions): State => {
switch (action.type) {
case ADMIN_USER_INDEX_QUERY_SET:
return create(state, draft => {
@ -47,7 +47,7 @@ const admin_user_index = (state: State = initialState, action: AnyAction): State
draft.isLoading = true;
draft.loaded = true;
draft.items = [];
draft.total = action.total;
draft.total = Infinity;
draft.page = 0;
draft.next = null;
});

View file

@ -10,12 +10,12 @@ import {
ADMIN_USER_DELETE_SUCCESS,
ADMIN_USER_APPROVE_REQUEST,
ADMIN_USER_APPROVE_SUCCESS,
type AdminActions,
} from 'pl-fe/actions/admin';
import { normalizeAdminReport, type AdminReport } from 'pl-fe/normalizers/admin-report';
import { normalizeAdminReport, type AdminReport as MinifiedReport } from 'pl-fe/normalizers/admin-report';
import type { AdminAccount, AdminGetAccountsParams, AdminReport as BaseAdminReport } from 'pl-api';
import type { AdminAccount, AdminGetAccountsParams, AdminReport } from 'pl-api';
import type { Config } from 'pl-fe/utils/config-db';
import type { AnyAction } from 'redux';
interface State {
reports: Record<string, MinifiedReport>;
@ -59,7 +59,7 @@ const minifyUser = (user: AdminAccount) => omit(user, ['account']);
type MinifiedUser = ReturnType<typeof minifyUser>;
const importUsers = (state: State, users: Array<AdminAccount>, params: AdminGetAccountsParams) => {
const importUsers = (state: State, users: Array<AdminAccount>, params?: AdminGetAccountsParams) => {
maybeImportUnapproved(state, users, params);
maybeImportLatest(state, users, params);
@ -80,16 +80,9 @@ const approveUser = (state: State, user: AdminAccount) => {
state.users[user.id] = normalizedUser;
};
const minifyReport = (report: AdminReport) => omit(
report,
['account', 'target_account', 'action_taken_by_account', 'assigned_account', 'statuses'],
);
type MinifiedReport = ReturnType<typeof minifyReport>;
const importReports = (state: State, reports: Array<BaseAdminReport>) => {
const importReports = (state: State, reports: Array<AdminReport>) => {
reports.forEach(report => {
const minifiedReport = minifyReport(normalizeAdminReport(report));
const minifiedReport = normalizeAdminReport(report);
if (!minifiedReport.action_taken) {
state.openReports = [...new Set([...state.openReports, report.id])];
}
@ -97,7 +90,7 @@ const importReports = (state: State, reports: Array<BaseAdminReport>) => {
});
};
const handleReportDiffs = (state: State, report: MinifiedReport) => {
const handleReportDiffs = (state: State, report: AdminReport) => {
// Note: the reports here aren't full report objects
// hence the need for a new function.
switch (report.action_taken) {
@ -113,7 +106,7 @@ const importConfigs = (state: State, configs: any) => {
state.configs = configs;
};
const admin = (state = initialState, action: AnyAction): State => {
const admin = (state = initialState, action: AdminActions): State => {
switch (action.type) {
case ADMIN_CONFIG_FETCH_SUCCESS:
case ADMIN_CONFIG_UPDATE_SUCCESS: