Skip to content

Commit

Permalink
tsukota: Remove in memory cache and add pull to refresh to transactio…
Browse files Browse the repository at this point in the history
…n index screen
  • Loading branch information
bouzuya committed Jun 8, 2023
1 parent 3dd1489 commit 7ded225
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
14 changes: 13 additions & 1 deletion packages/tsukota/app/accounts/[id]/transactions/index.tsx
Expand Up @@ -31,8 +31,11 @@ export default function Transactions(): JSX.Element {
new Date().toISOString().substring(0, 10)
);
const [transactionId, setTransactionId] = useState<string | null>(null);
const [account, handleAccountCommand] = useAccount(accountId, [pathname]);
const [account, handleAccountCommand, fetchAccount] = useAccount(accountId, [
pathname,
]);
const { t } = useTranslation();
const [refreshing, setRefreshing] = useState<boolean>(false);

if (account === null)
return <ActivityIndicator size="large" style={{ flex: 1 }} />;
Expand Down Expand Up @@ -68,6 +71,15 @@ export default function Transactions(): JSX.Element {
},
});
}}
onRefresh={async () => {
setRefreshing(true);
try {
await fetchAccount();
} finally {
setRefreshing(false);
}
}}
refreshing={refreshing}
/>
)}
{account.categories.length === 0 ? null : (
Expand Down
30 changes: 13 additions & 17 deletions packages/tsukota/components/AccountContext.tsx
Expand Up @@ -68,23 +68,15 @@ function fetchAccountsWithCache(
): (...accountIds: string[]) => Promise<void> {
const { accounts, setAccounts } = context;
return async (...accountIds: string[]): Promise<void> => {
const _ = await Promise.all(
accountIds.map(async (accountId: string): Promise<Account> => {
const cachedAccount = accounts[accountId];
if (cachedAccount !== undefined) {
return cachedAccount;
} else {
const account = await fetchAccount(accountId);
return account;
const _ = await Promise.all(accountIds.map(fetchAccount)).then(
(newAccounts) => {
const updated: Accounts = {};
for (const newAccount of newAccounts) {
updated[newAccount.id] = newAccount;
}
})
).then((newAccounts) => {
const updated: Accounts = {};
for (const newAccount of newAccounts) {
updated[newAccount.id] = newAccount;
setAccounts((accounts) => ({ ...accounts, ...updated }));
}
setAccounts((accounts) => ({ ...accounts, ...updated }));
});
);
return;
};
}
Expand Down Expand Up @@ -143,14 +135,18 @@ export function AccountContextProvider({ children }: Props): JSX.Element {
export function useAccount(
accountId: string,
deps: DependencyList
): [Account | null, HandleAccountCommand] {
): [Account | null, HandleAccountCommand, () => Promise<void>] {
const context = useContext(AccountContext);
const { accounts } = context;
useEffect(() => {
// no await
fetchAccountsWithCache(context)(accountId);
}, deps);
return [accounts[accountId] ?? null, buildHandleAccountCommand(context)];
return [
accounts[accountId] ?? null,
buildHandleAccountCommand(context),
() => fetchAccountsWithCache(context)(accountId),
];
}

export function useAccounts(): {
Expand Down

0 comments on commit 7ded225

Please sign in to comment.