-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shuaige1234567
committed
Dec 10, 2023
1 parent
ec365dc
commit 1780b3e
Showing
6 changed files
with
97 additions
and
76 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 |
---|---|---|
@@ -1,63 +1,67 @@ | ||
import { ActorSubclass, HttpAgent, Identity } from "@dfinity/agent"; | ||
import { _SERVICE } from "../declarations/feed/feed.did"; | ||
import { createActor } from "../declarations/feed/index"; | ||
import { _getHttpAgent } from '../utils/common'; | ||
import { Principal } from "@dfinity/principal"; | ||
import {Principal} from "@dfinity/principal"; | ||
import {idlFactory} from "../declarations/feed/feed.did.js"; | ||
import {getActor} from "../utils/Actor"; | ||
import {PostImmutable} from "../declarations/feed/feed"; | ||
|
||
const DFX_NETWORK = process.env.REACT_APP_DFX_NETWORK; | ||
|
||
export default class Feed { | ||
|
||
canisterId: Principal; | ||
identity: Identity; | ||
actor: ActorSubclass<_SERVICE>; | ||
|
||
constructor(canisterId: Principal, identity: Identity) { | ||
this.canisterId = canisterId; | ||
this.identity = identity; | ||
this.actor = createActor(canisterId, { | ||
agent: _getHttpAgent(identity), | ||
}); | ||
} | ||
private readonly canisterId: Principal; | ||
|
||
constructor(canisterId: Principal) { | ||
this.canisterId = canisterId; | ||
} | ||
|
||
private async getActor() { | ||
return await getActor.createActor(idlFactory, this.canisterId.toString()); | ||
} | ||
|
||
// async createPost() { | ||
// // 发帖前更新当前可用的bucket | ||
// assert(await this.actor.checkAvailableBucket(), true); | ||
|
||
// const result = await this.actor.createPost( | ||
// "this is title", | ||
// "this is content" | ||
// ); | ||
// // console.log('createPost result', result); | ||
// return result | ||
// } | ||
|
||
// async createComment(postId, content) { | ||
// const result = await this.actor.createComment(postId, content); | ||
// return result; | ||
// } | ||
|
||
// async createPost() { | ||
// // 发帖前更新当前可用的bucket | ||
// assert(await this.actor.checkAvailableBucket(), true); | ||
|
||
// const result = await this.actor.createPost( | ||
// "this is title", | ||
// "this is content" | ||
// ); | ||
// // console.log('createPost result', result); | ||
// return result | ||
// } | ||
|
||
// async createComment(postId, content) { | ||
// const result = await this.actor.createComment(postId, content); | ||
// return result; | ||
// } | ||
|
||
// async createRepost(postId) { | ||
// const result = await this.actor.createRepost(postId); | ||
// return result; | ||
// } | ||
|
||
// async createLike(postId) { | ||
// const result = await this.actor.createLike(postId); | ||
// return result; | ||
// } | ||
|
||
// async getFeedNumber() { | ||
// const result = await this.actor.getFeedNumber(); | ||
// return result; | ||
// } | ||
|
||
// async getFeed(postId) { | ||
// return await this.actor.getFeed(postId); | ||
// } | ||
|
||
// async getLatestFeed(n) { | ||
// return await this.actor.getLatestFeed(n); | ||
// } | ||
// async createRepost(postId) { | ||
// const result = await this.actor.createRepost(postId); | ||
// return result; | ||
// } | ||
|
||
// async createLike(postId) { | ||
// const result = await this.actor.createLike(postId); | ||
// return result; | ||
// } | ||
|
||
// async getFeedNumber() { | ||
// const result = await this.actor.getFeedNumber(); | ||
// return result; | ||
// } | ||
|
||
// async getFeed(postId) { | ||
// return await this.actor.getFeed(postId); | ||
// } | ||
|
||
async getLatestFeed(n: number): Promise<PostImmutable[]> { | ||
const actor = await this.getActor() | ||
try { | ||
const res = await actor.getLatestFeed(BigInt(n)) as PostImmutable[] | ||
console.log("feeds", res) | ||
return res | ||
} catch (e) { | ||
console.log("getLatestFeed error", e) | ||
throw e | ||
} | ||
} | ||
|
||
} |
File renamed without changes.
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
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,23 @@ | ||
import React from "react"; | ||
import React, {useEffect, useState} from "react"; | ||
|
||
import {Content} from "./content"; | ||
import {useAuth} from "../utils/useAuth"; | ||
import Feed from "../actors/feed"; | ||
import {PostImmutable} from "../declarations/feed/feed"; | ||
|
||
export const Home = React.memo(() => { | ||
return <Content/> | ||
const {userFeedCai} = useAuth() | ||
const [content, setContent] = useState<PostImmutable[]>([]) | ||
|
||
const fetch = async () => { | ||
if (!userFeedCai) return | ||
const feedApi = new Feed(userFeedCai) | ||
const feeds = await feedApi.getLatestFeed(20) | ||
setContent(feeds) | ||
} | ||
|
||
useEffect(() => { | ||
fetch() | ||
}, [userFeedCai]) | ||
return <Content content={content}/> | ||
}) |