forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Glitch] Proposal: a modern & typed way of writing Redux actions doin…
…g API requests Port 10ec421 to glitch-soc Signed-off-by: Claire <[email protected]>
- Loading branch information
1 parent
0b6a726
commit e969d28
Showing
14 changed files
with
281 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,9 @@ | ||
import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships'; | ||
import { createAppAsyncThunk } from 'flavours/glitch/store/typed_functions'; | ||
import { apiSubmitAccountNote } from 'flavours/glitch/api/accounts'; | ||
import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions'; | ||
|
||
import api from '../api'; | ||
|
||
export const submitAccountNote = createAppAsyncThunk( | ||
export const submitAccountNote = createDataLoadingThunk( | ||
'account_note/submit', | ||
async (args: { id: string; value: string }) => { | ||
const response = await api().post<ApiRelationshipJSON>( | ||
`/api/v1/accounts/${args.id}/note`, | ||
{ | ||
comment: args.value, | ||
}, | ||
); | ||
|
||
return { relationship: response.data }; | ||
}, | ||
(accountId: string, note: string) => apiSubmitAccountNote(accountId, note), | ||
(relationship) => ({ relationship }), | ||
{ skipLoading: true }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
app/javascript/flavours/glitch/actions/interactions_typed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { apiReblog, apiUnreblog } from 'flavours/glitch/api/interactions'; | ||
import type { StatusVisibility } from 'flavours/glitch/models/status'; | ||
import { createDataLoadingThunk } from 'flavours/glitch/store/typed_functions'; | ||
|
||
import { importFetchedStatus } from './importer'; | ||
|
||
export const reblog = createDataLoadingThunk( | ||
'status/reblog', | ||
(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 | ||
dispatch(importFetchedStatus(data.reblog)); | ||
|
||
// The payload is not used in any actions | ||
return discardLoadData; | ||
}, | ||
); | ||
|
||
export const unreblog = createDataLoadingThunk( | ||
'status/unreblog', | ||
(statusId: string) => apiUnreblog(statusId), | ||
(data, { dispatch, discardLoadData }) => { | ||
dispatch(importFetchedStatus(data)); | ||
|
||
// The payload is not used in any actions | ||
return discardLoadData; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { apiRequest } from 'flavours/glitch/api'; | ||
import type { ApiRelationshipJSON } from 'flavours/glitch/api_types/relationships'; | ||
|
||
export const apiSubmitAccountNote = (id: string, value: string) => | ||
apiRequest<ApiRelationshipJSON>('post', `/api/v1/accounts/${id}/note`, { | ||
comment: value, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { apiRequest } from 'flavours/glitch/api'; | ||
import type { Status, StatusVisibility } from 'flavours/glitch/models/status'; | ||
|
||
export const apiReblog = (statusId: string, visibility: StatusVisibility) => | ||
apiRequest<{ reblog: Status }>('post', `v1/statuses/${statusId}/reblog`, { | ||
visibility, | ||
}); | ||
|
||
export const apiUnreblog = (statusId: string) => | ||
apiRequest<Status>('post', `v1/statuses/${statusId}/unreblog`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.