Poll Post

To create a poll post, you must first create a poll and then attach the newly created poll to a post by passing pollId in the data property.

SDK keeps all poll-related APIs in PollRepository.

To create a poll post, you need to create a poll first and then attach the newly created poll to a post by passing pollId in data property.

// createPost(parameters: { data: { pollId }, dataType: ‘poll’ ...restPayload }) => LiveObject

SDK keeps all poll-related APIs in PollRepository.

Create a Poll

You can create a poll with the following inputs:

  • question - a question that can be up to 500 characters long

  • answers - a series of two to ten answers. Each answer can be up to 200 characters long

  • answerType - indicates whether the survey allows multiple choices. The default type is AnswerType.SINGLE

    The available options are AnswerType.SINGLE and AnswerType.MULTIPLE. You can select either one or more than one option

  • closedIn - a time window indicating how long this poll will take. By default, the closedIn value is set to 30 days if the user does not set a value for it.

This method will return a LiveObject instance of the post model.

This method returns a LiveObject instance of the post model.

Create a poll post with mention

You can mention users or community members in a poll post. Typing @ will enable the mention search so you can search the users with their display name and select them from the list to mention them.

You need to create the poll first using the createPoll method. Refer to Create a Poll for the explanation of the method's parameters.

This method will return a LiveObject instance of the post model. Get the pollId from the LiveObject returned after creating the poll. You can then create the poll post using the pollId.

To add mentions in the poll post, pass the mentionees parameter to the createPost method. You can define your own structure to represent mentions. Pass your data structure inside the metadata property to save the state of how you’ll represent your mention highlight.

import { PostRepository } from '@amity-co/js-sdk';

PostRepository.createPost({
       ...payload,
    data: {
        pollId,
    },
    dataType: 'poll',
    mentionees: [{
        type: 'user',
        userIds: [
            'test-user'
        ],
    }]
   metadata: { // your data structure to save how to highlight mentioned users, etc.
	mentioned: [
		{
                    index: 0,
                    length: 4,
                    type: 'user',
                    userId: 'test-user',
                }
        ]
    }
});

Get a Poll

Once a poll post is created, you can obtain a poll ID from post data and use it to get a hand on a poll object which can later be used to access answers for a voting API.

Vote Poll

This API allows you to vote only once and it cannot be undone. However, you can have multiple choices if the poll type is .multiple.

votePoll = async (pollId: string, answerIds: string[]) => Promise<any>

Close a Poll

Only the owner/creator of a poll can close a poll before the closing time.

closePoll = async (pollId: string) => Promise<any>

Last updated