Skip to content

Commit

Permalink
feat(api): use conditional requests and fetch all inbox notifications (
Browse files Browse the repository at this point in the history
…#1414)

* feat: conditional request

* feat: conditional request

* feat: conditional request

* feat: conditional request

* rename util fn

* Merge branch 'main' into feature/conditional-request

Signed-off-by: Adam Setch <adam.setch@outlook.com>

* feat: fetch all records

Signed-off-by: Adam Setch <adam.setch@outlook.com>

* feat: add notification setting to control fetching all

Signed-off-by: Adam Setch <adam.setch@outlook.com>

* feat: add notification setting to control fetching all

Signed-off-by: Adam Setch <adam.setch@outlook.com>

* Update src/components/settings/NotificationSettings.tsx

Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>

* Update src/components/settings/NotificationSettings.tsx

Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>

---------

Signed-off-by: Adam Setch <adam.setch@outlook.com>
Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>
  • Loading branch information
setchy and afonsojramos authored Sep 13, 2024
1 parent 7d4c723 commit b90814d
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/__mocks__/state-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const mockAppearanceSettings = {

const mockNotificationSettings = {
groupBy: GroupBy.REPOSITORY,
fetchAllNotifications: true,
participating: false,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
Expand Down
26 changes: 26 additions & 0 deletions src/components/settings/NotificationSettings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ describe('routes/components/settings/NotificationSettings.tsx', () => {
expect(updateSetting).toHaveBeenCalledTimes(1);
expect(updateSetting).toHaveBeenCalledWith('groupBy', 'DATE');
});

it('should toggle the fetchAllNotifications checkbox', async () => {
await act(async () => {
render(
<AppContext.Provider
value={{
auth: mockAuth,
settings: mockSettings,
updateSetting,
}}
>
<MemoryRouter>
<NotificationSettings />
</MemoryRouter>
</AppContext.Provider>,
);
});

fireEvent.click(screen.getByLabelText('Fetch all notifications'), {
target: { checked: true },
});

expect(updateSetting).toHaveBeenCalledTimes(1);
expect(updateSetting).toHaveBeenCalledWith('fetchAllNotifications', false);
});

it('should toggle the showOnlyParticipating checkbox', async () => {
await act(async () => {
render(
Expand Down
20 changes: 20 additions & 0 deletions src/components/settings/NotificationSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ export const NotificationSettings: FC = () => {
updateSetting('groupBy', evt.target.value as GroupBy);
}}
/>
<Checkbox
name="fetchAllNotifications"
label="Fetch all notifications"
checked={settings.fetchAllNotifications}
onChange={(evt) =>
updateSetting('fetchAllNotifications', evt.target.checked)
}
tooltip={
<div>
<div className="pb-3">
When <em>checked</em>, Gitify will fetch <strong>all</strong>{' '}
notifications from your inbox.
</div>
<div>
When <em>unchecked</em>, Gitify will only fetch the first page of
notifications (max 50 records per GitHub account)
</div>
</div>
}
/>
<Checkbox
name="showOnlyParticipating"
label="Show only participating"
Expand Down
1 change: 1 addition & 0 deletions src/context/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const defaultAppearanceSettings = {

const defaultNotificationSettings = {
groupBy: GroupBy.REPOSITORY,
fetchAllNotifications: true,
participating: false,
markAsDoneOnOpen: false,
markAsDoneOnUnsubscribe: false,
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe('hooks/useNotifications.ts', () => {
});

expect(result.current.globalError).toBe(Errors.BAD_CREDENTIALS);
expect(logErrorSpy).toHaveBeenCalledTimes(2);
expect(logErrorSpy).toHaveBeenCalledTimes(4);
});

it('should fetch notifications with different failures', async () => {
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('hooks/useNotifications.ts', () => {
});

expect(result.current.globalError).toBeNull();
expect(logErrorSpy).toHaveBeenCalledTimes(2);
expect(logErrorSpy).toHaveBeenCalledTimes(4);
});
});

Expand Down
44 changes: 44 additions & 0 deletions src/routes/__snapshots__/Settings.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ interface AppearanceSettingsState {

interface NotificationSettingsState {
groupBy: GroupBy;
fetchAllNotifications: boolean;
participating: boolean;
markAsDoneOnOpen: boolean;
markAsDoneOnUnsubscribe: boolean;
Expand Down
33 changes: 21 additions & 12 deletions src/utils/api/__snapshots__/client.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/utils/api/__snapshots__/request.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions src/utils/api/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,32 @@ describe('utils/api/client.ts', () => {
});

describe('listNotificationsForAuthenticatedUser', () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
};
it('should list notifications for user - github cloud - fetchAllNotifications true', async () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
fetchAllNotifications: true,
};

await listNotificationsForAuthenticatedUser(
mockGitHubCloudAccount,
mockSettings as SettingsState,
);

expect(axios).toHaveBeenCalledWith({
url: 'https://api.github.com/notifications?participating=true',
method: 'GET',
data: {},
});

expect(axios.defaults.headers.common).toMatchSnapshot();
});

it('should list notifications for user - github cloud - fetchAllNotifications false', async () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
fetchAllNotifications: false,
};

it('should list notifications for user - github cloud', async () => {
await listNotificationsForAuthenticatedUser(
mockGitHubCloudAccount,
mockSettings as SettingsState,
Expand All @@ -103,6 +124,10 @@ describe('utils/api/client.ts', () => {
});

it('should list notifications for user - github enterprise server', async () => {
const mockSettings: Partial<SettingsState> = {
participating: true,
};

await listNotificationsForAuthenticatedUser(
mockGitHubEnterpriseServerAccount,
mockSettings as SettingsState,
Expand Down
9 changes: 7 additions & 2 deletions src/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ export function listNotificationsForAuthenticatedUser(
const url = getGitHubAPIBaseUrl(account.hostname);
url.pathname += 'notifications';
url.searchParams.append('participating', String(settings.participating));

return apiRequestAuth(url.toString() as Link, 'GET', account.token);
return apiRequestAuth(
url.toString() as Link,
'GET',
account.token,
{},
settings.fetchAllNotifications,
);
}

/**
Expand Down
Loading

0 comments on commit b90814d

Please sign in to comment.