Update suggestions query with new api hook

This commit is contained in:
Justin 2022-08-10 08:46:00 -04:00
parent ae0fd07580
commit e72476d577
2 changed files with 26 additions and 28 deletions

View file

@ -1,12 +1,12 @@
import { renderHook } from '@testing-library/react-hooks';
import { mock, queryWrapper, waitFor } from 'soapbox/jest/test-helpers';
import { __stub } from 'soapbox/api';
import { renderHook, waitFor } from 'soapbox/jest/test-helpers';
import useOnboardingSuggestions from '../suggestions';
describe('useCarouselAvatars', () => {
describe('with a successul query', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v2/suggestions')
.reply(200, [
{ source: 'staff', account: { id: '1', acct: 'a', account_avatar: 'https://example.com/some.jpg' } },
@ -15,11 +15,10 @@ describe('useCarouselAvatars', () => {
link: '<https://example.com/api/v2/suggestions?since_id=1>; rel=\'prev\'',
});
});
});
it('is successful', async() => {
const { result } = renderHook(() => useOnboardingSuggestions(), {
wrapper: queryWrapper,
});
const { result } = renderHook(() => useOnboardingSuggestions());
await waitFor(() => expect(result.current.isFetching).toBe(false));
@ -29,13 +28,13 @@ describe('useCarouselAvatars', () => {
describe('with an unsuccessul query', () => {
beforeEach(() => {
__stub((mock) => {
mock.onGet('/api/v2/suggestions').networkError();
});
});
it('is successful', async() => {
const { result } = renderHook(() => useOnboardingSuggestions(), {
wrapper: queryWrapper,
});
const { result } = renderHook(() => useOnboardingSuggestions());
await waitFor(() => expect(result.current.isFetching).toBe(false));

View file

@ -3,8 +3,7 @@ import { useInfiniteQuery } from '@tanstack/react-query';
import { fetchRelationships } from 'soapbox/actions/accounts';
import { importFetchedAccounts } from 'soapbox/actions/importer';
import { getLinks } from 'soapbox/api';
import { useAppDispatch } from 'soapbox/hooks';
import API from 'soapbox/queries/client';
import { useApi, useAppDispatch } from 'soapbox/hooks';
type Account = {
acct: string
@ -36,10 +35,14 @@ type Suggestion = {
account: Account
}
const getV2Suggestions = async(dispatch: any, pageParam: any): Promise<{ data: Suggestion[], link: string | null, hasMore: boolean }> => {
return dispatch(async() => {
export default function useOnboardingSuggestions() {
const api = useApi();
const dispatch = useAppDispatch();
const getV2Suggestions = async(pageParam: any): Promise<{ data: Suggestion[], link: string | undefined, hasMore: boolean }> => {
const link = pageParam?.link || '/api/v2/suggestions';
const response = await API.get<Suggestion[]>(link);
const response = await api.get<Suggestion[]>(link);
const hasMore = !!response.headers.link;
const nextLink = getLinks(response).refs.find(link => link.rel === 'next')?.uri;
@ -53,13 +56,9 @@ const getV2Suggestions = async(dispatch: any, pageParam: any): Promise<{ data: S
link: nextLink,
hasMore,
};
});
};
export default function useOnboardingSuggestions() {
const dispatch = useAppDispatch();
const result = useInfiniteQuery(['suggestions', 'v2'], ({ pageParam }) => getV2Suggestions(dispatch, pageParam), {
const result = useInfiniteQuery(['suggestions', 'v2'], ({ pageParam }) => getV2Suggestions(pageParam), {
keepPreviousData: true,
getNextPageParam: (config) => {
if (config.hasMore) {