Skip to content

Commit b50556a

Browse files
committed
Add support for setting an account as active
1 parent 89dca51 commit b50556a

File tree

13 files changed

+333
-34
lines changed

13 files changed

+333
-34
lines changed

packages/mobile-app/app/(tabs)/index.tsx

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { StatusBar } from "expo-status-bar";
2-
import { ScrollView, StyleSheet, Text, View } from "react-native";
2+
import {
3+
ActivityIndicator,
4+
ScrollView,
5+
StyleSheet,
6+
Text,
7+
View,
8+
} from "react-native";
39
import { useFacade } from "../../data/facades";
410
import { useEffect, useState } from "react";
511
import { LinkButton } from "../../components/LinkButton";
@@ -17,23 +23,37 @@ export default function Balances() {
1723
);
1824

1925
const getAccountResult = facade.getAccount.useQuery(
20-
{ name: account },
26+
{},
2127
{
2228
refetchInterval: 1000,
2329
},
2430
);
2531

26-
const getAccountsResult = facade.getAccounts.useQuery();
27-
2832
const getWalletStatusResult = facade.getWalletStatus.useQuery(undefined, {
2933
refetchInterval: 1000,
3034
});
3135

3236
useEffect(() => {
33-
if (getAccountsResult.data?.[0]) {
34-
setAccount(getAccountsResult.data[0].name);
37+
if (getAccountResult.data) {
38+
setAccount(getAccountResult.data.name);
3539
}
36-
}, [getAccountsResult.data]);
40+
}, [getAccountResult.data]);
41+
42+
if (getAccountResult.isLoading) {
43+
return (
44+
<View style={styles.container}>
45+
<ActivityIndicator size="large" />
46+
</View>
47+
);
48+
}
49+
50+
if (getAccountResult.data === null) {
51+
return (
52+
<View style={styles.container}>
53+
<LinkButton title="Onboarding" href="/onboarding/" />
54+
</View>
55+
);
56+
}
3757

3858
return (
3959
<View style={styles.container}>

packages/mobile-app/app/account-select.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,41 @@ import { StatusBar } from "expo-status-bar";
22
import { Button, StyleSheet, Text, View } from "react-native";
33
import { useRouter } from "expo-router";
44
import { LinkButton } from "../components/LinkButton";
5+
import { useQueryClient } from "@tanstack/react-query";
56
import { useFacade } from "../data/facades";
67

78
export default function AccountSelect() {
89
const router = useRouter();
910
const facade = useFacade();
11+
const qc = useQueryClient();
1012

1113
const getAccountsResult = facade.getAccounts.useQuery(undefined, {
1214
refetchInterval: 1000,
1315
});
1416

17+
const setActiveAccount = facade.setActiveAccount.useMutation({
18+
onSuccess: async () => {
19+
await qc.invalidateQueries();
20+
},
21+
});
22+
1523
return (
1624
<View style={styles.container}>
1725
<Button title="Close" onPress={() => router.dismissAll()} />
1826
<StatusBar style="auto" />
1927
{getAccountsResult.data?.map((account) => (
2028
<View key={account.name}>
21-
<Text>{account.name}</Text>
29+
<Button
30+
onPress={async () => {
31+
const result = await setActiveAccount.mutateAsync({
32+
name: account.name,
33+
});
34+
console.log(`setActiveAccount: ${result}`);
35+
router.dismissAll();
36+
}}
37+
title={account.name}
38+
/>
39+
{account.active && <Text>Active</Text>}
2240
<Text>{`${account.balances.iron.confirmed} $IRON`}</Text>
2341
</View>
2442
))}

packages/mobile-app/app/account-settings/remove-account.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ export default function RemoveAccount() {
1313
const { accountName } = useLocalSearchParams<{ accountName: string }>();
1414

1515
const removeAccount = facade.removeAccount.useMutation({
16-
onSuccess: () => {
17-
qc.invalidateQueries({
18-
queryKey: ["getAccounts"],
19-
});
16+
onSuccess: async () => {
17+
await qc.invalidateQueries();
2018
},
2119
});
2220

packages/mobile-app/app/add-account/create.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ export default function CreateAccount() {
1111
const qc = useQueryClient();
1212

1313
const createAccount = facade.createAccount.useMutation({
14-
onSuccess: () => {
15-
qc.invalidateQueries({
16-
queryKey: ["getAccounts"],
17-
});
14+
onSuccess: async () => {
15+
await qc.invalidateQueries();
1816
},
1917
});
2018

packages/mobile-app/app/add-account/import-encoded.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ export default function ImportEncoded() {
1616
const qc = useQueryClient();
1717

1818
const importAccount = facade.importAccount.useMutation({
19-
onSuccess: () => {
20-
qc.invalidateQueries({
21-
queryKey: ["getAccounts"],
22-
});
19+
onSuccess: async () => {
20+
await qc.invalidateQueries();
2321
},
2422
});
2523

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { useQueryClient } from "@tanstack/react-query";
2+
import { StatusBar } from "expo-status-bar";
3+
import { Button, StyleSheet, TextInput, View } from "react-native";
4+
import { useRouter } from "expo-router";
5+
import { useState } from "react";
6+
import { useFacade } from "../../data/facades";
7+
8+
export default function OnboardingCreate() {
9+
const router = useRouter();
10+
const facade = useFacade();
11+
const qc = useQueryClient();
12+
13+
const createAccount = facade.createAccount.useMutation({
14+
onSuccess: async () => {
15+
await qc.invalidateQueries();
16+
},
17+
});
18+
19+
const [accountName, setAccountName] = useState("Account Name");
20+
21+
return (
22+
<View style={styles.container}>
23+
<TextInput
24+
placeholder="Account Name"
25+
value={accountName}
26+
onChangeText={setAccountName}
27+
/>
28+
<Button
29+
title="Continue"
30+
onPress={async () => {
31+
await createAccount.mutateAsync({ name: accountName });
32+
router.dismissAll();
33+
}}
34+
/>
35+
<StatusBar style="auto" />
36+
</View>
37+
);
38+
}
39+
40+
const styles = StyleSheet.create({
41+
container: {
42+
flex: 1,
43+
backgroundColor: "#fff",
44+
alignItems: "center",
45+
justifyContent: "center",
46+
},
47+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { useQueryClient } from "@tanstack/react-query";
2+
import { StatusBar } from "expo-status-bar";
3+
import { Button, Modal, StyleSheet, Text, TextInput, View } from "react-native";
4+
import { useRouter } from "expo-router";
5+
import { useState } from "react";
6+
import { useFacade } from "../../data/facades";
7+
8+
export default function OnboardingImportEncoded() {
9+
const router = useRouter();
10+
11+
const [modalVisible, setModalVisible] = useState(false);
12+
const [accountName, setAccountName] = useState("Account Name");
13+
const [encodedAccount, setEncodedAccount] = useState("");
14+
15+
const facade = useFacade();
16+
const qc = useQueryClient();
17+
18+
const importAccount = facade.importAccount.useMutation({
19+
onSuccess: async () => {
20+
await qc.invalidateQueries();
21+
},
22+
});
23+
24+
return (
25+
<View style={styles.container}>
26+
<Modal visible={modalVisible} animationType="slide">
27+
<View style={styles.container}>
28+
<Text>Account Imported!</Text>
29+
<Text>
30+
Before you start managing your digital assets, we need to scan the
31+
blockchain. This may take some time.
32+
</Text>
33+
<Button
34+
title="Let's go!"
35+
onPress={async () => {
36+
router.dismissAll();
37+
setModalVisible(false);
38+
}}
39+
/>
40+
</View>
41+
</Modal>
42+
<Text>Paste the complete string into the provided text field below.</Text>
43+
<TextInput
44+
placeholder="Account Name"
45+
value={accountName}
46+
onChangeText={setAccountName}
47+
/>
48+
<TextInput
49+
placeholder="Encoded Key"
50+
value={encodedAccount}
51+
onChangeText={setEncodedAccount}
52+
/>
53+
<Button
54+
title="Continue"
55+
onPress={async () => {
56+
await importAccount.mutateAsync({
57+
account: encodedAccount,
58+
name: accountName,
59+
});
60+
setModalVisible(true);
61+
}}
62+
/>
63+
<StatusBar style="auto" />
64+
</View>
65+
);
66+
}
67+
68+
const styles = StyleSheet.create({
69+
container: {
70+
flex: 1,
71+
backgroundColor: "#fff",
72+
alignItems: "center",
73+
justifyContent: "center",
74+
},
75+
});

packages/mobile-app/app/onboarding/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { StatusBar } from "expo-status-bar";
22
import { StyleSheet, View, Text } from "react-native";
3+
import { LinkButton } from "../../components/LinkButton";
34

45
export default function Onboarding() {
56
return (
67
<View style={styles.container}>
78
<Text>Welcome to Iron Fish</Text>
89
<Text>Let's Make Web3 Private</Text>
9-
<Text>Create Account</Text>
10-
<Text>I already have an account</Text>
10+
<LinkButton title="Create Account" href="/onboarding/create/" />
11+
<LinkButton
12+
title="I already have an account"
13+
href="/onboarding/import-encoded/"
14+
/>
1115
<StatusBar style="auto" />
1216
</View>
1317
);

packages/mobile-app/data/facades/wallet/demoHandlers.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const ACCOUNTS: Account[] = [
3030
custom: [],
3131
},
3232
settings: { balanceAutoHide: false },
33+
active: true,
3334
},
3435
{
3536
name: "bob",
@@ -48,6 +49,7 @@ const ACCOUNTS: Account[] = [
4849
custom: [],
4950
},
5051
settings: { balanceAutoHide: false },
52+
active: false,
5153
},
5254
{
5355
name: "carol",
@@ -66,6 +68,7 @@ const ACCOUNTS: Account[] = [
6668
custom: [],
6769
},
6870
settings: { balanceAutoHide: false },
71+
active: false,
6972
},
7073
];
7174

@@ -103,8 +106,10 @@ export const walletDemoHandlers = f.facade<WalletHandlers>({
103106
},
104107
viewOnly: false,
105108
settings: { balanceAutoHide: false },
109+
active: true,
106110
};
107111
console.log("createAccount", account);
112+
ACCOUNTS.filter((a) => a.active).forEach((a) => (a.active = false));
108113
ACCOUNTS.push(account);
109114
return account;
110115
}),
@@ -125,10 +130,10 @@ export const walletDemoHandlers = f.facade<WalletHandlers>({
125130
return JSON.stringify(account);
126131
},
127132
),
128-
getAccount: f.handler.query(async ({ name }: { name: string }) => {
133+
getAccount: f.handler.query(async ({ name }: { name?: string }) => {
129134
const account = ACCOUNTS.find((a) => a.name === name);
130135
if (account === undefined) {
131-
throw new Error(`No account found with name ${name}`);
136+
return null;
132137
}
133138
return account;
134139
}),
@@ -137,6 +142,15 @@ export const walletDemoHandlers = f.facade<WalletHandlers>({
137142
console.log("getAccounts", accounts);
138143
return accounts;
139144
}),
145+
setActiveAccount: f.handler.mutation(async ({ name }: { name: string }) => {
146+
const newAccount = ACCOUNTS.find((a) => a.name === name);
147+
if (!newAccount) {
148+
return false;
149+
}
150+
ACCOUNTS.filter((a) => a.active).forEach((a) => (a.active = false));
151+
newAccount.active = true;
152+
return true;
153+
}),
140154
getEstimatedFees: f.handler.query(
141155
async (args: { accountName: string; outputs: Output[] }) => {
142156
return { slow: "0.00000001", average: "0.00000002", fast: "0.00000003" };
@@ -264,8 +278,10 @@ export const walletDemoHandlers = f.facade<WalletHandlers>({
264278
custom: [],
265279
},
266280
settings: { balanceAutoHide: false },
281+
active: true,
267282
};
268283
console.log("importAccount", account);
284+
ACCOUNTS.filter((a) => a.active).forEach((a) => (a.active = false));
269285
ACCOUNTS.push(importedAccount);
270286
return importedAccount;
271287
},

0 commit comments

Comments
 (0)