From e32bfad88a76a1acd42e56c0ad2f83cdfe535aff Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Thu, 23 May 2024 20:22:42 +0200 Subject: [PATCH] [Glitch] Fix `createDataLoadingThunk` and related actions Port b6fd14f0e2842eca269ef8962e3c5bd560a76357 to glitch-soc Signed-off-by: Claire --- .../flavours/glitch/actions/account_notes.ts | 3 ++- .../glitch/actions/interactions_typed.ts | 11 ++++++--- app/javascript/flavours/glitch/api.ts | 6 ++--- .../flavours/glitch/api/accounts.ts | 2 +- .../glitch/containers/status_container.js | 4 ++-- .../containers/account_note_container.js | 2 +- .../containers/notification_container.js | 4 ++-- .../picture_in_picture/components/footer.jsx | 4 ++-- .../containers/detailed_status_container.js | 4 ++-- .../flavours/glitch/features/status/index.jsx | 4 ++-- .../flavours/glitch/reducers/statuses.js | 8 +++---- .../flavours/glitch/store/typed_functions.ts | 24 +++++++++---------- 12 files changed, 41 insertions(+), 35 deletions(-) diff --git a/app/javascript/flavours/glitch/actions/account_notes.ts b/app/javascript/flavours/glitch/actions/account_notes.ts index 9b05ae06c2..a71b342b06 100644 --- a/app/javascript/flavours/glitch/actions/account_notes.ts +++ b/app/javascript/flavours/glitch/actions/account_notes.ts @@ -3,7 +3,8 @@ import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions'; export const submitAccountNote = createDataLoadingThunk( 'account_note/submit', - (accountId: string, note: string) => apiSubmitAccountNote(accountId, note), + ({ accountId, note }: { accountId: string; note: string }) => + apiSubmitAccountNote(accountId, note), (relationship) => ({ relationship }), { skipLoading: true }, ); diff --git a/app/javascript/flavours/glitch/actions/interactions_typed.ts b/app/javascript/flavours/glitch/actions/interactions_typed.ts index 30ae270ea1..075fc242e4 100644 --- a/app/javascript/flavours/glitch/actions/interactions_typed.ts +++ b/app/javascript/flavours/glitch/actions/interactions_typed.ts @@ -6,8 +6,13 @@ import { importFetchedStatus } from './importer'; export const reblog = createDataLoadingThunk( 'status/reblog', - (statusId: string, visibility: StatusVisibility) => - apiReblog(statusId, visibility), + ({ + statusId, + visibility, + }: { + statusId: string; + visibility: StatusVisibility; + }) => apiReblog(statusId, visibility), (data, { dispatch, discardLoadData }) => { // The reblog API method returns a new status wrapped around the original. In this case we are only // interested in how the original is modified, hence passing it skipping the wrapper @@ -20,7 +25,7 @@ export const reblog = createDataLoadingThunk( export const unreblog = createDataLoadingThunk( 'status/unreblog', - (statusId: string) => apiUnreblog(statusId), + ({ statusId }: { statusId: string }) => apiUnreblog(statusId), (data, { dispatch, discardLoadData }) => { dispatch(importFetchedStatus(data)); diff --git a/app/javascript/flavours/glitch/api.ts b/app/javascript/flavours/glitch/api.ts index 4e5ccef08c..e133125a29 100644 --- a/app/javascript/flavours/glitch/api.ts +++ b/app/javascript/flavours/glitch/api.ts @@ -62,12 +62,12 @@ export default function api(withAuthorization = true) { export async function apiRequest( method: Method, url: string, - params?: unknown, + params?: Record, ) { const { data } = await api().request({ method, - url, - params, + url: '/api/' + url, + data: params, }); return data; diff --git a/app/javascript/flavours/glitch/api/accounts.ts b/app/javascript/flavours/glitch/api/accounts.ts index 19abc849b7..346b3bc38c 100644 --- a/app/javascript/flavours/glitch/api/accounts.ts +++ b/app/javascript/flavours/glitch/api/accounts.ts @@ -2,6 +2,6 @@ import { apiRequest } from 'flavours/glitch/api'; import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships'; export const apiSubmitAccountNote = (id: string, value: string) => - apiRequest('post', `/api/v1/accounts/${id}/note`, { + apiRequest('post', `v1/accounts/${id}/note`, { comment: value, }); diff --git a/app/javascript/flavours/glitch/containers/status_container.js b/app/javascript/flavours/glitch/containers/status_container.js index 6ae800c037..1430a0c845 100644 --- a/app/javascript/flavours/glitch/containers/status_container.js +++ b/app/javascript/flavours/glitch/containers/status_container.js @@ -115,9 +115,9 @@ const mapDispatchToProps = (dispatch, { intl, contextType }) => ({ onModalReblog (status, privacy) { if (status.get('reblogged')) { - dispatch(unreblog(status.id)); + dispatch(unreblog({ statusId: status.get('id') })); } else { - dispatch(reblog(status.id, privacy)); + dispatch(reblog({ statusId: status.get('id'), visibility: privacy })); } }, diff --git a/app/javascript/flavours/glitch/features/account/containers/account_note_container.js b/app/javascript/flavours/glitch/features/account/containers/account_note_container.js index 2fd7d56735..135e772f3e 100644 --- a/app/javascript/flavours/glitch/features/account/containers/account_note_container.js +++ b/app/javascript/flavours/glitch/features/account/containers/account_note_container.js @@ -11,7 +11,7 @@ const mapStateToProps = (state, { account }) => ({ const mapDispatchToProps = (dispatch, { account }) => ({ onSave (value) { - dispatch(submitAccountNote(account.get('id'), value)); + dispatch(submitAccountNote({ accountId: account.get('id'), note: value })); }, }); diff --git a/app/javascript/flavours/glitch/features/notifications/containers/notification_container.js b/app/javascript/flavours/glitch/features/notifications/containers/notification_container.js index 570f468157..338d27d8d6 100644 --- a/app/javascript/flavours/glitch/features/notifications/containers/notification_container.js +++ b/app/javascript/flavours/glitch/features/notifications/containers/notification_container.js @@ -36,12 +36,12 @@ const mapDispatchToProps = dispatch => ({ }, onModalReblog (status, privacy) { - dispatch(reblog(status.id, privacy)); + dispatch(reblog({ statusId: status.get('id'), visibility: privacy })); }, onReblog (status, e) { if (status.get('reblogged')) { - dispatch(unreblog(status.id)); + dispatch(unreblog({ statusId: status.get('id') })); } else { if (e.shiftKey || !boostModal) { this.onModalReblog(status); diff --git a/app/javascript/flavours/glitch/features/picture_in_picture/components/footer.jsx b/app/javascript/flavours/glitch/features/picture_in_picture/components/footer.jsx index bbcb6d46ba..13ad86f801 100644 --- a/app/javascript/flavours/glitch/features/picture_in_picture/components/footer.jsx +++ b/app/javascript/flavours/glitch/features/picture_in_picture/components/footer.jsx @@ -125,7 +125,7 @@ class Footer extends ImmutablePureComponent { _performReblog = (status, privacy) => { const { dispatch } = this.props; - dispatch(reblog(status.id, privacy)); + dispatch(reblog({ statusId: status.get('id'), visibility: privacy })); }; handleReblogClick = e => { @@ -134,7 +134,7 @@ class Footer extends ImmutablePureComponent { if (signedIn) { if (status.get('reblogged')) { - dispatch(unreblog(status.id)); + dispatch(unreblog({ statusId: status.get('id') })); } else if ((e && e.shiftKey) || !boostModal) { this._performReblog(status); } else { diff --git a/app/javascript/flavours/glitch/features/status/containers/detailed_status_container.js b/app/javascript/flavours/glitch/features/status/containers/detailed_status_container.js index 8fe0fc42e2..7cb9735cfe 100644 --- a/app/javascript/flavours/glitch/features/status/containers/detailed_status_container.js +++ b/app/javascript/flavours/glitch/features/status/containers/detailed_status_container.js @@ -71,12 +71,12 @@ const mapDispatchToProps = (dispatch, { intl }) => ({ }, onModalReblog (status, privacy) { - dispatch(reblog(status.id, privacy)); + dispatch(reblog({ statusId: status.get('id'), visibility: privacy })); }, onReblog (status, e) { if (status.get('reblogged')) { - dispatch(unreblog(status.id)); + dispatch(unreblog({ statusId: status.get('id') })); } else { if (e.shiftKey || !boostModal) { this.onModalReblog(status); diff --git a/app/javascript/flavours/glitch/features/status/index.jsx b/app/javascript/flavours/glitch/features/status/index.jsx index 943b48020a..00639a667e 100644 --- a/app/javascript/flavours/glitch/features/status/index.jsx +++ b/app/javascript/flavours/glitch/features/status/index.jsx @@ -346,9 +346,9 @@ class Status extends ImmutablePureComponent { const { dispatch } = this.props; if (status.get('reblogged')) { - dispatch(unreblog(status.id)); + dispatch(unreblog({ statusId: status.get('id') })); } else { - dispatch(reblog(status.id, privacy)); + dispatch(reblog({ statusId: status.get('id'), visibility: privacy })); } }; diff --git a/app/javascript/flavours/glitch/reducers/statuses.js b/app/javascript/flavours/glitch/reducers/statuses.js index 1da1c9cf2f..ca766f73a3 100644 --- a/app/javascript/flavours/glitch/reducers/statuses.js +++ b/app/javascript/flavours/glitch/reducers/statuses.js @@ -122,13 +122,13 @@ export default function statuses(state = initialState, action) { return statusTranslateUndo(state, action.id); default: if(reblog.pending.match(action)) - return state.setIn([action.meta.params.statusId, 'reblogged'], true); + return state.setIn([action.meta.arg.statusId, 'reblogged'], true); else if(reblog.rejected.match(action)) - return state.get(action.meta.params.statusId) === undefined ? state : state.setIn([action.meta.params.statusId, 'reblogged'], false); + return state.get(action.meta.arg.statusId) === undefined ? state : state.setIn([action.meta.arg.statusId, 'reblogged'], false); else if(unreblog.pending.match(action)) - return state.setIn([action.meta.params.statusId, 'reblogged'], false); + return state.setIn([action.meta.arg.statusId, 'reblogged'], false); else if(unreblog.rejected.match(action)) - return state.get(action.meta.params.statusId) === undefined ? state : state.setIn([action.meta.params.statusId, 'reblogged'], true); + return state.get(action.meta.arg.statusId) === undefined ? state : state.setIn([action.meta.arg.statusId, 'reblogged'], true); else return state; } diff --git a/app/javascript/flavours/glitch/store/typed_functions.ts b/app/javascript/flavours/glitch/store/typed_functions.ts index 4b07a55610..0392f373c0 100644 --- a/app/javascript/flavours/glitch/store/typed_functions.ts +++ b/app/javascript/flavours/glitch/store/typed_functions.ts @@ -92,20 +92,20 @@ type OnData = ( // Overload when there is no `onData` method, the payload is the `onData` result export function createDataLoadingThunk< LoadDataResult, - Args extends readonly unknown[], + Args extends Record, >( name: string, - loadData: (...args: Args) => Promise, + loadData: (args: Args) => Promise, thunkOptions?: AppThunkOptions, ): ReturnType>; // Overload when the `onData` method returns discardLoadDataInPayload, then the payload is empty export function createDataLoadingThunk< LoadDataResult, - Args extends readonly unknown[], + Args extends Record, >( name: string, - loadData: (...args: Args) => Promise, + loadData: (args: Args) => Promise, onDataOrThunkOptions?: | AppThunkOptions | OnData, @@ -115,10 +115,10 @@ export function createDataLoadingThunk< // Overload when the `onData` method returns nothing, then the mayload is the `onData` result export function createDataLoadingThunk< LoadDataResult, - Args extends readonly unknown[], + Args extends Record, >( name: string, - loadData: (...args: Args) => Promise, + loadData: (args: Args) => Promise, onDataOrThunkOptions?: AppThunkOptions | OnData, thunkOptions?: AppThunkOptions, ): ReturnType>; @@ -126,11 +126,11 @@ export function createDataLoadingThunk< // Overload when there is an `onData` method returning something export function createDataLoadingThunk< LoadDataResult, - Args extends readonly unknown[], + Args extends Record, Returned, >( name: string, - loadData: (...args: Args) => Promise, + loadData: (args: Args) => Promise, onDataOrThunkOptions?: AppThunkOptions | OnData, thunkOptions?: AppThunkOptions, ): ReturnType>; @@ -142,7 +142,7 @@ export function createDataLoadingThunk< * * It is a wrapper around RTK's [`createAsyncThunk`](https://redux-toolkit.js.org/api/createAsyncThunk) * @param name Prefix for the actions types - * @param loadData Function that loads the data. It's arguments will become the thunk's arguments + * @param loadData Function that loads the data. It's (object) argument will become the thunk's argument * @param onDataOrThunkOptions * Callback called on the results from `loadData`. * @@ -162,11 +162,11 @@ export function createDataLoadingThunk< */ export function createDataLoadingThunk< LoadDataResult, - Args extends readonly unknown[], + Args extends Record, Returned, >( name: string, - loadData: (...args: Args) => Promise, + loadData: (args: Args) => Promise, onDataOrThunkOptions?: AppThunkOptions | OnData, maybeThunkOptions?: AppThunkOptions, ) { @@ -184,7 +184,7 @@ export function createDataLoadingThunk< return createThunk( name, async (arg, { getState, dispatch }) => { - const data = await loadData(...arg); + const data = await loadData(arg); if (!onData) return data as Returned;