From 627a8105035192556cab2838643e06ec126293d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Wed, 27 Nov 2024 21:12:37 +0100 Subject: [PATCH] pl-fe: remove remaining AnyAction from reducers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-fe/src/actions/admin.ts | 105 ++++++++++++------ .../pl-fe/src/features/admin/user-index.tsx | 2 +- .../pl-fe/src/normalizers/admin-report.ts | 14 ++- .../pl-fe/src/reducers/admin-user-index.ts | 8 +- packages/pl-fe/src/reducers/admin.ts | 23 ++-- 5 files changed, 91 insertions(+), 61 deletions(-) diff --git a/packages/pl-fe/src/actions/admin.ts b/packages/pl-fe/src/actions/admin.ts index d0ee6bdb5..ee97042b0 100644 --- a/packages/pl-fe/src/actions/admin.ts +++ b/packages/pl-fe/src/actions/admin.ts @@ -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({ 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, accounts: [report.account?.account, report.target_account?.account] })); - dispatch({ type: ADMIN_REPORTS_FETCH_SUCCESS, reports: items, params }); + dispatch({ type: ADMIN_REPORTS_FETCH_SUCCESS, reports: items, params }); }); }).catch(error => { - dispatch({ type: ADMIN_REPORTS_FETCH_FAIL, error, params }); + dispatch({ 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({ type: ADMIN_REPORT_PATCH_REQUEST, reportId }); return getClient(state).admin.reports.resolveReport(reportId).then((report) => { - dispatch({ type: ADMIN_REPORT_PATCH_SUCCESS, report, reportId }); + dispatch({ type: ADMIN_REPORT_PATCH_SUCCESS, report, reportId }); }).catch(error => { - dispatch({ type: ADMIN_REPORT_PATCH_FAIL, error, reportId }); + dispatch({ 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({ 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({ 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({ 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({ 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({ type: ADMIN_USER_DELETE_REQUEST, accountId }); return getClient(getState).admin.accounts.deleteAccount(accountId) .then(() => { - dispatch({ type: ADMIN_USER_DELETE_SUCCESS, accountId }); + dispatch({ type: ADMIN_USER_DELETE_SUCCESS, accountId }); }).catch(error => { - dispatch({ type: ADMIN_USER_DELETE_FAIL, error, accountId }); + dispatch({ 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({ type: ADMIN_USER_APPROVE_REQUEST, accountId }); return getClient(state).admin.accounts.approveAccount(accountId) .then((user) => { - dispatch({ type: ADMIN_USER_APPROVE_SUCCESS, user, accountId }); + dispatch({ type: ADMIN_USER_APPROVE_SUCCESS, user, accountId }); }).catch(error => { - dispatch({ type: ADMIN_USER_APPROVE_FAIL, error, accountId }); + dispatch({ type: ADMIN_USER_APPROVE_FAIL, error, accountId }); }); }; const deleteStatus = (statusId: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: ADMIN_STATUS_DELETE_REQUEST, statusId }); + dispatch({ 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({ type: ADMIN_STATUS_DELETE_SUCCESS, statusId }); }).catch(error => { - return dispatch({ type: ADMIN_STATUS_DELETE_FAIL, error, statusId }); + return dispatch({ 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({ 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({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_SUCCESS, statusId, status }); }).catch(error => { - dispatch({ type: ADMIN_STATUS_TOGGLE_SENSITIVITY_FAIL, error, statusId }); + dispatch({ 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({ 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({ type: ADMIN_USER_TAG_SUCCESS, accountId, tags }); }).catch(error => { - dispatch({ type: ADMIN_USER_TAG_FAIL, error, accountId, tags }); + dispatch({ 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({ 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({ type: ADMIN_USER_UNTAG_SUCCESS, accountId, tags }); }).catch(error => { - dispatch({ type: ADMIN_USER_UNTAG_FAIL, error, accountId, tags }); + dispatch({ 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({ 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({ type: ADMIN_USER_INDEX_FETCH_SUCCESS, users: items, total, next, params }); }).catch(() => { - dispatch({ type: ADMIN_USER_INDEX_FETCH_FAIL }); + dispatch({ 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({ 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({ type: ADMIN_USER_INDEX_EXPAND_SUCCESS, users: items, total, next, params }); }).catch(() => { - dispatch({ type: ADMIN_USER_INDEX_EXPAND_FAIL }); + dispatch({ 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; 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; params?: AdminGetAccountsParams; next: (() => Promise>) | 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 } + | { type: typeof ADMIN_USER_TAG_SUCCESS; accountId: string; tags: Array } + | { type: typeof ADMIN_USER_TAG_FAIL; error: unknown; accountId: string; tags: Array } + | { type: typeof ADMIN_USER_UNTAG_REQUEST; accountId: string; tags: Array } + | { type: typeof ADMIN_USER_UNTAG_SUCCESS; accountId: string; tags: Array } + | { type: typeof ADMIN_USER_UNTAG_FAIL; error: unknown; accountId: string; tags: Array } + | ReturnType + | { type: typeof ADMIN_USER_INDEX_FETCH_REQUEST } + | { type: typeof ADMIN_USER_INDEX_FETCH_SUCCESS; users: Array; total?: number; next: (() => Promise>) | 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; total?: number; next: (() => Promise>) | null; params: AdminGetAccountsParams | null } + | { type: typeof ADMIN_USER_INDEX_EXPAND_FAIL }; export { ADMIN_CONFIG_FETCH_REQUEST, diff --git a/packages/pl-fe/src/features/admin/user-index.tsx b/packages/pl-fe/src/features/admin/user-index.tsx index e39d151b0..35a126cf5 100644 --- a/packages/pl-fe/src/features/admin/user-index.tsx +++ b/packages/pl-fe/src/features/admin/user-index.tsx @@ -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; diff --git a/packages/pl-fe/src/normalizers/admin-report.ts b/packages/pl-fe/src/normalizers/admin-report.ts index 8c7b64f4c..9d40f560a 100644 --- a/packages/pl-fe/src/normalizers/admin-report.ts +++ b/packages/pl-fe/src/normalizers/admin-report.ts @@ -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; diff --git a/packages/pl-fe/src/reducers/admin-user-index.ts b/packages/pl-fe/src/reducers/admin-user-index.ts index 0459cea4f..b02a89f78 100644 --- a/packages/pl-fe/src/reducers/admin-user-index.ts +++ b/packages/pl-fe/src/reducers/admin-user-index.ts @@ -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; - total: number; + total?: number; page: number; query: string; next: (() => Promise>) | 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; }); diff --git a/packages/pl-fe/src/reducers/admin.ts b/packages/pl-fe/src/reducers/admin.ts index 7531f4541..6e0be7d00 100644 --- a/packages/pl-fe/src/reducers/admin.ts +++ b/packages/pl-fe/src/reducers/admin.ts @@ -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; @@ -59,7 +59,7 @@ const minifyUser = (user: AdminAccount) => omit(user, ['account']); type MinifiedUser = ReturnType; -const importUsers = (state: State, users: Array, params: AdminGetAccountsParams) => { +const importUsers = (state: State, users: Array, 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; - -const importReports = (state: State, reports: Array) => { +const importReports = (state: State, reports: Array) => { 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) => { }); }; -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: