Skip to content

Commit

Permalink
Fix createDataLoadingThunk and related actions (#30408)
Browse files Browse the repository at this point in the history
  • Loading branch information
renchap committed May 23, 2024
1 parent 133d98f commit b6fd14f
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 35 deletions.
3 changes: 2 additions & 1 deletion app/javascript/mastodon/actions/account_notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { createDataLoadingThunk } from 'mastodon/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 },
);
11 changes: 8 additions & 3 deletions app/javascript/mastodon/actions/interactions_typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));

Expand Down
6 changes: 3 additions & 3 deletions app/javascript/mastodon/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ export default function api(withAuthorization = true) {
export async function apiRequest<ApiResponse = unknown>(
method: Method,
url: string,
params?: unknown,
params?: Record<string, unknown>,
) {
const { data } = await api().request<ApiResponse>({
method,
url,
params,
url: '/api/' + url,
data: params,
});

return data;
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/api/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { apiRequest } from 'mastodon/api';
import type { ApiRelationshipJSON } from 'mastodon/api_types/relationships';

export const apiSubmitAccountNote = (id: string, value: string) =>
apiRequest<ApiRelationshipJSON>('post', `/api/v1/accounts/${id}/note`, {
apiRequest<ApiRelationshipJSON>('post', `v1/accounts/${id}/note`, {
comment: value,
});
4 changes: 2 additions & 2 deletions app/javascript/mastodon/containers/status_container.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,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 }));
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
},

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,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 => {
Expand All @@ -132,7 +132,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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,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);
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/mastodon/features/status/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class Status extends ImmutablePureComponent {
};

handleModalReblog = (status, privacy) => {
this.props.dispatch(reblog(status.id, privacy));
this.props.dispatch(reblog({ statusId: status.get('id'), visibility: privacy }));
};

handleReblogClick = (status, e) => {
Expand All @@ -308,7 +308,7 @@ class Status 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.handleModalReblog(status);
Expand Down
8 changes: 4 additions & 4 deletions app/javascript/mastodon/reducers/statuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
24 changes: 12 additions & 12 deletions app/javascript/mastodon/store/typed_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,20 @@ type OnData<LoadDataResult, ReturnedData> = (
// Overload when there is no `onData` method, the payload is the `onData` result
export function createDataLoadingThunk<
LoadDataResult,
Args extends readonly unknown[],
Args extends Record<string, unknown>,
>(
name: string,
loadData: (...args: Args) => Promise<LoadDataResult>,
loadData: (args: Args) => Promise<LoadDataResult>,
thunkOptions?: AppThunkOptions,
): ReturnType<typeof createThunk<Args, LoadDataResult>>;

// Overload when the `onData` method returns discardLoadDataInPayload, then the payload is empty
export function createDataLoadingThunk<
LoadDataResult,
Args extends readonly unknown[],
Args extends Record<string, unknown>,
>(
name: string,
loadData: (...args: Args) => Promise<LoadDataResult>,
loadData: (args: Args) => Promise<LoadDataResult>,
onDataOrThunkOptions?:
| AppThunkOptions
| OnData<LoadDataResult, DiscardLoadData>,
Expand All @@ -115,22 +115,22 @@ 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<string, unknown>,
>(
name: string,
loadData: (...args: Args) => Promise<LoadDataResult>,
loadData: (args: Args) => Promise<LoadDataResult>,
onDataOrThunkOptions?: AppThunkOptions | OnData<LoadDataResult, void>,
thunkOptions?: AppThunkOptions,
): ReturnType<typeof createThunk<Args, LoadDataResult>>;

// Overload when there is an `onData` method returning something
export function createDataLoadingThunk<
LoadDataResult,
Args extends readonly unknown[],
Args extends Record<string, unknown>,
Returned,
>(
name: string,
loadData: (...args: Args) => Promise<LoadDataResult>,
loadData: (args: Args) => Promise<LoadDataResult>,
onDataOrThunkOptions?: AppThunkOptions | OnData<LoadDataResult, Returned>,
thunkOptions?: AppThunkOptions,
): ReturnType<typeof createThunk<Args, Returned>>;
Expand All @@ -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`.
*
Expand All @@ -162,11 +162,11 @@ export function createDataLoadingThunk<
*/
export function createDataLoadingThunk<
LoadDataResult,
Args extends readonly unknown[],
Args extends Record<string, unknown>,
Returned,
>(
name: string,
loadData: (...args: Args) => Promise<LoadDataResult>,
loadData: (args: Args) => Promise<LoadDataResult>,
onDataOrThunkOptions?: AppThunkOptions | OnData<LoadDataResult, Returned>,
maybeThunkOptions?: AppThunkOptions,
) {
Expand All @@ -184,7 +184,7 @@ export function createDataLoadingThunk<
return createThunk<Args, Returned>(
name,
async (arg, { getState, dispatch }) => {
const data = await loadData(...arg);
const data = await loadData(arg);

if (!onData) return data as Returned;

Expand Down

0 comments on commit b6fd14f

Please sign in to comment.