Skip to content

Commit

Permalink
style: fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela committed Oct 22, 2024
1 parent 21b8556 commit 4579394
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 63 deletions.
97 changes: 54 additions & 43 deletions src/poll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ export type PollOptionVotesQueryParams = {
type OptionId = string;
type PollVoteId = string;

export type PollState<SCG extends ExtendableGenerics = DefaultGenerics> = SCG['pollType'] & Omit<PollResponse<SCG>, 'own_votes' | 'id'> & {
lastActivityAt: Date; // todo: would be ideal to get this from the BE
maxVotedOptionIds: OptionId[];
ownVotes: PollVote<SCG>[];
ownVotesByOptionId: Record<OptionId, PollVoteId>; // single user can vote only once for the same option
ownAnswer?: PollAnswer; // each user can have only one answer
};
export type PollState<SCG extends ExtendableGenerics = DefaultGenerics> = SCG['pollType'] &
Omit<PollResponse<SCG>, 'own_votes' | 'id'> & {
lastActivityAt: Date; // todo: would be ideal to get this from the BE
maxVotedOptionIds: OptionId[];
ownVotes: PollVote<SCG>[];
ownVotesByOptionId: Record<OptionId, PollVoteId>; // single user can vote only once for the same option
ownAnswer?: PollAnswer; // each user can have only one answer
};

type PollInitOptions<SCG extends ExtendableGenerics = DefaultGenerics> = {
client: StreamChat<SCG>;
Expand All @@ -112,19 +113,24 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {

private getInitialStateFromPollResponse = (poll: PollInitOptions<SCG>['poll']) => {
const { own_votes, id, ...pollResponseForState } = poll;

Check warning on line 115 in src/poll.ts

View workflow job for this annotation

GitHub Actions / lint

'id' is assigned a value but never used
const { ownAnswer, ownVotes } = own_votes?.reduce<{ownVotes: PollVote<SCG>[], ownAnswer?: PollAnswer}>((acc, voteOrAnswer) => {
if (isVoteAnswer(voteOrAnswer)) {
acc.ownAnswer = voteOrAnswer;
} else {
acc.ownVotes.push(voteOrAnswer);
}
return acc;
}, {ownVotes: []}) ?? {ownVotes: []};
const { ownAnswer, ownVotes } = own_votes?.reduce<{ ownVotes: PollVote<SCG>[]; ownAnswer?: PollAnswer }>(
(acc, voteOrAnswer) => {
if (isVoteAnswer(voteOrAnswer)) {
acc.ownAnswer = voteOrAnswer;
} else {
acc.ownVotes.push(voteOrAnswer);
}
return acc;
},
{ ownVotes: [] },
) ?? { ownVotes: [] };

return {
...pollResponseForState,
lastActivityAt: new Date(),
maxVotedOptionIds: getMaxVotedOptionIds(pollResponseForState.vote_counts_by_option as PollResponse<SCG>['vote_counts_by_option']),
maxVotedOptionIds: getMaxVotedOptionIds(
pollResponseForState.vote_counts_by_option as PollResponse<SCG>['vote_counts_by_option'],
),
ownAnswer,
ownVotesByOptionId: getOwnVotesByOptionId(ownVotes),
ownVotes,
Expand All @@ -133,7 +139,7 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {

public reinitializeState = (poll: PollInitOptions<SCG>['poll']) => {
this.state.partialNext(this.getInitialStateFromPollResponse(poll));
}
};

get data(): PollState<SCG> {
return this.state.getLatestValue();
Expand All @@ -144,14 +150,14 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
if (!isPollUpdatedEvent(event)) return;
// @ts-ignore
this.state.partialNext({ ...extractPollData(event.poll), lastActivityAt: new Date(event.created_at) });
}
};

public handlePollClosed = (event: Event<SCG>) => {
if (event.poll?.id && event.poll.id !== this.id) return;
if (!isPollClosedEventEvent(event)) return;
// @ts-ignore
this.state.partialNext({ is_closed: true, lastActivityAt: new Date(event.created_at) });
}
};

public handleVoteCasted = (event: Event<SCG>) => {
if (event.poll?.id && event.poll.id !== this.id) return;
Expand Down Expand Up @@ -183,7 +189,7 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {

const { latest_answers, own_votes, ...pollEnrichData } = extractPollEnrichedData(event.poll);

Check warning on line 190 in src/poll.ts

View workflow job for this annotation

GitHub Actions / lint

'latest_answers' is assigned a value but never used

Check warning on line 190 in src/poll.ts

View workflow job for this annotation

GitHub Actions / lint

'own_votes' is assigned a value but never used
// @ts-ignore
this.state.partialNext( {
this.state.partialNext({
...pollEnrichData,
latest_answers: latestAnswers,
lastActivityAt: new Date(event.created_at),
Expand All @@ -192,7 +198,7 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
ownVotesByOptionId,
maxVotedOptionIds,
});
}
};

public handleVoteChanged = (event: Event<SCG>) => {
// this event is triggered only when event.poll.enforce_unique_vote === true
Expand All @@ -214,9 +220,10 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
];
ownVotes = ownVotes.filter((vote) => vote.id !== event.poll_vote.id);
ownAnswer = event.poll_vote;
} else { // event.poll.enforce_unique_vote === true
} else {
// event.poll.enforce_unique_vote === true
ownVotes = [event.poll_vote];
ownVotesByOptionId = {[event.poll_vote.option_id!]: event.poll_vote.id};
ownVotesByOptionId = { [event.poll_vote.option_id!]: event.poll_vote.id };

Check warning on line 226 in src/poll.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion

if (ownAnswer?.id === event.poll_vote.id) {
ownAnswer = undefined;
Expand All @@ -240,7 +247,7 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
ownVotesByOptionId,
maxVotedOptionIds,
});
}
};

public handleVoteRemoved = (event: Event<SCG>) => {
if (event.poll?.id && event.poll.id !== this.id) return;
Expand Down Expand Up @@ -277,66 +284,66 @@ export class Poll<SCG extends ExtendableGenerics = DefaultGenerics> {
ownVotesByOptionId,
maxVotedOptionIds,
});
}
};

query = async (id: string)=> {
query = async (id: string) => {
const { poll } = await this.client.getPoll(id);
// @ts-ignore
this.state.partialNext({ ...poll, lastActivityAt: new Date() });
return poll;
}
};

update = async (data: Exclude<PollData<SCG>, 'id'>) => {
return await this.client.updatePoll({ ...data, id: this.id });
}
};

partialUpdate = async (partialPollObject: PartialPollUpdate<SCG>) => {
return await this.client.partialUpdatePoll(this.id as string, partialPollObject);
}
};

close = async () => {
return await this.client.closePoll(this.id as string);
}
};

delete = async () => {
return await this.client.deletePoll(this.id as string);
}
};

createOption = async (option: PollOptionData) => {
return await this.client.createPollOption(this.id as string, option);
}
};

updateOption = async (option: PollOptionData) => {
return await this.client.updatePollOption(this.id as string, option);
}
};

deleteOption = async (optionId: string) => {
return await this.client.deletePollOption(this.id as string, optionId);
}
};

castVote = async (optionId: string, messageId: string) => {
return await this.client.castPollVote(messageId, this.id as string, { option_id: optionId });
}
};

removeVote = async (voteId: string, messageId: string) => {
return await this.client.removePollVote(messageId, this.id as string, voteId);
}
};

addAnswer = async (answerText: string, messageId: string) => {
return await this.client.addPollAnswer(messageId, this.id as string, answerText);
}
};

removeAnswer = async (answerId: string, messageId: string) => {
return await this.client.removePollVote(messageId, this.id as string, answerId);
}
};

queryAnswers = async (params: PollAnswersQueryParams) => {
return await this.client.queryPollAnswers(this.id as string, params.filter, params.sort, params.options);
}
};

queryOptionVotes = async (params: PollOptionVotesQueryParams) => {
return await this.client.queryPollVotes(this.id as string, params.filter, params.sort, params.options);
}
};
}

function getMaxVotedOptionIds(voteCountsByOption: PollResponse['vote_counts_by_option']) {
Expand All @@ -363,7 +370,9 @@ function getOwnVotesByOptionId<SCG extends ExtendableGenerics = DefaultGenerics>
}, {});
}

export function extractPollData <SCG extends ExtendableGenerics = DefaultGenerics>(pollResponse: PollResponse<SCG>): PollData<SCG> {
export function extractPollData<SCG extends ExtendableGenerics = DefaultGenerics>(
pollResponse: PollResponse<SCG>,
): PollData<SCG> {
return {
allow_answers: pollResponse.allow_answers,
allow_user_suggested_options: pollResponse.allow_user_suggested_options,
Expand All @@ -374,11 +383,13 @@ export function extractPollData <SCG extends ExtendableGenerics = DefaultGeneric
max_votes_allowed: pollResponse.max_votes_allowed,
name: pollResponse.name,
options: pollResponse.options,
voting_visibility: pollResponse.voting_visibility
voting_visibility: pollResponse.voting_visibility,
};
}

export function extractPollEnrichedData <SCG extends ExtendableGenerics = DefaultGenerics>(pollResponse: PollResponse<SCG>): PollEnrichData<SCG> {
export function extractPollEnrichedData<SCG extends ExtendableGenerics = DefaultGenerics>(
pollResponse: PollResponse<SCG>,
): PollEnrichData<SCG> {
return {
answers_count: pollResponse.answers_count,
latest_answers: pollResponse.latest_answers,
Expand Down
42 changes: 22 additions & 20 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3010,22 +3010,23 @@ export type UpdatePollAPIResponse<StreamChatGenerics extends ExtendableGenerics

export type PollResponse<
StreamChatGenerics extends ExtendableGenerics = DefaultGenerics
> = StreamChatGenerics['pollType'] & PollEnrichData<StreamChatGenerics> & {
created_at: string;
created_by: UserResponse<StreamChatGenerics> | null;
created_by_id: string;
enforce_unique_vote: boolean;
id: string;
max_votes_allowed: number;
name: string;
options: PollOption<StreamChatGenerics>[];
updated_at: string;
allow_answers?: boolean;
allow_user_suggested_options?: boolean;
description?: string;
is_closed?: boolean;
voting_visibility?: VotingVisibility;
};
> = StreamChatGenerics['pollType'] &
PollEnrichData<StreamChatGenerics> & {
created_at: string;
created_by: UserResponse<StreamChatGenerics> | null;
created_by_id: string;
enforce_unique_vote: boolean;
id: string;
max_votes_allowed: number;
name: string;
options: PollOption<StreamChatGenerics>[];
updated_at: string;
allow_answers?: boolean;
allow_user_suggested_options?: boolean;
description?: string;
is_closed?: boolean;
voting_visibility?: VotingVisibility;
};

export type PollOption<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
created_at: string;
Expand All @@ -3042,9 +3043,7 @@ export enum VotingVisibility {
public = 'public',
}

export type PollEnrichData<
StreamChatGenerics extends ExtendableGenerics = DefaultGenerics
> = {
export type PollEnrichData<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
answers_count: number;
latest_answers: PollAnswer<StreamChatGenerics>[]; // not updated with WS events, ordered DESC by created_at, seems like updated_at cannot be different from created_at
latest_votes_by_option: Record<string, PollVote<StreamChatGenerics>[]>; // not updated with WS events; always null in anonymous polls
Expand All @@ -3069,7 +3068,10 @@ export type PollData<
voting_visibility?: VotingVisibility;
};

export type CreatePollData<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = Partial<PollData<StreamChatGenerics>> & Pick<PollData<StreamChatGenerics>, 'name'>
export type CreatePollData<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = Partial<
PollData<StreamChatGenerics>
> &
Pick<PollData<StreamChatGenerics>, 'name'>;

export type PartialPollUpdate<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
set?: Partial<PollData<StreamChatGenerics>>;
Expand Down

0 comments on commit 4579394

Please sign in to comment.