0
0
Fork 0

Convert notification requests actions and reducers to Typescript (#31866)

This commit is contained in:
Claire 2024-09-16 11:54:03 +02:00 committed by GitHub
parent d5cf27e667
commit c0eda832f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 585 additions and 492 deletions

View file

@ -33,8 +33,12 @@ interface AppThunkConfig {
}
type AppThunkApi = Pick<GetThunkAPI<AppThunkConfig>, 'getState' | 'dispatch'>;
interface AppThunkOptions {
interface AppThunkOptions<Arg> {
useLoadingBar?: boolean;
condition?: (
arg: Arg,
{ getState }: { getState: AppThunkApi['getState'] },
) => boolean;
}
const createBaseAsyncThunk = createAsyncThunk.withTypes<AppThunkConfig>();
@ -42,7 +46,7 @@ const createBaseAsyncThunk = createAsyncThunk.withTypes<AppThunkConfig>();
export function createThunk<Arg = void, Returned = void>(
name: string,
creator: (arg: Arg, api: AppThunkApi) => Returned | Promise<Returned>,
options: AppThunkOptions = {},
options: AppThunkOptions<Arg> = {},
) {
return createBaseAsyncThunk(
name,
@ -70,6 +74,7 @@ export function createThunk<Arg = void, Returned = void>(
if (options.useLoadingBar) return { useLoadingBar: true };
return {};
},
condition: options.condition,
},
);
}
@ -96,7 +101,7 @@ type ArgsType = Record<string, unknown> | undefined;
export function createDataLoadingThunk<LoadDataResult, Args extends ArgsType>(
name: string,
loadData: (args: Args) => Promise<LoadDataResult>,
thunkOptions?: AppThunkOptions,
thunkOptions?: AppThunkOptions<Args>,
): ReturnType<typeof createThunk<Args, LoadDataResult>>;
// Overload when the `onData` method returns discardLoadDataInPayload, then the payload is empty
@ -104,17 +109,19 @@ export function createDataLoadingThunk<LoadDataResult, Args extends ArgsType>(
name: string,
loadData: LoadData<Args, LoadDataResult>,
onDataOrThunkOptions?:
| AppThunkOptions
| AppThunkOptions<Args>
| OnData<Args, LoadDataResult, DiscardLoadData>,
thunkOptions?: AppThunkOptions,
thunkOptions?: AppThunkOptions<Args>,
): ReturnType<typeof createThunk<Args, void>>;
// Overload when the `onData` method returns nothing, then the mayload is the `onData` result
export function createDataLoadingThunk<LoadDataResult, Args extends ArgsType>(
name: string,
loadData: LoadData<Args, LoadDataResult>,
onDataOrThunkOptions?: AppThunkOptions | OnData<Args, LoadDataResult, void>,
thunkOptions?: AppThunkOptions,
onDataOrThunkOptions?:
| AppThunkOptions<Args>
| OnData<Args, LoadDataResult, void>,
thunkOptions?: AppThunkOptions<Args>,
): ReturnType<typeof createThunk<Args, LoadDataResult>>;
// Overload when there is an `onData` method returning something
@ -126,9 +133,9 @@ export function createDataLoadingThunk<
name: string,
loadData: LoadData<Args, LoadDataResult>,
onDataOrThunkOptions?:
| AppThunkOptions
| AppThunkOptions<Args>
| OnData<Args, LoadDataResult, Returned>,
thunkOptions?: AppThunkOptions,
thunkOptions?: AppThunkOptions<Args>,
): ReturnType<typeof createThunk<Args, Returned>>;
/**
@ -154,6 +161,7 @@ export function createDataLoadingThunk<
* @param maybeThunkOptions
* Additional Mastodon specific options for the thunk. Currently supports:
* - `useLoadingBar` to display a loading bar while this action is pending. Defaults to true.
* - `condition` is passed to `createAsyncThunk` (https://redux-toolkit.js.org/api/createAsyncThunk#canceling-before-execution)
* @returns The created thunk
*/
export function createDataLoadingThunk<
@ -164,12 +172,12 @@ export function createDataLoadingThunk<
name: string,
loadData: LoadData<Args, LoadDataResult>,
onDataOrThunkOptions?:
| AppThunkOptions
| AppThunkOptions<Args>
| OnData<Args, LoadDataResult, Returned>,
maybeThunkOptions?: AppThunkOptions,
maybeThunkOptions?: AppThunkOptions<Args>,
) {
let onData: OnData<Args, LoadDataResult, Returned> | undefined;
let thunkOptions: AppThunkOptions | undefined;
let thunkOptions: AppThunkOptions<Args> | undefined;
if (typeof onDataOrThunkOptions === 'function') onData = onDataOrThunkOptions;
else if (typeof onDataOrThunkOptions === 'object')
@ -203,6 +211,9 @@ export function createDataLoadingThunk<
return undefined as Returned;
else return result;
},
{ useLoadingBar: thunkOptions?.useLoadingBar ?? true },
{
useLoadingBar: thunkOptions?.useLoadingBar ?? true,
condition: thunkOptions?.condition,
},
);
}