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

Features/answers visualization #14

Merged
merged 15 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class Meta:
class UserAnswerSummarySerializer(serializers.ModelSerializer):
class Meta:
model = UserAnswerSummary
fields = ['pk', 'user', 'exercise', 'max_points', 'answer_count', 'latest']
fields = ['pk', 'user', 'exercise', 'max_points', 'answer_count', 'latest']
3 changes: 1 addition & 2 deletions core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def create(self, request, off_pk=None, ex_slug=None):
return Response(answer.data, status=status.HTTP_201_CREATED)

return Response(answer.errors, status=status.HTTP_400_BAD_REQUEST)


def user_filter(request):
user_pk = request.user.pk
Expand Down Expand Up @@ -105,4 +105,3 @@ def list_summaries_for_exercise(request, off_pk, ex_slug):
all_summaries_json = UserAnswerSummarySerializer(all_summaries, many=True)

return Response(all_summaries_json.data, status=status.HTTP_200_OK)

23,303 changes: 23,303 additions & 0 deletions frontend/frontend-dev/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion frontend/frontend-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"axios": "^0.21.1",
"chart.js": "^3.5.1",
"framer-motion": "^4.1.17",
"i18next": "^20.4.0",
"react": "^17.0.2",
"react-chartjs-2": "^3.0.4",
"react-dom": "^17.0.2",
"react-hook-form": "^7.12.2",
"react-i18next": "^11.11.4",
Expand Down Expand Up @@ -79,4 +81,4 @@
}
]
}
}
}
60 changes: 60 additions & 0 deletions frontend/frontend-dev/src/fragments/AnswersChart/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { memo, useMemo } from "react";
import { Bar, Doughnut } from "react-chartjs-2";

interface IAnswersChartProps {
Copy link
Contributor

Choose a reason for hiding this comment

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

Great! This should be a naming convention. I've created issue #16 to fix the previous code.

title?: string;
options: string[];
selectedOptionsCount: number[];
}

// docs for customisation: https://github.com/reactchartjs/react-chartjs-2
function UnmemoizedAnswersChart({
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nice component. Could you write a story for this component? Also, some tests would be nice is there anything that is testable here? You can check the Button component for some examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would it be more appropriate to move it to /components?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I guess it makes sense. Also, what do you think about renaming this component? I don't see anything that is specific to "answers". Since you are moving it to /components, what about picking a more generic name?

title,
options,
selectedOptionsCount
}: IAnswersChartProps) {
const data = useMemo(() => {
return {
labels: options,
datasets: [
{
label: "# of answers",
data: selectedOptionsCount,
backgroundColor: [
"#ff638433",
"#76f25d33",
"#36a2eb33",
"#ffce5633",
"#765df233",
"#21fcf233",
"#ff9f4033"
],
borderColor: [
"#ff6384",
"#76f25d",
"#36a2eb",
"#ffce56",
"#765df2",
"#21fcf2",
"#ff9f40"
],
borderWidth: 2
}
]
};
}, [options, selectedOptionsCount]);

return (
<div className="flex flex-cols align-center justify-center">
<div style={{ maxHeight: 300 }}>
Copy link
Contributor

Choose a reason for hiding this comment

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

This and the style in the div below should be based on the theme in some way. If none of tailwinds defaults work in this case you can define some extensions on tailwind.config.js.

<Doughnut data={data} />
</div>

<div style={{ width: 500 }}>
<Bar data={data} options={{ maintainAspectRatio: false }} />
</div>
</div>
);
}

export const AnswersChart = memo(UnmemoizedAnswersChart);
Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't know this. Thanks! Created issue #18

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good practice for Pure Components specially if they are costly to render

Copy link
Contributor

Choose a reason for hiding this comment

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

That's great, thanks for the reference! I've updated #18 with your reference

27 changes: 20 additions & 7 deletions frontend/frontend-dev/src/fragments/ExerciseDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import Button from "../../components/Button";
import Container from "../../components/Container";
import { useUser } from "../../contexts/user-context";
import { dynamicPathname } from "../../helpers";
import { routes } from "../../routes";
import { useSummaryListForExercise } from "../../services/exercises";

interface ExerciseDetailsProps {
Expand All @@ -16,41 +18,52 @@ const ExerciseDetails = ({ offering, slug }: ExerciseDetailsProps) => {
const {
summaryList,
loading,
refresh: refreshSummaryList,
refresh: refreshSummaryList
} = useSummaryListForExercise(offering, slug, token);

const { t } = useTranslation();

const [numSubmissions, setNumSubmissions] = useState<number>(0);
const [numUniqueUsers, setNumUniqueUsers] = useState<number>(0);
const [lastRefresh, setLastRefresh] = useState<Date>();

useEffect(() => {
if (loading) return;
setNumUniqueUsers(summaryList.length);
setNumSubmissions(
summaryList
.map((answerSummary) => answerSummary.answer_count)
.reduce((a, b) => a + b, 0),
.reduce((a, b) => a + b, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if you erased this manually or if it was some formatter, but this is the reason we keep the trailing commas.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was done by vscode formatter, but I've just set it to allow trailing commas

);
setLastRefresh(new Date());
}, [loading, summaryList]);

const answersRoute = dynamicPathname(routes.EXERCISE_ANSWERS, {
offering,
slug
});

return (
<Container className="bg-gray-100 p-4 my-4 flex">
<div className="bg-gray-100 p-4 flex flex-col">
<div className="flex-grow">
<p> {slug} </p>
{!!lastRefresh && (
<p>
{t("submissions sent before", {
total: numSubmissions,
users: numUniqueUsers,
lastUpdate: lastRefresh?.toLocaleString(),
lastUpdate: lastRefresh?.toLocaleString()
})}
</p>
)}
</div>
<Button onClick={refreshSummaryList}>{t("Reload")}</Button>
</Container>
<div className="flex flex-row mt-4 justify-around">
<Link to={answersRoute}>
<Button>{t("See details")}</Button>
</Link>
<Button onClick={refreshSummaryList}>{t("Reload")}</Button>
</div>
</div>
);
};

Expand Down
22 changes: 22 additions & 0 deletions frontend/frontend-dev/src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
*
* @param route Dynamic route. Ex:
* ```typescript
* "/api/offerings/:off_pk/exercises/:ex_slug/answers/"
* ```
* @param params Object with dynamic params. Ex:
* ```typescript
* {off_pk: 1, ex_slug: 'quiz-0'})
* ```
* @returns Properly filled route
* ```typescript
* "/api/offerings/1/exercises/quiz-0/answers"
* ```
*/
export function dynamicPathname(route: string, params: Record<string, string|number>) {
for (let [key, value] of Object.entries(params)) {
const regexp = new RegExp(`:${key}`, "g");
route = route.replace(regexp, String(value));
}
return route;
}
toshikurauchi marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 18 additions & 7 deletions frontend/frontend-dev/src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { RouteProps } from "react-router";
import { User } from "../models/User";
import ExerciseAnswers from "../screens/ExerciseAnswers";
import InstructorHome from "../screens/InstructorHome";
import Login from "../screens/Login";
import StudentHome from "../screens/StudentHome";

export enum routes {
STUDENT_HOME = "/",
INSTRUCTOR_HOME = "/instructor",
LOGIN = "/login",
EXERCISE_ANSWERS = "/instructor/answers/:offering/:slug/",
LOGIN = "/login"
}

interface RouteData {
Expand All @@ -22,25 +24,34 @@ routesData[routes.STUDENT_HOME] = {
props: {
path: routes.STUDENT_HOME,
exact: true,
component: StudentHome,
},
component: StudentHome
}
};
routesData[routes.INSTRUCTOR_HOME] = {
title: "Instructor Dashboard",
permissionTest: (user: User | null): boolean => !!user?.isStaff,
props: {
path: routes.INSTRUCTOR_HOME,
exact: true,
component: InstructorHome,
},
component: InstructorHome
}
};
routesData[routes.EXERCISE_ANSWERS] = {
title: "Exercise Answers",
permissionTest: (user: User | null): boolean => !!user?.isStaff,
props: {
path: routes.EXERCISE_ANSWERS,
exact: true,
component: ExerciseAnswers
}
};
routesData[routes.LOGIN] = {
title: "Login",
props: {
path: routes.LOGIN,
exact: true,
component: Login,
},
component: Login
}
};

export const navRoutes: string[] = [routes.INSTRUCTOR_HOME];
Loading