File tree Expand file tree Collapse file tree 4 files changed +66
-4
lines changed
docusaurus/docs/reactnative/hooks/poll
package/src/components/Poll/hooks Expand file tree Collapse file tree 4 files changed +66
-4
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ id: use-poll-option-votes-pagination
33title : usePollOptionVotesPagination
44---
55
6- A hook that queries votes for a given option in a ` Poll ` and returns them in a paginated fashion. It can be used whenever we want to view all votes in a given ` poll ` , like in the [ ` PollOptionFullResults ` component] ( ../../ui-components/poll-option-full-results.mdx ) for example.
6+ A hook that queries votes for a given option in a ` Poll ` and returns them in a paginated fashion. It can be used whenever we want to view all votes for an option in a given ` poll ` , like in the [ ` PollOptionFullResults ` component] ( ../../ui-components/poll-option-full-results.mdx ) for example.
77
88## Properties
99
Original file line number Diff line number Diff line change @@ -11,10 +11,33 @@ export type UsePollAnswersPaginationParams = {
1111 paginationParams ?: PollAnswersQueryParams ;
1212} ;
1313
14+ export type UsePollAnswersReturnType = {
15+ error : Error | undefined ;
16+ hasNextPage : boolean ;
17+ loading : boolean ;
18+ loadMore : ( ) => void ;
19+ next : string | null | undefined ;
20+ pollAnswers : PollAnswer [ ] ;
21+ } ;
22+
23+ /**
24+ * A hook that queries answers for a given Poll and returns them in a paginated fashion.
25+ * Should be used instead of the latest_answers property within the reactive state in the
26+ * event that we need more than the top 10 answers. The returned property pollAnswers will
27+ * automatically be updated and trigger a state change when paginating further.
28+ *
29+ * @param loadFirstPage {boolean} Signifies whether the first page should be automatically loaded whenever
30+ * the hook is first called.
31+ * @param paginationParams {PollAnswersQueryParams} The pagination params we might want to use for our custom
32+ * needs when invoking the hook.
33+ *
34+ * @returns {UsePollAnswersReturnType } An object containing all of the needed pagination values as well as the
35+ * answers.
36+ **/
1437export const usePollAnswersPagination = ( {
1538 loadFirstPage = true ,
1639 paginationParams,
17- } : UsePollAnswersPaginationParams = { } ) => {
40+ } : UsePollAnswersPaginationParams = { } ) : UsePollAnswersReturnType => {
1841 const { poll } = usePollContext ( ) ;
1942 const { client } = useChatContext ( ) ;
2043
Original file line number Diff line number Diff line change @@ -11,11 +11,37 @@ export type UsePollOptionVotesPaginationParams = {
1111 paginationParams ?: PollOptionVotesQueryParams ;
1212} ;
1313
14+ export type UsePollVotesReturnType = {
15+ error : Error | undefined ;
16+ hasNextPage : boolean ;
17+ loading : boolean ;
18+ loadMore : ( ) => void ;
19+ next : string | null | undefined ;
20+ votes : PollVote [ ] ;
21+ } ;
22+
23+ /**
24+ * A hook that queries votes for a given Poll and returns them in a paginated fashion.
25+ * Should be used instead of the latest_votes_by_option property within the reactive state in the
26+ * event that we need more than the top 10 votes for an option. The returned property votes will
27+ * automatically be updated and trigger a state change when paginating further. Querying for votes
28+ * can only be done on an option by option basis.
29+ *
30+ * @param option {PollOption} The option for which we want to load the votes.
31+ * @param loadFirstPage {boolean} Signifies whether the first page should be automatically loaded whenever
32+ * the hook is first called.
33+ * @param paginationParams {PollOptionVotesQueryParams} The pagination params we might want to use for our custom
34+ * needs when invoking the hook.
35+ *
36+ * @returns {UsePollVotesReturnType } An object containing all of the needed pagination values as well as the
37+ * answers.
38+ **/
39+
1440export const usePollOptionVotesPagination = ( {
1541 loadFirstPage = true ,
1642 option,
1743 paginationParams,
18- } : UsePollOptionVotesPaginationParams ) => {
44+ } : UsePollOptionVotesPaginationParams ) : UsePollVotesReturnType => {
1945 const { poll } = usePollContext ( ) ;
2046 const { client } = useChatContext ( ) ;
2147
Original file line number Diff line number Diff line change 11import { useCallback } from 'react' ;
22
33import {
4+ APIResponse ,
5+ CastVoteAPIResponse ,
46 PollAnswer ,
57 PollOption ,
68 PollState ,
79 PollVote ,
10+ UpdatePollAPIResponse ,
811 UserResponse ,
912 VotingVisibility ,
1013} from 'stream-chat' ;
@@ -32,6 +35,16 @@ export type UsePollStateSelectorReturnType = {
3235 voting_visibility : VotingVisibility | undefined ;
3336} ;
3437
38+ export type UsePollStateReturnType <
39+ StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
40+ > = UsePollStateSelectorReturnType & {
41+ addComment : (
42+ answerText : string ,
43+ ) => Promise < APIResponse & CastVoteAPIResponse < StreamChatGenerics > > ;
44+ addOption : ( optionText : string ) => Promise < void > ;
45+ endVote : ( ) => Promise < APIResponse & UpdatePollAPIResponse < StreamChatGenerics > > ;
46+ } ;
47+
3548const selector = < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics > (
3649 nextValue : PollState < StreamChatGenerics > ,
3750) : UsePollStateSelectorReturnType => ( {
@@ -51,7 +64,7 @@ const selector = <StreamChatGenerics extends DefaultStreamChatGenerics = Default
5164 voting_visibility : nextValue . voting_visibility ,
5265} ) ;
5366
54- export const usePollState = ( ) => {
67+ export const usePollState = ( ) : UsePollStateReturnType => {
5568 const { message, poll } = usePollContext ( ) ;
5669 const {
5770 allow_answers,
You can’t perform that action at this time.
0 commit comments