-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add call to fetch user's canister (#239)
* Update User template declaration in canister * Update declarations with types in frontend * Ignore declarations folders * Add dummy Ids of declared canisters * Add example env variable * Ignore implicit any errors from ts * Add userCanister to user store * Update canisterId if logged in * Update backend util to cache actor & use identity * Add userCanisterPrincipal to auth and authHelper
- Loading branch information
1 parent
150b543
commit 08ea8c2
Showing
13 changed files
with
136 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ package | |
pnpm-lock.yaml | ||
package-lock.json | ||
yarn.lock | ||
declarations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
{ | ||
"webclient": { | ||
"ic": "vyatz-hqaaa-aaaam-qauea-cai" | ||
}, | ||
"individual_user_template": { | ||
"local": "rwlgt-iiaaa-aaaaa-aaaaa-cai" | ||
}, | ||
"user_index": { | ||
"local": "ryjl3-tyaaa-aaaaa-aaaba-cai" | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
packages/canisters/individual_user_template/individual_user_template.did
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
type PostDetailsFromFrontend = record { | ||
hashtags : vec text; | ||
description : text; | ||
video_url : text; | ||
}; | ||
service : { | ||
create_post : (text, vec text, text) -> (nat64); | ||
create_post : (PostDetailsFromFrontend) -> (nat64); | ||
mark_post_as_ready_to_view : (nat64) -> (); | ||
} |
7 changes: 6 additions & 1 deletion
7
packages/web-client/declarations/individual_user_template/individual_user_template.did.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import type { Principal } from '@dfinity/principal'; | ||
import type { ActorMethod } from '@dfinity/agent'; | ||
|
||
export interface PostDetailsFromFrontend { | ||
'hashtags' : Array<string>, | ||
'description' : string, | ||
'video_url' : string, | ||
} | ||
export interface _SERVICE { | ||
'create_post' : ActorMethod<[string, Array<string>, string], bigint>, | ||
'create_post' : ActorMethod<[PostDetailsFromFrontend], bigint>, | ||
'mark_post_as_ready_to_view' : ActorMethod<[bigint], undefined>, | ||
} |
11 changes: 6 additions & 5 deletions
11
packages/web-client/declarations/individual_user_template/individual_user_template.did.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/web-client/packages/web-client/declarations/user_index/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Actor, HttpAgent } from "@dfinity/agent"; | ||
|
||
// Imports and re-exports candid interface | ||
import { idlFactory } from './user_index.did.js'; | ||
export { idlFactory } from './user_index.did.js'; | ||
// CANISTER_ID is replaced by webpack based on node environment | ||
export const canisterId = process.env.USER_INDEX_CANISTER_ID; | ||
|
||
/** | ||
* | ||
* @param {string | import("@dfinity/principal").Principal} canisterId Canister ID of Agent | ||
* @param {{agentOptions?: import("@dfinity/agent").HttpAgentOptions; actorOptions?: import("@dfinity/agent").ActorConfig}} [options] | ||
* @return {import("@dfinity/agent").ActorSubclass<import("./user_index.did.js")._SERVICE>} | ||
*/ | ||
export const createActor = (canisterId, options) => { | ||
const agent = new HttpAgent(options ? { ...options.agentOptions } : {}); | ||
|
||
// Fetch root key for certificate validation during development | ||
if (process.env.NODE_ENV !== "production") { | ||
agent.fetchRootKey().catch(err => { | ||
console.warn("Unable to fetch root key. Check to ensure that your local replica is running"); | ||
console.error(err); | ||
}); | ||
} | ||
|
||
// Creates an actor with using the candid interface and the HttpAgent | ||
return Actor.createActor(idlFactory, { | ||
agent, | ||
canisterId, | ||
...(options ? options.actorOptions : {}), | ||
}); | ||
}; | ||
|
||
/** | ||
* A ready-to-use agent for the user_index canister | ||
* @type {import("@dfinity/agent").ActorSubclass<import("./user_index.did.js")._SERVICE>} | ||
*/ | ||
export const user_index = createActor(canisterId); |
7 changes: 7 additions & 0 deletions
7
packages/web-client/packages/web-client/declarations/user_index/user_index.did.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Principal } from '@dfinity/principal'; | ||
import type { ActorMethod } from '@dfinity/agent'; | ||
|
||
export interface _SERVICE { | ||
'__comment_this_reset_user_canisters' : ActorMethod<[], undefined>, | ||
'get_users_canister' : ActorMethod<[], Principal>, | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/web-client/packages/web-client/declarations/user_index/user_index.did.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const idlFactory = ({ IDL }) => { | ||
return IDL.Service({ | ||
'__comment_this_reset_user_canisters' : IDL.Func([], [], []), | ||
'get_users_canister' : IDL.Func([], [IDL.Principal], []), | ||
}); | ||
}; | ||
export const init = ({ IDL }) => { return []; }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,52 @@ | ||
import { createActor, canisterId } from '../../declarations/backend'; | ||
import { | ||
createActor as createUserIndexActor, | ||
canisterId as userIndexCanisterId | ||
} from '$canisters/user_index'; | ||
import { createActor as createIndividualUserActor } from '$canisters/individual_user_template'; | ||
import type { _SERVICE as _USER_INDEX_SERVICE } from '$canisters/user_index/user_index.did'; | ||
import type { _SERVICE as _INDIVIDUAL_USER_SERVICE } from '$canisters/individual_user_template/individual_user_template.did'; | ||
import { auth } from '$stores/auth'; | ||
import type { ActorSubclass } from '@dfinity/agent'; | ||
import { get } from 'svelte/store'; | ||
|
||
export const host = | ||
process.env.NODE_ENV === 'development' ? 'http://localhost:8000' : 'https://ic0.app'; | ||
|
||
export const backend = createActor(canisterId ?? '', { | ||
agentOptions: { | ||
host | ||
} | ||
}); | ||
export type UserIndexActor = ActorSubclass<_USER_INDEX_SERVICE>; | ||
export type IndividualUserCanister = ActorSubclass<_INDIVIDUAL_USER_SERVICE>; | ||
|
||
const userIndexCanister: { | ||
actor?: UserIndexActor; | ||
loginState: boolean; | ||
} = { | ||
loginState: false | ||
}; | ||
|
||
const individualUserCanister: { | ||
actor?: IndividualUserCanister; | ||
loginState: boolean; | ||
} = { | ||
loginState: false | ||
}; | ||
|
||
export function userIndex(): UserIndexActor { | ||
const authStore = get(auth); | ||
if (!userIndexCanister.actor || userIndexCanister.loginState != authStore.isLoggedIn) { | ||
userIndexCanister.actor = createUserIndexActor(userIndexCanisterId as string, { | ||
agentOptions: { identity: authStore?.identity, host } | ||
}) as UserIndexActor; | ||
userIndexCanister.loginState = authStore.isLoggedIn; | ||
return userIndexCanister.actor; | ||
} else return userIndexCanister.actor; | ||
} | ||
|
||
export function individualUser(individualUserCanisterId: string): IndividualUserCanister { | ||
const authStore = get(auth); | ||
if (!individualUserCanister.actor || individualUserCanister.loginState != authStore.isLoggedIn) { | ||
individualUserCanister.actor = createIndividualUserActor(individualUserCanisterId as string, { | ||
agentOptions: { identity: authStore?.identity, host } | ||
}) as IndividualUserCanister; | ||
individualUserCanister.loginState = authStore.isLoggedIn; | ||
return individualUserCanister.actor; | ||
} else return individualUserCanister.actor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters