Skip to content

Commit

Permalink
set loading state on error
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-jeremy committed Oct 19, 2024
1 parent 0f4d947 commit 5ed9bf7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
30 changes: 18 additions & 12 deletions packages/loot-core/src/client/data-hooks/schedules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,25 @@ export function useSchedules({
let isUnmounted = false;

setError(undefined);
setIsLoading(!!query);

if (!query) {
return;
}

if (query.state.table !== 'schedules') {
setError(new Error('Query must be a schedules query.'));
return;
}

function onError(error: Error) {
if (!isUnmounted) {
setError(error);
setIsLoading(false);
}
}

if (query.state.table !== 'schedules') {
onError(new Error('Query must be a schedules query.'));
return;
}

setIsLoading(true);

scheduleQueryRef.current = liveQuery<ScheduleEntity>(query, {
onData: async schedules => {
statusQueryRef.current = loadStatuses(
Expand Down Expand Up @@ -131,11 +133,9 @@ export function useSchedules({

type SchedulesContextValue = UseSchedulesResult;

const SchedulesContext = createContext<SchedulesContextValue>({
isLoading: false,
schedules: [],
statuses: new Map(),
});
const SchedulesContext = createContext<SchedulesContextValue | undefined>(
undefined,
);

type SchedulesProviderProps = PropsWithChildren<{
query?: UseSchedulesProps['query'];
Expand All @@ -151,7 +151,13 @@ export function SchedulesProvider({ query, children }: SchedulesProviderProps) {
}

export function useCachedSchedules() {
return useContext(SchedulesContext);
const context = useContext(SchedulesContext);
if (!context) {
throw new Error(
'useCachedSchedules must be used within a SchedulesProvider',
);
}
return context;
}

export function accountSchedulesQuery(
Expand Down
21 changes: 13 additions & 8 deletions packages/loot-core/src/client/data-hooks/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,33 @@ export function useTransactions({
let isUnmounted = false;

setError(undefined);
setIsLoading(!!query);

if (!query) {
return;
}

function onError(error: Error) {
if (!isUnmounted) {
setError(error);
setIsLoading(false);
}
}

if (query.state.table !== 'transactions') {
setError(new Error('Query must be a transactions query.'));
onError(new Error('Query must be a transactions query.'));
return;
}

setIsLoading(true);

pagedQueryRef.current = pagedQuery<TransactionEntity>(query, {
onData: data => {
if (!isUnmounted) {
setTransactions(data);
setIsLoading(false);
}
},
onError: error => {
if (!isUnmounted) {
setError(error);
}
},
onError,
options: { pageCount: optionsRef.current.pageCount },
});

Expand Down Expand Up @@ -163,13 +167,14 @@ export function usePreviewTransactions(): UsePreviewTransactionsResult {
),
}));

setIsLoading(false);
setPreviewTransactions(ungroupTransactions(withDefaults));
setIsLoading(false);
}
})
.catch(error => {
if (!isUnmounted) {
setError(error);
setIsLoading(false);
}
});

Expand Down

0 comments on commit 5ed9bf7

Please sign in to comment.