Skip to content

Commit 62926fc

Browse files
authored
refactor: use status type (loading, error, success) (#1061)
* refactor: introduce status type * refactor: status type * refactor: status type * test: rename suite * refactor: status type
1 parent 743fb07 commit 62926fc

File tree

9 files changed

+92
-94
lines changed

9 files changed

+92
-94
lines changed

src/components/Loading.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ describe('components/Loading.tsx', () => {
1616
jest.clearAllMocks();
1717
});
1818

19-
it('should check that NProgress is getting called in when isFetching changes (loading)', () => {
19+
it('should check that NProgress is getting called in when status changes (loading)', () => {
2020
const { container } = render(
21-
<AppContext.Provider value={{ isFetching: true }}>
21+
<AppContext.Provider value={{ status: 'loading' }}>
2222
<Loading />
2323
</AppContext.Provider>,
2424
);
@@ -28,9 +28,9 @@ describe('components/Loading.tsx', () => {
2828
expect(NProgress.start).toHaveBeenCalledTimes(1);
2929
});
3030

31-
it('should check that NProgress is getting called in when isFetching changes (not loading)', () => {
31+
it('should check that NProgress is getting called in when status changes (not loading)', () => {
3232
const { container } = render(
33-
<AppContext.Provider value={{ isFetching: false }}>
33+
<AppContext.Provider value={{ status: 'success' }}>
3434
<Loading />
3535
</AppContext.Provider>,
3636
);
@@ -42,7 +42,7 @@ describe('components/Loading.tsx', () => {
4242

4343
it('should remove NProgress on unmount', () => {
4444
const { unmount } = render(
45-
<AppContext.Provider value={{ isFetching: true }}>
45+
<AppContext.Provider value={{ status: 'loading' }}>
4646
<Loading />
4747
</AppContext.Provider>,
4848
);

src/components/Loading.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useContext, useEffect } from 'react';
44
import { AppContext } from '../context/App';
55

66
export const Loading = () => {
7-
const { isFetching } = useContext(AppContext);
7+
const { status } = useContext(AppContext);
88

99
useEffect(() => {
1010
NProgress.configure({
@@ -17,12 +17,12 @@ export const Loading = () => {
1717
}, []);
1818

1919
useEffect(() => {
20-
if (isFetching) {
20+
if (status === 'loading') {
2121
NProgress.start();
2222
} else {
2323
NProgress.done();
2424
}
25-
}, [isFetching]);
25+
}, [status]);
2626

2727
return null;
2828
};

src/components/Sidebar.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const Sidebar: FC = () => {
1919
const navigate = useNavigate();
2020
const location = useLocation();
2121

22-
const { notifications, fetchNotifications, isLoggedIn, isFetching } =
22+
const { notifications, fetchNotifications, isLoggedIn, status } =
2323
useContext(AppContext);
2424

2525
const onOpenBrowser = useCallback(() => {
@@ -81,15 +81,14 @@ export const Sidebar: FC = () => {
8181
navigate('/', { replace: true });
8282
fetchNotifications();
8383
}}
84-
disabled={isFetching}
84+
disabled={status === 'loading'}
8585
>
8686
<SyncIcon
8787
size={16}
8888
aria-label="Refresh Notifications"
89-
className={isFetching ? 'animate-spin' : undefined}
89+
className={status === 'loading' ? 'animate-spin' : undefined}
9090
/>
9191
</button>
92-
9392
<button
9493
type="button"
9594
className={sidebarButtonClasses}

src/context/App.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
type AuthTokenOptions,
1717
type GitifyError,
1818
type SettingsState,
19+
type Status,
1920
Theme,
2021
} from '../types';
2122
import { headNotifications } from '../utils/api/client';
@@ -54,8 +55,7 @@ interface AppContextState {
5455
logout: () => void;
5556

5657
notifications: AccountNotifications[];
57-
isFetching: boolean;
58-
requestFailed: boolean;
58+
status: Status;
5959
errorDetails: GitifyError;
6060
removeNotificationFromState: (id: string, hostname: string) => void;
6161
fetchNotifications: () => Promise<void>;
@@ -80,9 +80,8 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
8080
const {
8181
fetchNotifications,
8282
notifications,
83-
requestFailed,
8483
errorDetails,
85-
isFetching,
84+
status,
8685
removeNotificationFromState,
8786
markNotificationRead,
8887
markNotificationDone,
@@ -239,8 +238,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
239238
logout,
240239

241240
notifications,
242-
isFetching,
243-
requestFailed,
241+
status,
244242
errorDetails,
245243
removeNotificationFromState,
246244
fetchNotifications: fetchNotificationsWithAccounts,

0 commit comments

Comments
 (0)