From f6751915b066ee2b907553fcc50ebf3b9044e6aa Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Sun, 23 Jun 2024 18:34:44 -0300 Subject: [PATCH] feat(actions): create expandZaps() interaction --- src/actions/interactions.ts | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/actions/interactions.ts b/src/actions/interactions.ts index 07ceb8f40..b18b8f3a2 100644 --- a/src/actions/interactions.ts +++ b/src/actions/interactions.ts @@ -88,6 +88,9 @@ const ZAPS_FETCH_REQUEST = 'ZAPS_FETCH_REQUEST'; const ZAPS_FETCH_SUCCESS = 'ZAPS_FETCH_SUCCESS'; const ZAPS_FETCH_FAIL = 'ZAPS_FETCH_FAIL'; +const ZAPS_EXPAND_SUCCESS = 'ZAPS_EXPAND_SUCCESS'; +const ZAPS_EXPAND_FAIL = 'ZAPS_EXPAND_FAIL'; + const messages = defineMessages({ bookmarkAdded: { id: 'status.bookmarked', defaultMessage: 'Bookmark added.' }, bookmarkRemoved: { id: 'status.unbookmarked', defaultMessage: 'Bookmark removed.' }, @@ -634,8 +637,9 @@ const fetchZaps = (id: string) => dispatch(fetchZapsRequest(id)); api(getState).get(`/api/v1/ditto/statuses/${id}/zapped_by`).then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next'); dispatch(importFetchedAccounts((response.data as APIEntity[]).map(({ account }) => account).flat())); - dispatch(fetchZapsSuccess(id, response.data)); + dispatch(fetchZapsSuccess(id, response.data, next ? next.uri : null)); }).catch(error => { dispatch(fetchZapsFail(id, error)); }); @@ -646,10 +650,11 @@ const fetchZapsRequest = (id: string) => ({ id, }); -const fetchZapsSuccess = (id: string, zaps: APIEntity[]) => ({ +const fetchZapsSuccess = (id: string, zaps: APIEntity[], next: string | null) => ({ type: ZAPS_FETCH_SUCCESS, id, zaps, + next, }); const fetchZapsFail = (id: string, error: unknown) => ({ @@ -658,6 +663,31 @@ const fetchZapsFail = (id: string, error: unknown) => ({ error, }); +const expandZaps = (id: string, path: string) => + (dispatch: AppDispatch, getState: () => RootState) => { + api(getState).get(path).then(response => { + const next = getLinks(response).refs.find(link => link.rel === 'next'); + dispatch(importFetchedAccounts(response.data.map((item: APIEntity) => item.account))); + dispatch(fetchRelationships(response.data.map((item: APIEntity) => item.account.id))); + dispatch(expandZapsSuccess(id, response.data, next ? next.uri : null)); + }).catch(error => { + dispatch(expandZapsFail(id, error)); + }); + }; + +const expandZapsSuccess = (id: string, zaps: APIEntity[], next: string | null) => ({ + type: ZAPS_EXPAND_SUCCESS, + id, + zaps, + next, +}); + +const expandZapsFail = (id: string, error: unknown) => ({ + type: ZAPS_EXPAND_FAIL, + id, + error, +}); + const pin = (status: StatusEntity) => (dispatch: AppDispatch, getState: () => RootState) => { if (!isLoggedIn(getState)) return; @@ -838,6 +868,8 @@ export { ZAPS_FETCH_REQUEST, ZAPS_FETCH_SUCCESS, ZAPS_FETCH_FAIL, + ZAPS_EXPAND_SUCCESS, + ZAPS_EXPAND_FAIL, reblog, unreblog, toggleReblog, @@ -909,4 +941,5 @@ export { remoteInteractionFail, zap, fetchZaps, + expandZaps, };