pl-fe: types

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-11-26 18:53:23 +01:00
parent dddac128fb
commit 6915ce4502
4 changed files with 53 additions and 29 deletions

View file

@ -69,23 +69,23 @@ const ADMIN_USER_INDEX_QUERY_SET = 'ADMIN_USER_INDEX_QUERY_SET' as const;
const fetchConfig = () => const fetchConfig = () =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: ADMIN_CONFIG_FETCH_REQUEST }); dispatch<AdminActions>({ type: ADMIN_CONFIG_FETCH_REQUEST });
return getClient(getState).admin.config.getPleromaConfig() return getClient(getState).admin.config.getPleromaConfig()
.then((data) => { .then((data) => {
dispatch({ type: ADMIN_CONFIG_FETCH_SUCCESS, configs: data.configs, needsReboot: data.need_reboot }); dispatch<AdminActions>({ type: ADMIN_CONFIG_FETCH_SUCCESS, configs: data.configs, needsReboot: data.need_reboot });
}).catch(error => { }).catch(error => {
dispatch({ type: ADMIN_CONFIG_FETCH_FAIL, error }); dispatch<AdminActions>({ type: ADMIN_CONFIG_FETCH_FAIL, error });
}); });
}; };
const updateConfig = (configs: PleromaConfig['configs']) => const updateConfig = (configs: PleromaConfig['configs']) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: ADMIN_CONFIG_UPDATE_REQUEST, configs }); dispatch<AdminActions>({ type: ADMIN_CONFIG_UPDATE_REQUEST, configs });
return getClient(getState).admin.config.updatePleromaConfig(configs) return getClient(getState).admin.config.updatePleromaConfig(configs)
.then((data) => { .then((data) => {
dispatch({ type: ADMIN_CONFIG_UPDATE_SUCCESS, configs: data.configs, needsReboot: data.need_reboot }); dispatch<AdminActions>({ type: ADMIN_CONFIG_UPDATE_SUCCESS, configs: data.configs, needsReboot: data.need_reboot });
}).catch(error => { }).catch(error => {
dispatch({ type: ADMIN_CONFIG_UPDATE_FAIL, error, configs }); dispatch<AdminActions>({ 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 { export {
ADMIN_CONFIG_FETCH_REQUEST, ADMIN_CONFIG_FETCH_REQUEST,
ADMIN_CONFIG_FETCH_SUCCESS, ADMIN_CONFIG_FETCH_SUCCESS,
@ -378,4 +386,5 @@ export {
setUserIndexQuery, setUserIndexQuery,
fetchUserIndex, fetchUserIndex,
expandUserIndex, expandUserIndex,
type AdminActions,
}; };

View file

@ -5,7 +5,7 @@ import { isLoggedIn } from 'pl-fe/utils/auth';
import { getClient } from '../api'; 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'; import type { AppDispatch, RootState } from 'pl-fe/store';
const FILTERS_FETCH_REQUEST = 'FILTERS_FETCH_REQUEST' as const; const FILTERS_FETCH_REQUEST = 'FILTERS_FETCH_REQUEST' as const;
@ -39,16 +39,16 @@ const fetchFilters = () =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
if (!isLoggedIn(getState)) return; if (!isLoggedIn(getState)) return;
dispatch({ dispatch<FiltersAction>({
type: FILTERS_FETCH_REQUEST, type: FILTERS_FETCH_REQUEST,
}); });
return getClient(getState).filtering.getFilters() return getClient(getState).filtering.getFilters()
.then((data) => dispatch({ .then((data) => dispatch<FiltersAction>({
type: FILTERS_FETCH_SUCCESS, type: FILTERS_FETCH_SUCCESS,
filters: data, filters: data,
})) }))
.catch(err => dispatch({ .catch(err => dispatch<FiltersAction>({
type: FILTERS_FETCH_FAIL, type: FILTERS_FETCH_FAIL,
err, err,
skipAlert: true, skipAlert: true,
@ -57,11 +57,11 @@ const fetchFilters = () =>
const fetchFilter = (filterId: string) => const fetchFilter = (filterId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: FILTER_FETCH_REQUEST }); dispatch<FiltersAction>({ type: FILTER_FETCH_REQUEST });
return getClient(getState).filtering.getFilter(filterId) return getClient(getState).filtering.getFilter(filterId)
.then((data) => { .then((data) => {
dispatch({ dispatch<FiltersAction>({
type: FILTER_FETCH_SUCCESS, type: FILTER_FETCH_SUCCESS,
filter: data, filter: data,
}); });
@ -69,7 +69,7 @@ const fetchFilter = (filterId: string) =>
return data; return data;
}) })
.catch(err => { .catch(err => {
dispatch({ dispatch<FiltersAction>({
type: FILTER_FETCH_FAIL, type: FILTER_FETCH_FAIL,
err, err,
skipAlert: true, skipAlert: true,
@ -79,7 +79,7 @@ const fetchFilter = (filterId: string) =>
const createFilter = (title: string, expires_in: number | undefined, context: Array<FilterContext>, hide: boolean, keywords_attributes: FilterKeywords) => const createFilter = (title: string, expires_in: number | undefined, context: Array<FilterContext>, hide: boolean, keywords_attributes: FilterKeywords) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: FILTERS_CREATE_REQUEST }); dispatch<FiltersAction>({ type: FILTERS_CREATE_REQUEST });
return getClient(getState).filtering.createFilter({ return getClient(getState).filtering.createFilter({
title, title,
@ -88,18 +88,18 @@ const createFilter = (title: string, expires_in: number | undefined, context: Ar
expires_in, expires_in,
keywords_attributes, keywords_attributes,
}).then(response => { }).then(response => {
dispatch({ type: FILTERS_CREATE_SUCCESS, filter: response }); dispatch<FiltersAction>({ type: FILTERS_CREATE_SUCCESS, filter: response });
toast.success(messages.added); toast.success(messages.added);
return response; return response;
}).catch(error => { }).catch(error => {
dispatch({ type: FILTERS_CREATE_FAIL, error }); dispatch<FiltersAction>({ type: FILTERS_CREATE_FAIL, error });
}); });
}; };
const updateFilter = (filterId: string, title: string, expires_in: number | undefined, context: Array<FilterContext>, hide: boolean, keywords_attributes: FilterKeywords) => const updateFilter = (filterId: string, title: string, expires_in: number | undefined, context: Array<FilterContext>, hide: boolean, keywords_attributes: FilterKeywords) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: FILTERS_UPDATE_REQUEST }); dispatch<FiltersAction>({ type: FILTERS_UPDATE_REQUEST });
return getClient(getState).filtering.updateFilter(filterId, { return getClient(getState).filtering.updateFilter(filterId, {
title, title,
@ -108,28 +108,45 @@ const updateFilter = (filterId: string, title: string, expires_in: number | unde
expires_in, expires_in,
keywords_attributes, keywords_attributes,
}).then(response => { }).then(response => {
dispatch({ type: FILTERS_UPDATE_SUCCESS, filter: response }); dispatch<FiltersAction>({ type: FILTERS_UPDATE_SUCCESS, filter: response });
toast.success(messages.added); toast.success(messages.added);
return response; return response;
}).catch(error => { }).catch(error => {
dispatch({ type: FILTERS_UPDATE_FAIL, filterId, error }); dispatch<FiltersAction>({ type: FILTERS_UPDATE_FAIL, filterId, error });
}); });
}; };
const deleteFilter = (filterId: string) => const deleteFilter = (filterId: string) =>
(dispatch: AppDispatch, getState: () => RootState) => { (dispatch: AppDispatch, getState: () => RootState) => {
dispatch({ type: FILTERS_DELETE_REQUEST }); dispatch<FiltersAction>({ type: FILTERS_DELETE_REQUEST });
return getClient(getState).filtering.deleteFilter(filterId).then(response => { return getClient(getState).filtering.deleteFilter(filterId).then(response => {
dispatch({ type: FILTERS_DELETE_SUCCESS, filterId }); dispatch<FiltersAction>({ type: FILTERS_DELETE_SUCCESS, filterId });
toast.success(messages.removed); toast.success(messages.removed);
return response; return response;
}).catch(error => { }).catch(error => {
dispatch({ type: FILTERS_DELETE_FAIL, filterId, error }); dispatch<FiltersAction>({ type: FILTERS_DELETE_FAIL, filterId, error });
}); });
}; };
type FiltersAction =
| { type: typeof FILTERS_FETCH_REQUEST }
| { type: typeof FILTERS_FETCH_SUCCESS; filters: Array<Filter> }
| { 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 { export {
FILTERS_FETCH_REQUEST, FILTERS_FETCH_REQUEST,
FILTERS_FETCH_SUCCESS, FILTERS_FETCH_SUCCESS,
@ -151,4 +168,5 @@ export {
createFilter, createFilter,
updateFilter, updateFilter,
deleteFilter, deleteFilter,
type FiltersAction,
}; };

View file

@ -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 { Filter } from 'pl-api';
import type { AnyAction } from 'redux';
type State = Array<Filter>; type State = Array<Filter>;
const filters = (state: State = [], action: AnyAction): State => { const filters = (state: State = [], action: FiltersAction): State => {
switch (action.type) { switch (action.type) {
case FILTERS_FETCH_SUCCESS: case FILTERS_FETCH_SUCCESS:
return action.filters; return action.filters;

View file

@ -2,14 +2,12 @@ import { create } from 'mutative';
import { type Instance, instanceSchema, PleromaConfig } from 'pl-api'; import { type Instance, instanceSchema, PleromaConfig } from 'pl-api';
import * as v from 'valibot'; 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 { 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 { PLEROMA_PRELOAD_IMPORT, type PreloadAction } from 'pl-fe/actions/preload';
import KVStore from 'pl-fe/storage/kv-store'; import KVStore from 'pl-fe/storage/kv-store';
import ConfigDB from 'pl-fe/utils/config-db'; import ConfigDB from 'pl-fe/utils/config-db';
import type { AnyAction } from 'redux';
const initialState: State = v.parse(instanceSchema, {}); const initialState: State = v.parse(instanceSchema, {});
type State = Instance; 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) { switch (action.type) {
case PLEROMA_PRELOAD_IMPORT: case PLEROMA_PRELOAD_IMPORT:
return create(state, (draft) => preloadImport(draft, action, '/api/v1/instance')); return create(state, (draft) => preloadImport(draft, action, '/api/v1/instance'));