-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
executable file
·70 lines (59 loc) · 1.8 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { writable, get } from "svelte/store";
import { AuthStore, createAuthStore } from 'fpdao-ui/auth-store';
import { canisterId as ledgerCanisterId } from "./declarations/ledger";
import {
staging as ext,
createActor as createExtActor,
idlFactory as extIdlFactory,
} from "./declarations/ext";
// we can't use the canister id from the ext declarations, as
// we don't deploy the NFT canister from within this project,
// we just reference them
import { collection } from "./collection";
export const HOST = process.env.DFX_NETWORK !== "ic" ? "http://localhost:4943" : "https://icp0.io";
type State = {
extActor: typeof ext;
isLoading: boolean;
isBuying: boolean;
};
const defaultState: State = {
extActor: createExtActor(collection.canisterId, {
agentOptions: { host: HOST },
}),
isLoading: false,
isBuying: false,
};
export const createStore = (authStore: AuthStore) => {
const { subscribe, update } = writable<State>(defaultState);
let curAuth = get(authStore).isAuthed;
authStore.subscribe(async (state) => {
if (curAuth !== state.isAuthed) {
curAuth = state.isAuthed;
if (curAuth == null) {
update((prevState) => {
return {...defaultState};
});
} else {
let extActor = await authStore.createActor<typeof ext>(collection.canisterId, extIdlFactory);
update((prevState) => {
return {
...prevState,
extActor,
};
});
}
}
});
subscribe((state) => {
console.log("state", state);
});
return {
subscribe,
update,
};
};
export const authStore = createAuthStore({
whitelist: [collection.canisterId, ledgerCanisterId],
host: process.env.DFX_NETWORK !== "ic" ? "http://localhost:4943" : "https://icp0.io",
});
export const store = createStore(authStore);