Don't get live like data

Hey I use amity sdk typescript when user click on the like button data not updated the response of the post kindly tell me how to do this

Hello, could you please help share the response screenshot, your ts sdk version and code sample?

Thanks for replying actually i use this function for like and dislike post aftre that donā€™t receive latest post dataPreformatted text
const handleLikePost = async () => {
// console.log(complete Item : ${JSON.stringify(item)});
if (!myReactions?.includes(ā€˜loveā€™)) {
ReactionRepository.addReaction(ā€˜postā€™, postId, ā€˜loveā€™);
getPost();
// setLiked(true);
} else {
ReactionRepository.removeReaction(ā€˜postā€™, postId, ā€˜loveā€™);
getPost();
// setLiked(false);
}
};
function getPost() {
PostRepository.getPost(postId, (data) => {
console.log(ā€˜latest comment dataā€“>ā€™, data);
setPost(data?.data);
// setLatestComments(data?.data?.latestComments);
});
}

Hello,

We will escalate this matter to our team for thorough examination. Rest assured, we will keep you informed of any updates.

Hello @anser786 May I inquire about your current SDK version?

ā€œ@amityco/ts-sdkā€: ā€œ^6.14.0ā€,

Based on your code, getPost() might be called before ReactionRepository.addReaction(ā€˜postā€™, postId, ā€˜loveā€™); is successfully called. Can you try adding await in front of ReactionRepository and try calling getPost() after that?

   const isPostReactionAdded = await ReactionRepository.addReaction(
          'post',
          postId,
          reactionName
        );
  if(isPostReactionAdded){
   getPost()
  }
1 Like

I tried but face same issue.post data donā€™t updated after hit reaction api

1 Like

Hello, @anser786 let me pass to may team check for you and iā€™ll back to you.

Hey, I have a similar problem as mentioned above.

Iā€™m subscribing to a bunch of posts based on a certain community ID. When Iā€™m calling a CommentRepository service every post is updated accordingly without a need to refresh posts (calling a getPosts function again).

Itā€™s sadly not a case for reactions. Looks like the subscription callback isnā€™t triggered for post reactions. Is that expected behavior?

(Iā€™m using the same sdk version by the way)

It will be a different one from Reaction, but Can I see your code that youā€™re using?

Sure, here it is.

Itā€™s a vue code, but I hope everything is straightforward.

    const unsubscribe = ref<Amity.Unsubscriber>();
    const posts = ref<Amity.Post<'text'>[]>([]);

    const getPosts = () => {
        unsubscribe.value = PostRepository.getPosts(getPostsParams, ({ data, loading, hasNextPage, onNextPage }) => {
            posts.value = data;
        });
    };

Iā€™m calling the getPosts function immediately after establishing the connection with Amity sdk

And there are button handlers:

    const addPostReaction = async (type: Amity.ReactableType = 'post', postId: string, reaction = 'like') => {
        await ReactionRepository.addReaction(type, postId, reaction);
    };
    const removePostReaction = async (type: Amity.ReactableType = 'post', postId: string, reaction = 'like') => {
        await ReactionRepository.removeReaction(type, postId, reaction);
    };

Thanks for the quick response, I appreciate that.

When I tried to subscribe to to PostTopic I stumbled on this error:

index.esm.js:20761 Failed to subscribe to topic 6552841adcf84ecddbf53c6b/social/community/65623a469ad025e5512235f5/post/+ Error: Amity SDK (800000): Unexpected error
    at new ASCUnknownError (index.esm.js:804:9)
    at callbackWrapper (index.esm.js:20759:29)
    at cb (index.esm.js:2709:10)
    at MqttClient._handleAck (index.esm.js:3526:8)
    at MqttClient._handlePacket (index.esm.js:2419:13)
    at work (index.esm.js:2315:13)
    at writable._write (index.esm.js:2329:6)
    at doWrite (index.esm.js:17546:140)
    at writeOrBuffer (index.esm.js:17535:6)
    at Writable.write (index.esm.js:17444:12)

To subscribe to to PostTopic, you need to run subscribeTopic function first after you call getPosts. Here is my example code

import { CommunityRepository, PostRepository, SubscriptionLevels, UserRepository, getCommunityTopic, getUserTopic, subscribeTopic } from '@amityco/ts-sdk-react-native';

  const disposers: Amity.Unsubscriber[] = [];
  let isSubscribed = false;

  const subscribePostTopic = (targetType: string, targetId: string) => {
    if (isSubscribed) return;

    if (targetType === 'user') {
      let user = {} as Amity.User; // use getUser to get user by targetId
      UserRepository.getUser(targetId, ({ data }) => {
        user = data
      });
      disposers.push(
        subscribeTopic(getUserTopic(user, SubscriptionLevels.POST), () => {
          // use callback to handle errors with event subscription
        }),
      );
      isSubscribed = true;
      return;
    }

    if (targetType === 'community') {
      CommunityRepository.getCommunity(targetId, (data) => {
        if (data.data) {
          subscribeTopic(
            getCommunityTopic(data.data, SubscriptionLevels.POST),

          );
        }
      });

    }
  };
  async function getFeed(): Promise<void> {
    const unsubscribe = PostRepository.getPosts(
      { targetId, targetType, sortBy: 'lastCreated', limit: 10, feedType: 'published' },
      (data) => {
        setPostData(data);
        subscribePostTopic(targetType, targetId)
      }
    );
  }

I already use this subscribeTopic function when getPosts please check i face same problem with like updating

  const subscribePostTopic = (targetType: string, targetId: string) => {
    if (isSubscribed) return;

    if (targetType === 'user') {
      let user = {} as Amity.User; // use getUser to get user by targetId
      UserRepository.getUser(targetId, ({ data }) => {
        user = data;
      });
      disposers.push(
        subscribeTopic(getUserTopic(user, SubscriptionLevels.POST), () => {
          // use callback to handle errors with event subscription
        }),
      );
      isSubscribed = true;
      return;
    }

    if (targetType === 'community') {
      CommunityRepository.getCommunity(targetId, (data) => {
        if (data.data) {
          subscribeTopic(getCommunityTopic(data.data, SubscriptionLevels.POST));
        }
      });
    }
  };

  async function getFeed(props): Promise<void> {
    // console.log('call');

    const unsubscribe = PostRepository.getPosts(
      {
        targetId,
        targetType,
        ...props,
      },
      (data) => {
        console.log('Data from authProvider-->', data);
        let slice = { ...data };
        setPostData(slice);
        subscribePostTopic(targetType, targetId);
      },
    );
    disposers.push(unsubscribe);
  }```

Hello! @anser786 Iā€™ll pass to my team . After our recent holiday, weā€™re actively addressing it. Stay tuned for updates. Thanks for your patience!

Hello, could you please make sure that the user has successfully joined the community before proceeding to subscribe to the topic?

Is it necessary to join before subscribe to the topic?

okay now its working thanks

1 Like