Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add number of respondents #142

Merged
merged 2 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public class QuestionResponseDto
public string QuestionText { get; set; } = null!;
public string QuestionDescription { get; set; } = null!;
public bool IsMultipleChoice { get; set; }

public DateTimeOffset CreatedAt { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public virtual ICollection<GetChoiceDto>? Choices { get; set; }
public int NumberOfRespondents { get; set; }

public static QuestionResponseDto CreateFromEntity(Question question, ICollection<GetChoiceDto> getChoiceDto)
{
Expand All @@ -28,8 +28,10 @@ public static QuestionResponseDto CreateFromEntity(Question question, ICollectio
IsMultipleChoice = question.IsMultipleChoice,
CreatedAt = question.CreatedAt,
UpdatedAt = question.UpdatedAt,
// NumberOfRespondents = //TODO
Choices = getChoiceDto
Choices = getChoiceDto,
NumberOfRespondents = question.ResponseUsers != null
? question.ResponseUsers.Select(ru => ru.UserId).Distinct().Count()
: 0,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SurveyDto
public DateTimeOffset? UpdatedAt { get; set; }
public DateTimeOffset? LastSyncedAt { get; set; }
public virtual ICollection<SurveyElementDto>? SurveyBlocks { get; set; }
public int TotalParticipants { get; set; }

public static SurveyDto CreateFromEntity(Domain.Entities.Survey.Survey survey)
{
Expand All @@ -23,6 +24,7 @@ public static SurveyDto CreateFromEntity(Domain.Entities.Survey.Survey survey)
UpdatedAt = survey.UpdatedAt,
LastSyncedAt = survey.LastSyncedAt,
SurveyBlocks = survey.SurveyBlocks?.Select(SurveyElementDto.CreateFromEntity).ToList(),
TotalParticipants = survey.Users?.Count ?? 0
};
}
}
13 changes: 11 additions & 2 deletions frontend/src/app/components/SurveyBlock/SurveyBlockRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@ const SurveyBlockRenderer = ({ questionId }: { questionId: string }) => {

if (isLoading) return <div>Henter resultater...</div>;
if (error) return <div>Error: {error.message}</div>;

return (
<section className="flex text-black gap-4 flex-col lg:flex-row lg:gap-6">
<QuestionContainer data={data} />
<AnswerContainer tabs={tabs}>
<BarChart {...barChartData} />
{data.isMultipleChoice && <DotPlotChart data={dotPlot} />}
<BarChart
{...barChartData}
numberOfRespondents={data.numberOfRespondents}
/>
{data.isMultipleChoice && (
<DotPlotChart
data={dotPlot}
numberOfRespondents={data?.numberOfRespondents || 0}
/>
)}
</AnswerContainer>
</section>
);
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/app/components/charts/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ import { chartConfig, getGridShapes, useChartTheme } from "./chartConfig";
import { BarChartData } from "@/app/hooks/useGetBarChartData";
import ChartCounter from "./ChartCounter";

interface Props extends BarChartData {
numberOfRespondents: number;
}

// lazy load 'react-plotly.js'
const Plot = dynamic(() => import("react-plotly.js"), { ssr: false });

const BarChart = ({ x, y }: BarChartData) => {
const BarChart = ({ x, y, numberOfRespondents }: Props) => {
// check dark mode and set graph theme
const theme = useChartTheme();
const numberOfChoices = y.length;
const plotHeight = numberOfChoices * 30;
const shapes = getGridShapes(y);

const chartData: Partial<Plotly.Data>[] = [
{
x,
Expand Down Expand Up @@ -77,7 +80,7 @@ const BarChart = ({ x, y }: BarChartData) => {
/>
</div>
<div className="mt-auto ml-auto">
<ChartCounter number={50} total={200} />
<ChartCounter numberOfRespondents={numberOfRespondents} />
</div>
</div>
);
Expand Down
14 changes: 9 additions & 5 deletions frontend/src/app/components/charts/ChartCounter.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { useSurveyStructure } from "@/app/hooks/useSurveyStructure";
import React from "react";

interface ChartCounterProps {
number: number;
total: number;
numberOfRespondents: number;
}

const ChartCounter = ({ number, total }: ChartCounterProps) => {
const percentage = total > 0 ? ((number / total) * 100).toFixed(0) : "0";
const ChartCounter = ({ numberOfRespondents }: ChartCounterProps) => {
const { data } = useSurveyStructure();
const percentage =
data.totalParticipants > 0
? ((numberOfRespondents / data.totalParticipants) * 100).toFixed(0)
: "0";

return (
<div className="flex items-center space-x-1 text-white">
<div>Antall svar:</div>
<div className="font-bold">{number.toString()}</div>
<div className="font-bold">{numberOfRespondents.toString()}</div>
<div className="font-bold">({percentage}%)</div>
</div>
);
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/app/components/charts/DotPlotChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ const Plot = dynamic(() => import("react-plotly.js"), {
interface DotPlotChartJsonProps {
title?: string;
data: DotPlot;
numberOfRespondents: number;
}

const DotPlotChart = ({ title, data }: DotPlotChartJsonProps) => {
const DotPlotChart = ({
title,
data,
numberOfRespondents,
}: DotPlotChartJsonProps) => {
//check darkmode and set graph theme
const theme = useChartTheme();

Expand Down Expand Up @@ -117,7 +122,7 @@ const DotPlotChart = ({ title, data }: DotPlotChartJsonProps) => {
style={{ width: "100%", height: plotHeight }}
/>
<div className="mt-auto mb-2 ml-auto">
<ChartCounter number={50} total={200} />
<ChartCounter numberOfRespondents={numberOfRespondents} />
</div>
</div>
);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/types/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface SurveyBlock extends BaseEntity {
export interface Survey extends BaseEntity {
name: string;
surveyId: string;
totalParticipants: number;
lastSyncedAt: string;
surveyBlocks: SurveyBlock[];
}
Expand All @@ -41,6 +42,7 @@ export interface Answer extends BaseEntity {
createdAt: string;
updatedAt: string | null;
choices: Choice[];
numberOfRespondents: number;
}

export interface Choice extends BaseEntity {
Expand Down