Skip to content

Commit

Permalink
Automatically reload when API call is redirected
Browse files Browse the repository at this point in the history
  • Loading branch information
TimQuelch committed Aug 23, 2024
1 parent 132fc34 commit 48695bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/desktop-client/src/global-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,8 @@ export function handleGlobalEvents(actions: BoundActions, store: Store<State>) {
actions.closeBudgetUI();
actions.setAppState({ loadingText: null });
});

listen('api-fetch-redirected', () => {
actions.reloadApp();
});
}
6 changes: 6 additions & 0 deletions packages/loot-core/src/client/actions/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ export function focused() {
return send('app-focused');
};
}

export function reloadApp() {
return () => {
global.Actual.reload();
};
}
17 changes: 16 additions & 1 deletion packages/loot-core/src/platform/server/fetch/index.web.ts
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
export const fetch = globalThis.fetch;
import * as connection from "../connection"

Check warning on line 1 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"../connection"` with `'../connection';`

export const fetch = async (
input: RequestInfo | URL,

Check warning on line 4 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
options?: RequestInit,

Check warning on line 5 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
): Promise<Response> => {
const response = await globalThis.fetch(input, options);

Check warning on line 7 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `····` with `··`

const originalHost = new URL(input instanceof Request ? input.url : input).host

Check warning on line 9 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `····const·originalHost·=·new·URL(input·instanceof·Request·?·input.url·:·input).host` with `··const·originalHost·=·new·URL(input·instanceof·Request·?·input.url·:·input)⏎····.host;`

if (response.redirected && (new URL(response.url)).host !== originalHost) {

Check warning on line 11 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `··if·(response.redirected·&&·(new·URL(response.url))` with `if·(response.redirected·&&·new·URL(response.url)`
connection.send("api-fetch-redirected")

Check warning on line 12 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `········connection.send("api-fetch-redirected")` with `····connection.send('api-fetch-redirected');`
}

Check warning on line 13 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `····` with `··`

return response

Check warning on line 15 in packages/loot-core/src/platform/server/fetch/index.web.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `····return·response` with `··return·response;`
};
1 change: 1 addition & 0 deletions packages/loot-core/src/types/server-events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface ServerEvents {
'start-load': unknown;
'sync-event': { type; subtype; meta; tables; syncDisabled };
'undo-event': UndoState;
'api-fetch-redirected': unknown;
}

0 comments on commit 48695bf

Please sign in to comment.