-
-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add task performance widget (#2461)
* feat: add task performance widget * fix: code review
- Loading branch information
1 parent
6ae534e
commit 32f1e2e
Showing
12 changed files
with
432 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
client/src/modules/AdminDashboard/components/DonutChart/DonutChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Pie, PieConfig } from '@ant-design/plots'; | ||
|
||
type Props = { | ||
data: { | ||
type: string; | ||
value: number; | ||
}[]; | ||
config?: Partial<PieConfig>; | ||
}; | ||
|
||
const DonutChart = ({ data, config = {} }: Props) => { | ||
const pieConfig: PieConfig = { | ||
...config, | ||
data, | ||
angleField: 'value', | ||
colorField: 'type', | ||
innerRadius: 0.6, | ||
label: { | ||
content: '', | ||
}, | ||
legend: { | ||
color: { | ||
title: false, | ||
position: 'right', | ||
rowPadding: 5, | ||
}, | ||
}, | ||
}; | ||
return <Pie {...pieConfig} />; | ||
}; | ||
|
||
export default DonutChart; |
92 changes: 92 additions & 0 deletions
92
client/src/modules/AdminDashboard/components/TaskPerformanceCard/TaskPerformanceCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Datum } from '@antv/g2plot'; | ||
import { Card, Flex, Form, Image, Select, Typography } from 'antd'; | ||
import { CourseStatsApi, CourseTaskDto, TaskPerformanceStatsDto } from 'api'; | ||
import { useActiveCourseContext } from 'modules/Course/contexts'; | ||
import dynamic from 'next/dynamic'; | ||
import { useState } from 'react'; | ||
import { useAsync } from 'react-use'; | ||
import { Colors } from '../data'; | ||
|
||
const courseStatsApi = new CourseStatsApi(); | ||
|
||
type Props = { | ||
tasks: CourseTaskDto[]; | ||
}; | ||
|
||
const { Text } = Typography; | ||
|
||
const DonutChart = dynamic(() => import('../DonutChart/DonutChart'), { ssr: false }); | ||
|
||
export const TaskPerformanceCard = ({ tasks }: Props) => { | ||
const { course } = useActiveCourseContext(); | ||
|
||
const [taskId, setTaskId] = useState<number>(); | ||
|
||
const { value: taskPerformanceStats } = useAsync(async () => { | ||
if (taskId) { | ||
const { data } = await courseStatsApi.getTaskPerformance(course.id, taskId); | ||
return data; | ||
} | ||
}, [taskId]); | ||
|
||
return ( | ||
<Card title="Task Performance"> | ||
<Form.Item name="courseTaskIds"> | ||
<Select | ||
placeholder="Select tasks" | ||
showSearch | ||
optionFilterProp="label" | ||
onChange={(value: number) => setTaskId(value)} | ||
options={tasks.map(({ name, id }) => ({ | ||
label: name, | ||
value: id, | ||
}))} | ||
/> | ||
</Form.Item> | ||
<div style={{ height: 250, width: '100%' }}> | ||
{taskPerformanceStats?.totalAchievement ? ( | ||
<DonutChart data={getChartData(taskPerformanceStats)} config={getChartConfig()} /> | ||
) : ( | ||
<Flex vertical align="center" justify="center"> | ||
<Text>No data available for this task, please select another task.</Text> | ||
<Image preview={false} src="/static/svg/err.svg" alt="Error 404" width={175} height={175} /> | ||
</Flex> | ||
)} | ||
</div> | ||
</Card> | ||
); | ||
}; | ||
|
||
function getChartConfig() { | ||
return { | ||
color: ({ type }: Datum) => { | ||
switch (type) { | ||
case 'Minimal': | ||
return Colors.Volcano; | ||
case 'Low': | ||
return Colors.Orange; | ||
case 'Moderate': | ||
return Colors.Blue; | ||
case 'High': | ||
return Colors.Lime; | ||
case 'Exceptional': | ||
return Colors.Purple; | ||
case 'Perfect': | ||
return Colors.Magenta; | ||
default: | ||
return Colors.Gray; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
function getChartData(taskPerformanceStats: TaskPerformanceStatsDto) { | ||
return [ | ||
{ type: 'Minimal', value: taskPerformanceStats.minimalAchievement }, | ||
{ type: 'Low', value: taskPerformanceStats.lowAchievement }, | ||
{ type: 'Moderate', value: taskPerformanceStats.moderateAchievement }, | ||
{ type: 'High', value: taskPerformanceStats.highAchievement }, | ||
{ type: 'Exceptional', value: taskPerformanceStats.exceptionalAchievement }, | ||
{ type: 'Perfect', value: taskPerformanceStats.perfectScores }, | ||
].filter(({ value }) => value > 0); | ||
} |
1 change: 1 addition & 0 deletions
1
client/src/modules/AdminDashboard/components/TaskPerformanceCard/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { TaskPerformanceCard } from './TaskPerformanceCard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
client/src/modules/AdminDashboard/hooks/useCourseStats/useCourseStats.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.