diff --git a/packages/pl-fe/src/actions/admin.ts b/packages/pl-fe/src/actions/admin.ts index 006c3a567..d0ee6bdb5 100644 --- a/packages/pl-fe/src/actions/admin.ts +++ b/packages/pl-fe/src/actions/admin.ts @@ -69,23 +69,23 @@ const ADMIN_USER_INDEX_QUERY_SET = 'ADMIN_USER_INDEX_QUERY_SET' as const; const fetchConfig = () => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: ADMIN_CONFIG_FETCH_REQUEST }); + dispatch({ type: ADMIN_CONFIG_FETCH_REQUEST }); return getClient(getState).admin.config.getPleromaConfig() .then((data) => { - dispatch({ type: ADMIN_CONFIG_FETCH_SUCCESS, configs: data.configs, needsReboot: data.need_reboot }); + dispatch({ type: ADMIN_CONFIG_FETCH_SUCCESS, configs: data.configs, needsReboot: data.need_reboot }); }).catch(error => { - dispatch({ type: ADMIN_CONFIG_FETCH_FAIL, error }); + dispatch({ type: ADMIN_CONFIG_FETCH_FAIL, error }); }); }; const updateConfig = (configs: PleromaConfig['configs']) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: ADMIN_CONFIG_UPDATE_REQUEST, configs }); + dispatch({ type: ADMIN_CONFIG_UPDATE_REQUEST, configs }); return getClient(getState).admin.config.updatePleromaConfig(configs) .then((data) => { - dispatch({ type: ADMIN_CONFIG_UPDATE_SUCCESS, configs: data.configs, needsReboot: data.need_reboot }); + dispatch({ type: ADMIN_CONFIG_UPDATE_SUCCESS, configs: data.configs, needsReboot: data.need_reboot }); }).catch(error => { - dispatch({ type: ADMIN_CONFIG_UPDATE_FAIL, error, configs }); + dispatch({ type: ADMIN_CONFIG_UPDATE_FAIL, error, configs }); }); }; @@ -312,6 +312,14 @@ const expandUserIndex = () => }); }; +type AdminActions = + | { type: typeof ADMIN_CONFIG_FETCH_REQUEST } + | { type: typeof ADMIN_CONFIG_FETCH_SUCCESS; configs: PleromaConfig['configs']; needsReboot: boolean } + | { type: typeof ADMIN_CONFIG_FETCH_FAIL; error: unknown } + | { 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'] } + export { ADMIN_CONFIG_FETCH_REQUEST, ADMIN_CONFIG_FETCH_SUCCESS, @@ -378,4 +386,5 @@ export { setUserIndexQuery, fetchUserIndex, expandUserIndex, + type AdminActions, }; diff --git a/packages/pl-fe/src/actions/filters.ts b/packages/pl-fe/src/actions/filters.ts index 1499664ea..2ac06b2f4 100644 --- a/packages/pl-fe/src/actions/filters.ts +++ b/packages/pl-fe/src/actions/filters.ts @@ -5,7 +5,7 @@ import { isLoggedIn } from 'pl-fe/utils/auth'; import { getClient } from '../api'; -import type { FilterContext } from 'pl-api'; +import type { Filter, FilterContext } from 'pl-api'; import type { AppDispatch, RootState } from 'pl-fe/store'; const FILTERS_FETCH_REQUEST = 'FILTERS_FETCH_REQUEST' as const; @@ -39,16 +39,16 @@ const fetchFilters = () => (dispatch: AppDispatch, getState: () => RootState) => { if (!isLoggedIn(getState)) return; - dispatch({ + dispatch({ type: FILTERS_FETCH_REQUEST, }); return getClient(getState).filtering.getFilters() - .then((data) => dispatch({ + .then((data) => dispatch({ type: FILTERS_FETCH_SUCCESS, filters: data, })) - .catch(err => dispatch({ + .catch(err => dispatch({ type: FILTERS_FETCH_FAIL, err, skipAlert: true, @@ -57,11 +57,11 @@ const fetchFilters = () => const fetchFilter = (filterId: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: FILTER_FETCH_REQUEST }); + dispatch({ type: FILTER_FETCH_REQUEST }); return getClient(getState).filtering.getFilter(filterId) .then((data) => { - dispatch({ + dispatch({ type: FILTER_FETCH_SUCCESS, filter: data, }); @@ -69,7 +69,7 @@ const fetchFilter = (filterId: string) => return data; }) .catch(err => { - dispatch({ + dispatch({ type: FILTER_FETCH_FAIL, err, skipAlert: true, @@ -79,7 +79,7 @@ const fetchFilter = (filterId: string) => const createFilter = (title: string, expires_in: number | undefined, context: Array, hide: boolean, keywords_attributes: FilterKeywords) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: FILTERS_CREATE_REQUEST }); + dispatch({ type: FILTERS_CREATE_REQUEST }); return getClient(getState).filtering.createFilter({ title, @@ -88,18 +88,18 @@ const createFilter = (title: string, expires_in: number | undefined, context: Ar expires_in, keywords_attributes, }).then(response => { - dispatch({ type: FILTERS_CREATE_SUCCESS, filter: response }); + dispatch({ type: FILTERS_CREATE_SUCCESS, filter: response }); toast.success(messages.added); return response; }).catch(error => { - dispatch({ type: FILTERS_CREATE_FAIL, error }); + dispatch({ type: FILTERS_CREATE_FAIL, error }); }); }; const updateFilter = (filterId: string, title: string, expires_in: number | undefined, context: Array, hide: boolean, keywords_attributes: FilterKeywords) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: FILTERS_UPDATE_REQUEST }); + dispatch({ type: FILTERS_UPDATE_REQUEST }); return getClient(getState).filtering.updateFilter(filterId, { title, @@ -108,28 +108,45 @@ const updateFilter = (filterId: string, title: string, expires_in: number | unde expires_in, keywords_attributes, }).then(response => { - dispatch({ type: FILTERS_UPDATE_SUCCESS, filter: response }); + dispatch({ type: FILTERS_UPDATE_SUCCESS, filter: response }); toast.success(messages.added); return response; }).catch(error => { - dispatch({ type: FILTERS_UPDATE_FAIL, filterId, error }); + dispatch({ type: FILTERS_UPDATE_FAIL, filterId, error }); }); }; const deleteFilter = (filterId: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: FILTERS_DELETE_REQUEST }); + dispatch({ type: FILTERS_DELETE_REQUEST }); return getClient(getState).filtering.deleteFilter(filterId).then(response => { - dispatch({ type: FILTERS_DELETE_SUCCESS, filterId }); + dispatch({ type: FILTERS_DELETE_SUCCESS, filterId }); toast.success(messages.removed); return response; }).catch(error => { - dispatch({ type: FILTERS_DELETE_FAIL, filterId, error }); + dispatch({ type: FILTERS_DELETE_FAIL, filterId, error }); }); }; +type FiltersAction = + | { type: typeof FILTERS_FETCH_REQUEST } + | { type: typeof FILTERS_FETCH_SUCCESS; filters: Array } + | { type: typeof FILTERS_FETCH_FAIL; error: unknown; skipAlert: true } + | { type: typeof FILTER_FETCH_REQUEST } + | { type: typeof FILTER_FETCH_SUCCESS; filter: Filter } + | { type: typeof FILTER_FETCH_FAIL; error: unknown; skipAlert: true } + | { type: typeof FILTERS_CREATE_REQUEST } + | { type: typeof FILTERS_CREATE_SUCCESS; filter: Filter } + | { type: typeof FILTERS_CREATE_FAIL; error: unknown } + | { type: typeof FILTERS_UPDATE_REQUEST } + | { type: typeof FILTERS_UPDATE_SUCCESS; filter: Filter } + | { type: typeof FILTERS_UPDATE_FAIL; filterId: string; error: unknown } + | { type: typeof FILTERS_DELETE_REQUEST } + | { type: typeof FILTERS_DELETE_SUCCESS; filterId: string } + | { type: typeof FILTERS_DELETE_FAIL; filterId: string; error: unknown } + export { FILTERS_FETCH_REQUEST, FILTERS_FETCH_SUCCESS, @@ -151,4 +168,5 @@ export { createFilter, updateFilter, deleteFilter, + type FiltersAction, }; diff --git a/packages/pl-fe/src/reducers/filters.ts b/packages/pl-fe/src/reducers/filters.ts index 11fbdc5be..17aed76f3 100644 --- a/packages/pl-fe/src/reducers/filters.ts +++ b/packages/pl-fe/src/reducers/filters.ts @@ -1,11 +1,10 @@ -import { FILTERS_FETCH_SUCCESS } from '../actions/filters'; +import { FILTERS_FETCH_SUCCESS, type FiltersAction } from '../actions/filters'; import type { Filter } from 'pl-api'; -import type { AnyAction } from 'redux'; type State = Array; -const filters = (state: State = [], action: AnyAction): State => { +const filters = (state: State = [], action: FiltersAction): State => { switch (action.type) { case FILTERS_FETCH_SUCCESS: return action.filters; diff --git a/packages/pl-fe/src/reducers/instance.ts b/packages/pl-fe/src/reducers/instance.ts index 2822663b4..9d4475dc5 100644 --- a/packages/pl-fe/src/reducers/instance.ts +++ b/packages/pl-fe/src/reducers/instance.ts @@ -2,14 +2,12 @@ import { create } from 'mutative'; import { type Instance, instanceSchema, PleromaConfig } from 'pl-api'; import * as v from 'valibot'; -import { ADMIN_CONFIG_UPDATE_REQUEST, ADMIN_CONFIG_UPDATE_SUCCESS } from 'pl-fe/actions/admin'; +import { ADMIN_CONFIG_UPDATE_REQUEST, ADMIN_CONFIG_UPDATE_SUCCESS, type AdminActions } from 'pl-fe/actions/admin'; import { INSTANCE_FETCH_FAIL, INSTANCE_FETCH_SUCCESS, type InstanceAction } from 'pl-fe/actions/instance'; import { PLEROMA_PRELOAD_IMPORT, type PreloadAction } from 'pl-fe/actions/preload'; import KVStore from 'pl-fe/storage/kv-store'; import ConfigDB from 'pl-fe/utils/config-db'; -import type { AnyAction } from 'redux'; - const initialState: State = v.parse(instanceSchema, {}); type State = Instance; @@ -85,7 +83,7 @@ const handleInstanceFetchFail = (state: State, error: any) => { } }; -const instance = (state = initialState, action: AnyAction | InstanceAction | PreloadAction): State => { +const instance = (state = initialState, action: AdminActions | InstanceAction | PreloadAction): State => { switch (action.type) { case PLEROMA_PRELOAD_IMPORT: return create(state, (draft) => preloadImport(draft, action, '/api/v1/instance'));