-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create students countries stats widget (#2406)
- Loading branch information
1 parent
47e906c
commit 5ca3aaa
Showing
18 changed files
with
3,564 additions
and
12 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
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
21 changes: 21 additions & 0 deletions
21
client/src/modules/AdminDashboard/components/StudentsCountriesCard/StudentsCountriesCard.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,21 @@ | ||
import { Card } from 'antd'; | ||
import { StudentsCountriesStatsDto } from 'api'; | ||
import dynamic from 'next/dynamic'; | ||
|
||
type Props = { | ||
studentsCountriesStats: StudentsCountriesStatsDto; | ||
studentsActiveCount: number; | ||
}; | ||
|
||
const StudentsCountriesChart = dynamic(() => import('./StudentsCountriesChart'), { ssr: false }); | ||
|
||
export const StudentsCountriesCard = ({ studentsCountriesStats, studentsActiveCount }: Props) => { | ||
const { countries } = studentsCountriesStats; | ||
return ( | ||
<Card title="Students Countries Stats"> | ||
<div style={{ height: 400, width: '100%' }}> | ||
<StudentsCountriesChart data={countries} studentsActiveCount={studentsActiveCount} /> | ||
</div> | ||
</Card> | ||
); | ||
}; |
60 changes: 60 additions & 0 deletions
60
...nt/src/modules/AdminDashboard/components/StudentsCountriesCard/StudentsCountriesChart.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,60 @@ | ||
import { Bar, BarConfig } from '@ant-design/plots'; | ||
import { Flex, Image, Typography } from 'antd'; | ||
import { CountryStatDto } from 'api'; | ||
import { useCallback, useMemo } from 'react'; | ||
|
||
type Props = { | ||
data: CountryStatDto[]; | ||
studentsActiveCount: number; | ||
}; | ||
|
||
/* | ||
* Defining the Datum type manually as it cannot be imported directly from @ant-design/plots. | ||
* This type is inferred from the 'data' property of the Bar component's first parameter. | ||
* This approach is necessary because @ant-design/plots does not explicitly export the Datum type. | ||
* Use this type definition cautiously and review it if the library updates. | ||
*/ | ||
type Datum = Parameters<typeof Bar>[0]['data'][number]; | ||
|
||
const { Text } = Typography; | ||
|
||
function StudentsCountriesChart({ data, studentsActiveCount }: Props) { | ||
const tooltipFormatter = useCallback( | ||
(datum: Datum) => { | ||
const percentage = studentsActiveCount ? Math.ceil((datum.studentsCount / studentsActiveCount) * 100) : 0; | ||
return { | ||
name: 'Number of Students', | ||
value: `${datum.studentsCount} (${percentage}%)`, | ||
}; | ||
}, | ||
[studentsActiveCount], | ||
); | ||
|
||
const config: BarConfig = useMemo( | ||
() => ({ | ||
data, | ||
yField: 'country', | ||
xField: 'studentsCount', | ||
yAxis: { | ||
label: { autoRotate: false }, | ||
}, | ||
tooltip: { formatter: tooltipFormatter }, | ||
xAxis: { title: { text: 'Number of Students' } }, | ||
scrollbar: { type: 'vertical' }, | ||
}), | ||
[data, tooltipFormatter], | ||
); | ||
|
||
if (!data.length) { | ||
return ( | ||
<Flex vertical gap="middle" align="center" justify="center"> | ||
<Text strong>No student data available to display</Text> | ||
<Image preview={false} src="/static/svg/err.svg" alt="Error 404" width={175} height={175} /> | ||
</Flex> | ||
); | ||
} | ||
|
||
return <Bar {...config} />; | ||
} | ||
|
||
export default StudentsCountriesChart; |
1 change: 1 addition & 0 deletions
1
client/src/modules/AdminDashboard/components/StudentsCountriesCard/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 { StudentsCountriesCard } from './StudentsCountriesCard'; |
19 changes: 19 additions & 0 deletions
19
client/src/modules/AdminDashboard/components/StudentsStatsCard/StudentsStatsCard.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,19 @@ | ||
import { Card } from 'antd'; | ||
import { CourseStatsDto } from 'api'; | ||
import dynamic from 'next/dynamic'; | ||
|
||
type Props = { | ||
studentsStats: CourseStatsDto; | ||
}; | ||
|
||
const StudentsStatsChart = dynamic(() => import('./StudentsStatsChart'), { ssr: false }); | ||
|
||
export const StudentsStatsCard = ({ studentsStats }: Props) => { | ||
return ( | ||
<Card title="Active Students Stats"> | ||
<div style={{ height: 200, width: '100%' }}> | ||
<StudentsStatsChart studentsStats={studentsStats} /> | ||
</div> | ||
</Card> | ||
); | ||
}; |
23 changes: 23 additions & 0 deletions
23
client/src/modules/AdminDashboard/components/StudentsStatsCard/StudentsStatsChart.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,23 @@ | ||
import { Liquid } from '@ant-design/plots'; | ||
import { CourseStatsDto } from 'api'; | ||
|
||
type Props = { | ||
studentsStats: CourseStatsDto; | ||
}; | ||
|
||
function StudentsStatsChart({ studentsStats }: Props) { | ||
const percent = studentsStats.studentsActiveCount / studentsStats.studentsTotalCount; | ||
const config = { | ||
percent: percent, | ||
outline: { | ||
border: 4, | ||
distance: 8, | ||
}, | ||
wave: { | ||
length: 128, | ||
}, | ||
}; | ||
return <Liquid {...config} />; | ||
} | ||
|
||
export default StudentsStatsChart; |
1 change: 1 addition & 0 deletions
1
client/src/modules/AdminDashboard/components/StudentsStatsCard/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 { StudentsStatsCard } from './StudentsStatsCard'; |
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 { useCourseStats } from './useCourseStats/useCourseStats'; |
22 changes: 22 additions & 0 deletions
22
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { message } from 'antd'; | ||
import { CourseStatsApi } from 'api'; | ||
import { useAsync } from 'react-use'; | ||
|
||
const courseStatsApi = new CourseStatsApi(); | ||
|
||
export function useCourseStats(courseId: number) { | ||
return useAsync(async () => { | ||
try { | ||
const [studentsCountries, studentsStats] = await Promise.all([ | ||
courseStatsApi.getCourseStudentCountries(courseId), | ||
courseStatsApi.getCourseStats(courseId), | ||
]); | ||
return { | ||
studentsCountries: studentsCountries.data, | ||
studentsStats: studentsStats.data, | ||
}; | ||
} catch (error) { | ||
message.error('Something went wrong, please try to reload the page later'); | ||
} | ||
}, [courseId]); | ||
} |
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 { default as AdminDashboard } from './pages/AdminDashboard'; |
72 changes: 72 additions & 0 deletions
72
client/src/modules/AdminDashboard/pages/AdminDashboard.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,72 @@ | ||
import { PageLayout } from 'components/PageLayout'; | ||
import { useActiveCourseContext } from 'modules/Course/contexts'; | ||
import Masonry from 'react-masonry-css'; | ||
import { StudentsCountriesCard } from '../components/StudentsCountriesCard'; | ||
import { useCourseStats } from '../hooks'; | ||
import { StudentsStatsCard } from '../components/StudentsStatsCard'; | ||
import css from 'styled-jsx/css'; | ||
|
||
const gapSize = 24; | ||
|
||
function AdminDashboard() { | ||
const { course } = useActiveCourseContext(); | ||
const { loading, value: stats } = useCourseStats(course.id); | ||
|
||
const masonryBreakPoints = { | ||
default: 4, | ||
1100: 3, | ||
700: 2, | ||
500: 1, | ||
}; | ||
|
||
const cards = [ | ||
stats?.studentsCountries && { | ||
title: 'studentsCountriesCard', | ||
component: ( | ||
<StudentsCountriesCard | ||
studentsCountriesStats={stats.studentsCountries} | ||
studentsActiveCount={stats.studentsStats.studentsActiveCount} | ||
/> | ||
), | ||
}, | ||
stats?.studentsStats && { | ||
title: 'studentsStatsCard', | ||
component: <StudentsStatsCard studentsStats={stats.studentsStats} />, | ||
}, | ||
].filter(Boolean); | ||
|
||
return ( | ||
<PageLayout loading={loading} title="Dashboard" background="#F0F2F5" showCourseName> | ||
<Masonry | ||
breakpointCols={masonryBreakPoints} | ||
className={masonryClassName} | ||
columnClassName={masonryColumnClassName} | ||
> | ||
{cards.map(({ title, component }) => ( | ||
<div style={{ marginBottom: gapSize }} key={title}> | ||
{component} | ||
</div> | ||
))} | ||
</Masonry> | ||
{masonryStyles} | ||
{masonryColumnStyles} | ||
</PageLayout> | ||
); | ||
} | ||
|
||
const { className: masonryClassName, styles: masonryStyles } = css.resolve` | ||
div { | ||
display: flex; | ||
margin-left: -${gapSize}px; | ||
width: auto; | ||
min-height: 85vh; | ||
} | ||
`; | ||
const { className: masonryColumnClassName, styles: masonryColumnStyles } = css.resolve` | ||
div { | ||
padding-left: ${gapSize}px; | ||
background-clip: padding-box; | ||
} | ||
`; | ||
|
||
export default AdminDashboard; |
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,15 @@ | ||
import { AdminDashboard } from 'modules/AdminDashboard'; | ||
import { ActiveCourseProvider, SessionProvider } from 'modules/Course/contexts'; | ||
import { CourseRole } from 'services/models'; | ||
|
||
function Page() { | ||
return ( | ||
<SessionProvider allowedRoles={[CourseRole.Manager, CourseRole.Supervisor, CourseRole.Dementor]}> | ||
<ActiveCourseProvider> | ||
<AdminDashboard /> | ||
</ActiveCourseProvider> | ||
</SessionProvider> | ||
); | ||
} | ||
|
||
export default Page; |
Oops, something went wrong.