Skip to content

Commit

Permalink
点赞,转推
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuaige1234567 committed Dec 12, 2023
1 parent dbdc6d4 commit c9564e1
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 54 deletions.
29 changes: 29 additions & 0 deletions frontend/src/actors/bucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {Principal} from "@dfinity/principal";
import {idlFactory} from "../declarations/bucket/bucket.did.js";
import {getActor} from "../utils/Actor";
import {PostImmutable} from "../declarations/bucket/bucket";


export default class Bucket {

private readonly canisterId: Principal;

constructor(canisterId: Principal) {
this.canisterId = canisterId;
}

private async getActor() {
return await getActor.createActor(idlFactory, this.canisterId.toString());
}

async getLatestFeed(n: number) {
const actor = await this.getActor()
try {
return await actor.getLatestFeed(BigInt(n)) as PostImmutable[]
} catch (e) {
console.log("getLatestFeed", e)
throw e
}
}

}
56 changes: 36 additions & 20 deletions frontend/src/actors/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,45 @@ export default class Feed {
}
}

// 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 createComment(postId: string, content: string) {
const actor = await this.getActor()
try {
return await actor.createComment(postId, content) as boolean
} catch (e) {
console.log("createComment", e)
throw e
}
}

// async createLike(postId) {
// const result = await this.actor.createLike(postId);
// return result;
// }
async createRepost(postId: string) {
const actor = await this.getActor()
try {
return await actor.createRepost(postId) as boolean
} catch (e) {
console.log("createRepost", e)
throw e
}
}

// async getFeedNumber() {
// const result = await this.actor.getFeedNumber();
// return result;
// }
async createLike(postId: string) {
const actor = await this.getActor()
try {
return await actor.createLike(postId) as boolean
} catch (e) {
console.log("createLike", e)
throw e
}
}

// async getFeed(postId) {
// return await this.actor.getFeed(postId);
// }
async getPost(postId: string) {
const actor = await this.getActor()
try {
return await actor.getPost(postId) as [] | [PostImmutable]
} catch (e) {
console.log("getPost", e)
throw e
}
}

async getLatestFeed(n: number): Promise<PostImmutable[]> {
const actor = await this.getActor()
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/actors/rootPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {getActor} from "../utils/Actor";
import {idlFactory} from "../declarations/rootPost/rootPost.did.js"
import {Principal} from "@dfinity/principal";

const rootPostCai = "lyksr-aiaaa-aaaan-qgj2q-cai"

class rootPost {
private static async getActor() {
return await getActor.createActor(idlFactory, rootPostCai);
}

async getAvailableBucket() {
const actor = await rootPost.getActor()
try {
return await actor.getAvailableBucket() as [] | [Principal]
} catch (e) {
console.log("getAvailableBucket", e)
throw e
}
}
}

export const rootPostApi = new rootPost()
12 changes: 6 additions & 6 deletions frontend/src/components/comment.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {Flex, Space, Avatar, Typography, Divider} from 'antd';
import {Comment} from "../declarations/feed/feed"

export default function Comment() {

export function Comments(props: { content: Comment }) {
const {content} = props
return (
<Flex
vertical
Expand All @@ -17,12 +20,9 @@ export default function Comment() {
<p>NeutronStarDAO</p>
</Space>
<Typography.Paragraph>
This is Comment;This is Comment;This is Comment;
This is Comment;This is Comment;This is Comment;
This is Comment;This is Comment;This is Comment;
This is Comment;This is Comment;This is Comment;
{content.content}
</Typography.Paragraph>
<Divider style={{marginBottom:"0"}}/>
<Divider style={{marginBottom: "0"}}/>
</Flex>
)
}
44 changes: 38 additions & 6 deletions frontend/src/components/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,48 @@ import {
HeartOutlined
} from '@ant-design/icons';
import {PostImmutable} from "../declarations/feed/feed";
import {useAuth} from "../utils/useAuth";
import Feed from "../actors/feed";
import React, {useState} from "react";

export default function Post(props: { content: PostImmutable, setPostItem?: Function }) {
const {content: data, setPostItem} = props
const {userFeedCai} = useAuth()
const [content, setContent] = useState(data)

const feedApi = React.useMemo(() => {
if (!userFeedCai) return undefined
return new Feed(userFeedCai)
}, [userFeedCai])

const update = async () => {
if (!feedApi) return
const updateData = await feedApi.getPost(content.postId)
console.log("updateData", updateData)
if (!updateData[0]) return
setContent(updateData[0])
}

const repost = async () => {
if (!feedApi) return
await feedApi.createRepost(content.postId)
update().then()
}

const like = async () => {
if (!feedApi) return
await feedApi.createLike(content.postId)
update().then()
}

export default function Post(props: { content: PostImmutable }) {
const {content} = props
return (
<div className={"content"} style={{
padding: "12px",
border: "1px solid rgba(0,0,0,0.2)",
borderRadius: "20px",
marginBottom: "20px",
}}>
cursor: "pointer"
}} onClick={() => setPostItem?.(content)}>
{/*<Divider/>*/}
<Space>
<Avatar
Expand All @@ -37,16 +69,16 @@ export default function Post(props: { content: PostImmutable }) {
paddingLeft: '25px'
}}
>
<div>
<div style={{cursor: "pointer"}}>
<CommentOutlined/> &nbsp;
{content.comment.length}
</div>
<div>
<div style={{cursor: "pointer"}} onClick={repost}>
<RedoOutlined/>
&nbsp;
{content.repost.length}
</div>
<div>
<div style={{cursor: "pointer"}} onClick={like}>
<HeartOutlined/>&nbsp;
{content.like.length}
</div>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion frontend/src/declarations/bucket/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 './bucket.did';
import { _SERVICE } from './bucket';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/declarations/rootPost/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 './rootPost.did';
import { _SERVICE } from './rootPost';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;
Expand Down
32 changes: 14 additions & 18 deletions frontend/src/routes/content.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from "react";
import React, {useState} from "react";
import {Layout} from "antd";
import Post from "../components/post";
import Comment from "../components/comment";
import {Comments} from "../components/comment";
import {Like, PostId, PostImmutable, Repost, Time, UserId} from "../declarations/feed/feed";

export const Content = React.memo((props: { contents: PostImmutable[] }) => {
const {contents} = props
const [postItem, setPostItem] = useState<PostImmutable>()
return <>
<Layout.Content className={"posts"} style={{
backgroundColor: "white",
Expand All @@ -16,23 +17,18 @@ export const Content = React.memo((props: { contents: PostImmutable[] }) => {
padding: "40px 20px"
}}>
{contents.map((v, k) => {
return <Post content={v} key={k}/>
return <Post setPostItem={setPostItem} content={v} key={k}/>
})}
</Layout.Content>
<Layout.Content className={"posts"} style={{
backgroundColor: 'white',
overflowY: 'auto',
scrollbarWidth: 'thin',
padding: "40px 20px"
}}>
{postItem && postItem.comment.map((v, k) => {
return <Comments content={v} key={k}/>
})}
</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>*/}
</>
})
21 changes: 19 additions & 2 deletions frontend/src/routes/explore.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import {Content} from "./content";
import React from "react";
import React, {useEffect, useState} from "react";
import {rootPostApi} from "../actors/rootPost";
import Bucket from "../actors/bucket";
import {PostImmutable} from "../declarations/feed/feed";

export default function Explore() {
return <Content contents={[]}/>
const [contents, setContents] = useState<PostImmutable[]>([])

const fetch = async () => {
const bucket = await rootPostApi.getAvailableBucket()
if (!bucket[0]) return
const bucketApi = new Bucket(bucket[0])
const res = await bucketApi.getLatestFeed(30)
setContents(res)
}

useEffect(() => {
fetch()
}, [])

return <Content contents={contents}/>
}

0 comments on commit c9564e1

Please sign in to comment.