From acf6a53f7730d0cf5bd97889867de0d08894c862 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 25 Mar 2023 14:38:34 -0700 Subject: [PATCH 1/5] refactor(ui); use composition api + SWR pattern for CommentDetail, CommentVotes, CommentsForPost --- ui/src/herd/posts/CommentDetail.vue | 235 +++++++++++--------------- ui/src/herd/posts/CommentVotes.vue | 155 ++++++++--------- ui/src/herd/posts/CommentsForPost.vue | 6 +- 3 files changed, 172 insertions(+), 224 deletions(-) diff --git a/ui/src/herd/posts/CommentDetail.vue b/ui/src/herd/posts/CommentDetail.vue index 22a562a..fee7f88 100644 --- a/ui/src/herd/posts/CommentDetail.vue +++ b/ui/src/herd/posts/CommentDetail.vue @@ -1,27 +1,27 @@ - diff --git a/ui/src/herd/posts/CommentVotes.vue b/ui/src/herd/posts/CommentVotes.vue index 4b357e9..1214067 100644 --- a/ui/src/herd/posts/CommentVotes.vue +++ b/ui/src/herd/posts/CommentVotes.vue @@ -7,98 +7,79 @@ /> - +const fetchMyVote = async () => { + try { + const res = await client.callZome({ + cell_id: [props.dnaHash, client.myPubKey], + zome_name: 'posts', + fn_name: 'get_my_vote_on_comment', + payload: props.originalActionHash, + }); + + return res ? res.value : 0; + } catch (e: any) { + toast.error(`Failed to fetch post votes count: ${e.data.data}`); + } +}; + +const upvote = async () => { + if(myVote.value === 1) return; + + try { + await client.callZome({ + cell_id: [props.dnaHash, client.myPubKey], + zome_name: 'posts', + fn_name: 'upvote_comment', + payload: props.originalActionHash, + }); + emit('upvote'); + runFetchMyVote(); + } catch (e: any) { + toast.error(`Failed to vote on post: ${e.data.data}`); + } +}; - \ No newline at end of file + try { + await client.callZome({ + cell_id: [props.dnaHash, client.myPubKey], + zome_name: 'posts', + fn_name: 'downvote_comment', + payload: props.originalActionHash, + }); + emit('downvote'); + runFetchMyVote(); + } catch (e: any) { + toast.error("Failed to vote on post: ", e.data.data); + } +}; + +const {data: myVote, run: runFetchMyVote } = useRequest(fetchMyVote, { + onError: (e: any) => { + toast.error(`Failed to fetch post votes count: ${e.data.data}`); + } +}); + +watch(props, () => { + runFetchMyVote(); +}); + diff --git a/ui/src/herd/posts/CommentsForPost.vue b/ui/src/herd/posts/CommentsForPost.vue index 75a549f..4e73e94 100644 --- a/ui/src/herd/posts/CommentsForPost.vue +++ b/ui/src/herd/posts/CommentsForPost.vue @@ -12,8 +12,8 @@ class="w-full" > import { inject, ComputedRef } from 'vue'; -import { AppAgentClient, encodeHashToBase64 } from '@holochain/client'; +import { AppAgentClient } from '@holochain/client'; import CommentDetail from './CommentDetail.vue'; import CreateComment from './CreateComment.vue'; import { toast } from 'vue3-toastify'; From 4602d7e46ec0640669b0d6e0971e38f24bd56839 Mon Sep 17 00:00:00 2001 From: Matt Gabrenya Date: Sat, 25 Mar 2023 14:49:46 -0700 Subject: [PATCH 2/5] refactor(ui): convert components to composition api, add prop allowPeeking to BaseContentHidden --- ui/src/components/BaseContentHidden.vue | 13 ++++++++----- ui/src/herd/posts/CommentDetail.vue | 9 +++++++-- ui/src/herd/posts/PostListItem.vue | 1 + 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/ui/src/components/BaseContentHidden.vue b/ui/src/components/BaseContentHidden.vue index 1c2c952..3e707bb 100644 --- a/ui/src/components/BaseContentHidden.vue +++ b/ui/src/components/BaseContentHidden.vue @@ -8,6 +8,7 @@ - diff --git a/ui/src/herd/posts/CreatePost.vue b/ui/src/herd/posts/CreatePost.vue index 8b24107..e29d0bf 100644 --- a/ui/src/herd/posts/CreatePost.vue +++ b/ui/src/herd/posts/CreatePost.vue @@ -37,76 +37,56 @@ - diff --git a/ui/src/herd/posts/EditComment.vue b/ui/src/herd/posts/EditComment.vue index d29eb3c..06b5771 100644 --- a/ui/src/herd/posts/EditComment.vue +++ b/ui/src/herd/posts/EditComment.vue @@ -27,74 +27,47 @@ - diff --git a/ui/src/herd/posts/EditPost.vue b/ui/src/herd/posts/EditPost.vue index 7dc0482..e103825 100644 --- a/ui/src/herd/posts/EditPost.vue +++ b/ui/src/herd/posts/EditPost.vue @@ -45,84 +45,52 @@ - diff --git a/ui/src/herd/posts/PostDetail.vue b/ui/src/herd/posts/PostDetail.vue index 06ad899..6f50c87 100644 --- a/ui/src/herd/posts/PostDetail.vue +++ b/ui/src/herd/posts/PostDetail.vue @@ -84,12 +84,14 @@ import AgentProfile from '../profiles/AgentProfile.vue'; import EditPost from './EditPost.vue'; import {marked} from 'marked'; import dayjs from 'dayjs'; +import relativeTime from 'dayjs/plugin/relativeTime'; import { isEqual } from 'lodash'; import { toast } from 'vue3-toastify'; import BaseEditDeleteButtons from '../../components/BaseEditDeleteButtons.vue'; import DOMPurify from 'dompurify'; import { useRoute, useRouter } from 'vue-router'; import { useRequest } from 'vue-request'; +dayjs.extend(relativeTime); const route = useRoute(); const router = useRouter(); @@ -116,20 +118,18 @@ const postContent = computed(() => { }); const myPost = computed(() => { - if(!post_metadata.value?.record || !appInfo.value) return false; + if(!post_metadata.value?.record) return false; return isEqual(post_metadata.value.record.signed_action.hashed.content.author, client.myPubKey); }); const authorHash = computed(() => { if (!post_metadata.value?.record) return undefined; - - return post_metadata.value?.record.signed_action.hashed.content.author; + return post_metadata.value.record.signed_action.hashed.content.author; }); -const dateRelative = computed(() => { - if(!post_metadata.value?.record?.signed_action.hashed.content.timestamp) return; - - return dayjs(post_metadata.value?.record.signed_action.hashed.content.timestamp/1000).fromNow(); +const dateRelative = computed((): undefined | string => { + if(!post_metadata.value?.record) return undefined; + return dayjs(post_metadata.value.record.signed_action.hashed.content.timestamp/1000).fromNow(); }); const fetchPost = async (): Promise<{ upvotes: number, downvotes: number, record: Record }> => { @@ -143,11 +143,6 @@ const fetchPost = async (): Promise<{ upvotes: number, downvotes: number, record return post_metadata; }; -const fetchAppInfo = async () => { - const res = await client.appInfo(); - return res; -} - const deletePost = async() => { try { await client.callZome({ @@ -170,10 +165,4 @@ const { data: post_metadata, run: runFetchPost } = useRequest(fetchPost, { toast.error(`Error fetching the post: ${e.data.data}`); } }); - -const { data: appInfo } = useRequest(fetchAppInfo, { - onError: (e: any) => { - toast.error(`Failed to fetch appInfo ${e.data.data}`) - } -}); diff --git a/ui/src/herd/posts/PostListItem.vue b/ui/src/herd/posts/PostListItem.vue index 2cc84af..7494b9a 100644 --- a/ui/src/herd/posts/PostListItem.vue +++ b/ui/src/herd/posts/PostListItem.vue @@ -1,5 +1,5 @@ - diff --git a/ui/src/herd/posts/PostVotes.vue b/ui/src/herd/posts/PostVotes.vue index 488f3e1..812f8f0 100644 --- a/ui/src/herd/posts/PostVotes.vue +++ b/ui/src/herd/posts/PostVotes.vue @@ -9,107 +9,78 @@ /> - +}; + +const upvote = async() => { + if(myVote.value === 1) return; + + try { + await client.callZome({ + cell_id: [props.dnaHash, client.myPubKey], + cap_secret: null, + zome_name: 'posts', + fn_name: 'upvote_post', + payload: props.postHash, + }); + emit('upvote'); + runGetMyVote(); + } catch (e: any) { + toast.error(`Failed to upvote post: ${e.data.data}`); + } +}; + +const downvote = async () => { + if(myVote.value === -1) return; - \ No newline at end of file +const { data: myVote, run: runGetMyVote } = useRequest(getMyVote); + diff --git a/ui/src/herd/profiles/AgentProfile.vue b/ui/src/herd/profiles/AgentProfile.vue index ea0dc30..0b0a764 100644 --- a/ui/src/herd/profiles/AgentProfile.vue +++ b/ui/src/herd/profiles/AgentProfile.vue @@ -2,8 +2,8 @@
- +const props = withDefaults(defineProps<{ + agentPubKey: Uint8Array, + size?: string, + muted?: boolean, + hoverForDetails?: boolean +}>(),{ + size: 'lg', + muted: false, + hoverForDetails: true +}); - \ No newline at end of file +const agentPubKeyString = computed(() => { + return encodeHashToBase64(props.agentPubKey); +}); + +const getAgentProfile = async (): Promise => { + const res = await profilesStore.client.getAgentProfile(props.agentPubKey); + return res; +}; + +const { data: profile, run: runGetAgentProfile } = useRequest(getAgentProfile, { + onError: (e: any) => { + toast.error(`Error getting agent profile ${e.data.data}`); + } +}); + +watch(props, runGetAgentProfile); + \ No newline at end of file diff --git a/ui/src/herd/profiles/AgentProfileDetail.vue b/ui/src/herd/profiles/AgentProfileDetail.vue index f88bf1a..f0b3061 100644 --- a/ui/src/herd/profiles/AgentProfileDetail.vue +++ b/ui/src/herd/profiles/AgentProfileDetail.vue @@ -25,36 +25,22 @@
- - -