Handle error state for carousels

sw-index-revalidate
Justin 2022-06-23 09:26:13 -04:00
rodzic 14a9a2f4eb
commit 9e6bb5264a
4 zmienionych plików z 22 dodań i 3 usunięć

Wyświetl plik

@ -1,5 +1,6 @@
import classNames from 'classnames';
import React, { useEffect, useState } from 'react';
import { FormattedMessage } from 'react-intl';
import { fetchCarouselAvatars } from 'soapbox/actions/carousels';
import { replaceHomeTimeline } from 'soapbox/actions/timelines';
@ -57,6 +58,7 @@ const FeedCarousel = () => {
const avatars = useAppSelector((state) => state.carousels.avatars);
const isLoading = useAppSelector((state) => state.carousels.isLoading);
const hasError = useAppSelector((state) => state.carousels.error);
const numberOfPages = Math.floor(avatars.length / pageSize);
const hasNextPage = currentPage < numberOfPages && numberOfPages > 1;
@ -81,6 +83,16 @@ const FeedCarousel = () => {
return null;
}
if (hasError) {
return (
<Card variant='rounded' size='lg'>
<Text align='center'>
<FormattedMessage id='common.error' defaultMessage="Something isn't right. Try reloading the page." />
</Text>
</Card>
);
}
return (
<Card variant='rounded' size='lg' ref={cardRef} className='relative'>
<div>

Wyświetl plik

@ -258,6 +258,7 @@
"column_forbidden.title": "Forbidden",
"column_header.show_settings": "Show settings",
"common.cancel": "Cancel",
"common.error": "Something isn't right. Try reloading the page.",
"community.column_settings.media_only": "Media Only",
"community.column_settings.title": "Local timeline settings",
"compose.character_counter.title": "Used {chars} out of {maxChars} characters",

Wyświetl plik

@ -32,6 +32,7 @@ describe('carousels reducer', () => {
expect(result.isLoading).toEqual(false);
expect(result.avatars).toEqual([45]);
expect(result.error).toEqual(false);
});
});
@ -39,7 +40,10 @@ describe('carousels reducer', () => {
it('sets "isLoading" to "true"', () => {
const initialState = { isLoading: true, avatars: [] };
const action = { type: CAROUSEL_AVATAR_FAIL };
expect(reducer(initialState, action).isLoading).toEqual(false);
const result = reducer(initialState, action);
expect(result.isLoading).toEqual(false);
expect(result.error).toEqual(true);
});
});
});

Wyświetl plik

@ -13,13 +13,15 @@ type Avatar = {
}
type CarouselsState = {
avatars: Avatar[],
avatars: Avatar[]
isLoading: boolean
error: boolean
}
const initialState: CarouselsState = {
avatars: [],
isLoading: false,
error: false,
};
export default function rules(state: CarouselsState = initialState, action: AnyAction): CarouselsState {
@ -29,7 +31,7 @@ export default function rules(state: CarouselsState = initialState, action: AnyA
case CAROUSEL_AVATAR_SUCCESS:
return { ...state, isLoading: false, avatars: action.payload };
case CAROUSEL_AVATAR_FAIL:
return { ...state, isLoading: false };
return { ...state, isLoading: false, error: true };
default:
return state;
}