Skip to content

Commit

Permalink
chore: Added average to rating summary (#1966)
Browse files Browse the repository at this point in the history
Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
  • Loading branch information
Dhruwang and mattinannt committed Jan 26, 2024
1 parent 6340d49 commit 92417d4
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Headline from "@/app/(app)/environments/[environmentId]/surveys/[surveyId]/(analysis)/summary/components/Headline";
import { questionTypes } from "@/app/lib/questions";
import { InboxStackIcon } from "@heroicons/react/24/solid";
import { CircleSlash2, SmileIcon, StarIcon } from "lucide-react";
import { useMemo } from "react";

import type { TSurveyQuestionSummary } from "@formbricks/types/surveys";
Expand Down Expand Up @@ -79,6 +80,26 @@ export default function RatingSummary({ questionSummary }: RatingSummaryProps) {
return total;
}, [results]);

const averageRating = useMemo(() => {
let total = 0;
let count = 0;
questionSummary.responses.forEach((response) => {
if (response.value && typeof response.value === "number") {
total += response.value;
count += 1;
}
});
const average = count > 0 ? total / count : 0;
return parseFloat(average.toFixed(2));
}, [questionSummary]);

const getIconBasedOnScale = useMemo(() => {
const scale = questionSummary.question.scale;
if (scale === "number") return <CircleSlash2 className="h-4 w-4" />;
else if (scale === "star") return <StarIcon fill="rgb(250 204 21)" className="h-4 w-4 text-yellow-400" />;
else if (scale === "smiley") return <SmileIcon className="h-4 w-4" />;
}, [questionSummary]);

return (
<div className=" rounded-lg border border-slate-200 bg-slate-50 shadow-sm">
<div className="space-y-2 px-4 pb-5 pt-6 md:px-6">
Expand All @@ -93,6 +114,10 @@ export default function RatingSummary({ questionSummary }: RatingSummaryProps) {
<InboxStackIcon className="mr-2 h-4 w-4 " />
{totalResponses} responses
</div>
<div className="flex items-center space-x-2 rounded-lg bg-slate-100 p-2">
{getIconBasedOnScale}
<div>Overall: {averageRating}</div>
</div>
{!questionSummary.question.required && (
<div className="flex items-center rounded-lg bg-slate-100 p-2">Optional</div>
)}
Expand Down

1 comment on commit 92417d4

@vercel
Copy link

@vercel vercel bot commented on 92417d4 Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

formbricks-com – ./apps/formbricks-com

formbricks-com.vercel.app
formbricks-com-git-main-formbricks.vercel.app
www.formbricks.com
formbricks-com-formbricks.vercel.app
formbricks.com

Please sign in to comment.