Fix summary showing NaN values when adding new Consent or CTA question (#518)

This commit is contained in:
tyjkerr
2023-07-11 14:54:59 +07:00
committed by GitHub
parent 7be6879afb
commit ce8a8df091
2 changed files with 19 additions and 12 deletions

View File

@@ -16,10 +16,11 @@ interface ChoiceResult {
export default function CTASummary({ questionSummary }: CTASummaryProps) {
const ctr: ChoiceResult = useMemo(() => {
const clickedAbs = questionSummary.responses.filter((response) => response.value === "clicked").length;
const count = questionSummary.responses.length;
if (count === 0) return { count: 0, percentage: 0 };
return {
count: questionSummary.responses.length,
percentage: clickedAbs / questionSummary.responses.length,
count: count,
percentage: clickedAbs / count,
};
}, [questionSummary]);

View File

@@ -2,6 +2,7 @@ import { ConsentQuestion } from "@formbricks/types/questions";
import type { QuestionSummary } from "@formbricks/types/responses";
import { ProgressBar } from "@formbricks/ui";
import { InboxStackIcon } from "@heroicons/react/24/solid";
import { useMemo } from "react";
interface ConsentSummaryProps {
questionSummary: QuestionSummary<ConsentQuestion>;
@@ -16,15 +17,20 @@ interface ChoiceResult {
}
export default function ConsentSummary({ questionSummary }: ConsentSummaryProps) {
const total = questionSummary.responses.length;
const clickedAbs = questionSummary.responses.filter((response) => response.value !== "dismissed").length;
const ctr: ChoiceResult = {
count: total,
acceptedCount: clickedAbs,
acceptedPercentage: clickedAbs / total,
dismissedCount: total - clickedAbs,
dismissedPercentage: 1 - clickedAbs / total,
};
const ctr: ChoiceResult = useMemo(() => {
const total = questionSummary.responses.length;
const clickedAbs = questionSummary.responses.filter((response) => response.value !== "dismissed").length;
if (total === 0) {
return { count: 0, acceptedCount: 0, acceptedPercentage: 0, dismissedCount: 0, dismissedPercentage: 0 };
}
return {
count: total,
acceptedCount: clickedAbs,
acceptedPercentage: clickedAbs / total,
dismissedCount: total - clickedAbs,
dismissedPercentage: 1 - clickedAbs / total,
};
}, [questionSummary]);
return (
<div className=" rounded-lg border border-slate-200 bg-slate-50 shadow-sm">