Skip to content

Commit

Permalink
Remove account-local-storage module
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 13, 2023
1 parent 0fb5a0f commit c401373
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 52 deletions.
3 changes: 1 addition & 2 deletions app/accounts/[id]/edit.tsx
Expand Up @@ -10,7 +10,6 @@ import {
ActivityIndicator,
} from "../../../components";
import { getLastEventId, updateAccount } from "../../../lib/account";
import { storeAccountLocal } from "../../../lib/account-local-storage";
import { storeAccountEvent } from "../../../lib/api";
import { useTranslation } from "../../../lib/i18n";

Expand Down Expand Up @@ -40,7 +39,7 @@ export default function CategoryEdit(): JSX.Element {
const [newAccount, event] = result.value;
storeAccountEvent(getLastEventId(account), event).then((_) => {
setAccount(newAccount);
storeAccountLocal({ id: newAccount.id, name: newAccount.name });

router.back();
});
};
Expand Down
20 changes: 16 additions & 4 deletions app/accounts/[id]/settings.tsx
Expand Up @@ -9,16 +9,21 @@ import {
View,
useAccount,
} from "../../../components";
import { getLastEvent, listCategory } from "../../../lib/account";
import { deleteAccountFromLocal } from "../../../lib/account-local-storage";
import {
deleteAccount,
getLastEvent,
getLastEventId,
listCategory,
} from "../../../lib/account";
import { storeAccountEvent } from "../../../lib/api";
import { useTranslation } from "../../../lib/i18n";

export default function Settings(): JSX.Element {
const pathname = usePathname();
const params = useSearchParams();
const accountId = `${params.id}`;
const router = useRouter();
const [account, _setAccount] = useAccount(accountId, [pathname]);
const [account, setAccount] = useAccount(accountId, [pathname]);
const [deleteModalVisible, setDeleteModalVisible] = useState<boolean>(false);
const { t } = useTranslation();

Expand Down Expand Up @@ -90,7 +95,14 @@ export default function Settings(): JSX.Element {
name={account.name}
onClickCancel={() => setDeleteModalVisible(false)}
onClickOk={() => {
deleteAccountFromLocal(accountId);
if (accountId === null || account === undefined) return;
const result = deleteAccount(account);
if (result.isErr()) throw new Error(result.error);
const [newAccount, event] = result.value;
setAccount(newAccount);
storeAccountEvent(getLastEventId(account), event).catch((_) =>
setAccount(account)
);
router.back();
}}
visible={deleteModalVisible}
Expand Down
18 changes: 5 additions & 13 deletions app/accounts/new.tsx
Expand Up @@ -5,16 +5,9 @@ import { useTranslation } from "react-i18next";
import { IconButton, Screen, TextInput, View } from "../../components";
import { useAccounts } from "../../components/AccountContext";
import { useCredential } from "../../hooks/use-credential";
import { Account, AccountEvent, createAccount } from "../../lib/account";
import { createAccount } from "../../lib/account";
import { storeAccountEvent } from "../../lib/api";

const storeRemote = async (
_account: Account,
event: AccountEvent
): Promise<void> => {
await storeAccountEvent(null, event);
};

type Form = {
name: string;
};
Expand All @@ -35,11 +28,10 @@ export default function AccountNew(): JSX.Element {
const onClickOk = ({ name }: Form) => {
const result = createAccount(credential.user.uid, name);
if (result.isErr()) return;
const [account, event] = result.value;

setAccount(account.id, account);
storeRemote(account, event).catch((_) => {
setAccount(account.id, null);
const [newAccount, event] = result.value;
setAccount(newAccount.id, newAccount);
storeAccountEvent(null, event).catch((_) => {
setAccount(newAccount.id, null);
});

router.back();
Expand Down
33 changes: 0 additions & 33 deletions lib/account-local-storage.ts

This file was deleted.

0 comments on commit c401373

Please sign in to comment.