Skip to content

Commit

Permalink
创建feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuaige1234567 committed Dec 10, 2023
1 parent ec365dc commit 1780b3e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 76 deletions.
116 changes: 60 additions & 56 deletions frontend/src/actors/feed.ts
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.
2 changes: 1 addition & 1 deletion frontend/src/declarations/feed/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import type { Principal } from "@dfinity/principal";
import type { IDL } from "@dfinity/candid";

import { _SERVICE } from './feed.did';
import { _SERVICE } from './feed';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;
Expand Down
33 changes: 17 additions & 16 deletions frontend/src/routes/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React from "react";
import {Layout} from "antd";
import Post from "../components/post";
import Comment from "../components/comment";
import {PostImmutable} from "../declarations/feed/feed";

export const Content = React.memo(()=>{
export const Content = React.memo((props:{content:PostImmutable[]})=>{
return <>
<Layout.Content className={"posts"} style={{
backgroundColor: "white",
Expand All @@ -21,20 +22,20 @@ export const Content = React.memo(()=>{
<Post/>
<Post/>
</Layout.Content>
<Layout.Content className={"posts"} style={{
backgroundColor: 'white',
overflowY: 'auto',
scrollbarWidth: 'thin',
padding:"40px 20px"
}}>
<Comment/>
<Comment/>
<Comment/>
<Comment/>
<Comment/>
<Comment/>
<Comment/>
<Comment/>
</Layout.Content>
{/*<Layout.Content className={"posts"} style={{*/}
{/* backgroundColor: 'white',*/}
{/* overflowY: 'auto',*/}
{/* scrollbarWidth: 'thin',*/}
{/* padding:"40px 20px"*/}
{/*}}>*/}
{/* <Comment/>*/}
{/* <Comment/>*/}
{/* <Comment/>*/}
{/* <Comment/>*/}
{/* <Comment/>*/}
{/* <Comment/>*/}
{/* <Comment/>*/}
{/* <Comment/>*/}
{/*</Layout.Content>*/}
</>
})
2 changes: 1 addition & 1 deletion frontend/src/routes/explore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import {Content} from "./content";
import React from "react";

export default function Explore() {
return <Content/>
return <Content content={[]}/>
}
20 changes: 18 additions & 2 deletions frontend/src/routes/home.tsx
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}/>
})

0 comments on commit 1780b3e

Please sign in to comment.