generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create general statistic page (#378)
* feat: initalize general statistic and create date range component * feat: create member activity chart * feat: change sidebar to have my statistics only at root pages and change general statistics to my statistics * feat: rename file to myAnalyticspage * fix: responsive design and filling ranges gaps * feat: controlling number of bars depends on screen size
- Loading branch information
Showing
21 changed files
with
1,384 additions
and
1,091 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
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,67 @@ | ||
import React, { useContext, useState } from 'react'; | ||
import { DateRangePicker, defaultStaticRanges } from 'react-date-range'; | ||
import 'react-date-range/dist/styles.css'; | ||
import 'react-date-range/dist/theme/default.css'; | ||
|
||
import { Box, Popover, TextField } from '@mui/material'; | ||
|
||
import { format } from 'date-fns'; | ||
|
||
import i18n, { locales, useAnalyticsTranslation } from '@/config/i18n'; | ||
|
||
import { MyAnalyticsDateRangeDataContext } from '../context/MyAnalyticsDateRangeContext'; | ||
|
||
const DateRange = (): JSX.Element => { | ||
const { t } = useAnalyticsTranslation(); | ||
const { dateRange, setDateRange } = useContext( | ||
MyAnalyticsDateRangeDataContext, | ||
); | ||
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null); | ||
const open = Boolean(anchorEl); | ||
|
||
const handleClose = () => { | ||
setAnchorEl(null); | ||
}; | ||
|
||
const formattedStartDate = format(dateRange.startDate, 'MMM d, yyyy'); | ||
const formattedEndDate = format(dateRange.endDate, 'MMM d, yyyy'); | ||
const inputValue = `${formattedStartDate} - ${formattedEndDate}`; | ||
|
||
const defaultStaticRangesTranslatedLabels = defaultStaticRanges.map((r) => ({ | ||
...r, | ||
label: t(r.label as string), | ||
})); | ||
|
||
return ( | ||
<Box margin={{ sm: 'auto', md: 'unset' }}> | ||
<TextField | ||
required | ||
label={t('RANGE_DATE_PICKER_INPUT_LABEL')} | ||
value={inputValue} | ||
onClick={(event) => { | ||
setAnchorEl(event.currentTarget); | ||
}} | ||
sx={{ minWidth: '240px' }} | ||
/> | ||
<Popover | ||
onClose={handleClose} | ||
open={open} | ||
anchorEl={anchorEl} | ||
anchorOrigin={{ | ||
vertical: 'bottom', | ||
horizontal: 'left', | ||
}} | ||
> | ||
<DateRangePicker | ||
onChange={(item) => setDateRange({ ...dateRange, ...item.selection })} | ||
maxDate={new Date()} | ||
ranges={[dateRange]} | ||
locale={locales[i18n.language]} | ||
staticRanges={defaultStaticRangesTranslatedLabels} | ||
/> | ||
</Popover> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default DateRange; |
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,38 @@ | ||
import React from 'react'; | ||
|
||
import { Box, Stack, Typography, styled } from '@mui/material'; | ||
|
||
type Props = { | ||
title: string; | ||
stat: number; | ||
}; | ||
|
||
const StyledCardBox = styled(Stack)(({ theme }) => ({ | ||
padding: theme.spacing(2), | ||
backgroundColor: 'rgba(228, 224, 228, 0.61)', | ||
borderRadius: theme.spacing(2), | ||
color: '#808080', | ||
flex: 1, | ||
justifyContent: 'space-between', | ||
})); | ||
|
||
const MyAnalyticsCard = ({ title, stat }: Props): JSX.Element => { | ||
return ( | ||
<StyledCardBox> | ||
<Typography fontWeight={700}>{title}</Typography> | ||
<Box | ||
sx={{ | ||
width: '100%', | ||
display: 'flex', | ||
justifyContent: 'flex-end', | ||
fontWeight: 900, | ||
color: '#7F82CD', | ||
}} | ||
> | ||
{stat} | ||
</Box> | ||
</StyledCardBox> | ||
); | ||
}; | ||
|
||
export default MyAnalyticsCard; |
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,65 @@ | ||
import { createContext, useMemo, useState } from 'react'; | ||
|
||
import { addDays, intervalToDuration } from 'date-fns'; | ||
|
||
import { DateRange, GroupByInterval } from '@/config/type'; | ||
|
||
const defaultValue: { | ||
dateRange: DateRange; | ||
setDateRange: (view: DateRange) => void; | ||
groupInterval: GroupByInterval; | ||
} = { | ||
dateRange: { | ||
startDate: addDays(new Date(), -30), | ||
endDate: new Date(), | ||
key: 'selection', | ||
}, | ||
setDateRange: () => { | ||
// do nothing | ||
}, | ||
groupInterval: GroupByInterval.Week, | ||
}; | ||
|
||
export const MyAnalyticsDateRangeDataContext = createContext(defaultValue); | ||
|
||
const MyAnalyticsDateRangeProvider = ({ | ||
children, | ||
}: { | ||
children: JSX.Element; | ||
}): JSX.Element => { | ||
const [dateRange, setDateRange] = useState({ | ||
startDate: addDays(new Date(), -30), | ||
endDate: new Date(), | ||
key: 'selection', | ||
}); | ||
|
||
const { months, days, years } = intervalToDuration({ | ||
start: dateRange.startDate, | ||
end: dateRange.endDate, | ||
}); | ||
|
||
const groupInterval = | ||
years && years >= 1 | ||
? GroupByInterval.Year | ||
: months && months > 2 | ||
? GroupByInterval.Month | ||
: days && days < 8 | ||
? GroupByInterval.Day | ||
: GroupByInterval.Week; | ||
|
||
const value = useMemo( | ||
() => ({ | ||
dateRange, | ||
setDateRange, | ||
groupInterval, | ||
}), | ||
[dateRange, setDateRange, groupInterval], | ||
); | ||
return ( | ||
<MyAnalyticsDateRangeDataContext.Provider value={value}> | ||
{children} | ||
</MyAnalyticsDateRangeDataContext.Provider> | ||
); | ||
}; | ||
|
||
export default MyAnalyticsDateRangeProvider; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react'; | ||
|
||
import { Grid } from '@mui/material'; | ||
|
||
import { Action, ActionTriggers } from '@graasp/sdk'; | ||
|
||
import { useAnalyticsTranslation } from '@/config/i18n'; | ||
|
||
import MyAnalyticsCard from '../common/MyAnalyticCard'; | ||
|
||
type Props = { | ||
actionsGroupedByTypes: { [key: string]: Action[] }; | ||
}; | ||
const MemberGeneralStatisticsCards = ({ | ||
actionsGroupedByTypes, | ||
}: Props): JSX.Element => { | ||
const { t } = useAnalyticsTranslation(); | ||
|
||
return ( | ||
<Grid container spacing={2} p={2}> | ||
<Grid item md={3} lg={2} sx={{ display: 'flex' }}> | ||
<MyAnalyticsCard | ||
title={t('GENERAL_STATISTIC_ITEMS_CREATED')} | ||
stat={actionsGroupedByTypes[ActionTriggers.Create]?.length ?? 0} | ||
/> | ||
</Grid> | ||
<Grid item md={3} lg={2} sx={{ display: 'flex' }}> | ||
<MyAnalyticsCard | ||
title={t('GENERAL_STATISTIC_LIKED_ITEMS')} | ||
stat={actionsGroupedByTypes[ActionTriggers.ItemLike]?.length ?? 0} | ||
/> | ||
</Grid> | ||
<Grid item md={3} lg={2} sx={{ display: 'flex' }}> | ||
<MyAnalyticsCard | ||
title={t('GENERAL_STATISTIC_DOWNLOADED_ITEMS')} | ||
stat={actionsGroupedByTypes[ActionTriggers.ItemDownload]?.length ?? 0} | ||
/> | ||
</Grid> | ||
<Grid item md={3} lg={2} sx={{ display: 'flex' }}> | ||
<MyAnalyticsCard | ||
title={t('GENERAL_STATISTIC_CHAT_CREATED')} | ||
stat={actionsGroupedByTypes[ActionTriggers.ChatCreate]?.length ?? 0} | ||
/> | ||
</Grid> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default MemberGeneralStatisticsCards; |
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.