From 2379bfe3ea9f1612b865a45c547b9f96059d22ba Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 26 Aug 2024 18:38:34 +0200 Subject: [PATCH] [Glitch] Fix spurious loading bar middleware usage Port e38ce3beb7b83f43ef6e6f144c19b186c393910c to glitch-soc Signed-off-by: Claire --- .../flavours/glitch/actions/account_notes.ts | 1 - .../glitch/store/middlewares/loading_bar.ts | 5 +++-- .../flavours/glitch/store/typed_functions.ts | 19 ++++++++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/javascript/flavours/glitch/actions/account_notes.ts b/app/javascript/flavours/glitch/actions/account_notes.ts index a71b342b06..162fa67505 100644 --- a/app/javascript/flavours/glitch/actions/account_notes.ts +++ b/app/javascript/flavours/glitch/actions/account_notes.ts @@ -6,5 +6,4 @@ export const submitAccountNote = createDataLoadingThunk( ({ accountId, note }: { accountId: string; note: string }) => apiSubmitAccountNote(accountId, note), (relationship) => ({ relationship }), - { skipLoading: true }, ); diff --git a/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts b/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts index d259be899b..33d99f9852 100644 --- a/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts +++ b/app/javascript/flavours/glitch/store/middlewares/loading_bar.ts @@ -48,8 +48,9 @@ export const loadingBarMiddleware = ( let isRejected = false; if ( - isAsyncThunkAction(action) - // TODO: once we get the first use-case for it, add a check for skipLoading + isAsyncThunkAction(action) && + 'useLoadingBar' in action.meta && + action.meta.useLoadingBar ) { if (isThunkActionPending(action)) isPending = true; else if (isThunkActionFulfilled(action)) isFulfilled = true; diff --git a/app/javascript/flavours/glitch/store/typed_functions.ts b/app/javascript/flavours/glitch/store/typed_functions.ts index cd0f95cef9..544d619682 100644 --- a/app/javascript/flavours/glitch/store/typed_functions.ts +++ b/app/javascript/flavours/glitch/store/typed_functions.ts @@ -15,7 +15,7 @@ export interface AsyncThunkRejectValue { } interface AppMeta { - skipLoading?: boolean; + useLoadingBar?: boolean; } export const createAppAsyncThunk = createAsyncThunk.withTypes<{ @@ -34,7 +34,7 @@ interface AppThunkConfig { type AppThunkApi = Pick, 'getState' | 'dispatch'>; interface AppThunkOptions { - skipLoading?: boolean; + useLoadingBar?: boolean; } const createBaseAsyncThunk = createAsyncThunk.withTypes(); @@ -54,15 +54,20 @@ export function createThunk( const result = await creator(arg, { dispatch, getState }); return fulfillWithValue(result, { - skipLoading: options.skipLoading, + useLoadingBar: options.useLoadingBar, }); } catch (error) { - return rejectWithValue({ error }, { skipLoading: true }); + return rejectWithValue( + { error }, + { + useLoadingBar: options.useLoadingBar, + }, + ); } }, { getPendingMeta() { - if (options.skipLoading) return { skipLoading: true }; + if (options.useLoadingBar) return { useLoadingBar: true }; return {}; }, }, @@ -148,7 +153,7 @@ export function createDataLoadingThunk< * You can also omit this parameter and pass `thunkOptions` directly * @param maybeThunkOptions * Additional Mastodon specific options for the thunk. Currently supports: - * - `skipLoading` to avoid showing the loading bar when the request is in progress + * - `useLoadingBar` to display a loading bar while this action is pending. Defaults to true. * @returns The created thunk */ export function createDataLoadingThunk< @@ -198,6 +203,6 @@ export function createDataLoadingThunk< return undefined as Returned; else return result; }, - thunkOptions, + { useLoadingBar: thunkOptions?.useLoadingBar ?? true }, ); }