|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useMemo, |
| 4 | + useState, |
| 5 | +} from 'react'; |
| 6 | +import { |
| 7 | + BarChart, |
| 8 | + BlockLoading, |
| 9 | + Container, |
| 10 | + KeyFigure, |
| 11 | + TimeSeriesChart, |
| 12 | +} from '@ifrc-go/ui'; |
| 13 | +import { useTranslation } from '@ifrc-go/ui/hooks'; |
| 14 | +import { getDatesSeparatedByYear } from '@ifrc-go/ui/utils'; |
| 15 | +import { |
| 16 | + isDefined, |
| 17 | + isNotDefined, |
| 18 | +} from '@togglecorp/fujs'; |
| 19 | + |
| 20 | +import useAlert from '#hooks/useAlert'; |
| 21 | +import { |
| 22 | + type GoApiResponse, |
| 23 | + type GoApiUrlQuery, |
| 24 | + useRequest, |
| 25 | +} from '#utils/restRequest'; |
| 26 | + |
| 27 | +import OperationalLearningMap from '../OperationalLearningMap'; |
| 28 | + |
| 29 | +import i18n from './i18n.json'; |
| 30 | +import styles from './styles.module.css'; |
| 31 | + |
| 32 | +type OpsLearningQuery = GoApiUrlQuery<'/api/v2/ops-learning/'>; |
| 33 | +type OpsLearningSummaryResponse = GoApiResponse<'/api/v2/ops-learning/stats/'>; |
| 34 | +type SectorStatItem = NonNullable<OpsLearningSummaryResponse['learning_by_sector']>[number]; |
| 35 | +type RegionStatItem = NonNullable<OpsLearningSummaryResponse['learning_by_region']>[number]; |
| 36 | +type SourcesOverTimeItem = NonNullable<OpsLearningSummaryResponse['sources_overtime']>[number]; |
| 37 | + |
| 38 | +const sectorKeySelector = (datum: SectorStatItem) => datum.sector_id; |
| 39 | +const sectorValueSelector = (datum: SectorStatItem) => datum.count; |
| 40 | +const sectorLabelSelector = (datum: SectorStatItem) => datum.title; |
| 41 | + |
| 42 | +const regionKeySelector = (datum: RegionStatItem) => datum.region_id; |
| 43 | +const regionValueSelector = (datum: RegionStatItem) => datum.count; |
| 44 | +const regionLabelSelector = (datum: RegionStatItem) => datum.region_name; |
| 45 | + |
| 46 | +type SourceType = 'dref' | 'emergencyAppeal' | 'others'; |
| 47 | +const dataKeyToClassNameMap: Record<SourceType, string> = { |
| 48 | + dref: styles.dref, |
| 49 | + emergencyAppeal: styles.emergencyAppeal, |
| 50 | + others: styles.others, |
| 51 | +}; |
| 52 | +const dataKeys: SourceType[] = [ |
| 53 | + 'dref', |
| 54 | + 'emergencyAppeal', |
| 55 | + 'others', |
| 56 | +]; |
| 57 | +const sourceClassNameSelector = (dataKey: SourceType) => dataKeyToClassNameMap[dataKey]; |
| 58 | +const xAxisFormatter = (date: Date) => date.toLocaleString( |
| 59 | + navigator.language, |
| 60 | + { year: 'numeric' }, |
| 61 | +); |
| 62 | + |
| 63 | +interface Props { |
| 64 | + query: OpsLearningQuery | undefined |
| 65 | +} |
| 66 | + |
| 67 | +const transformSourcesOverTimeData = (data: SourcesOverTimeItem[]) => { |
| 68 | + const groupedData: Record<string, Record<SourceType, number>> = {}; |
| 69 | + |
| 70 | + data.forEach((entry) => { |
| 71 | + const year = new Date(entry.date).getFullYear().toString(); |
| 72 | + if (!groupedData[year]) { |
| 73 | + groupedData[year] = { dref: 0, emergencyAppeal: 0, others: 0 }; |
| 74 | + } |
| 75 | + if (entry.type_display === 'DREF') { |
| 76 | + groupedData[year].dref += entry.count; |
| 77 | + } else if (entry.type_display === 'Emergency Appeal') { |
| 78 | + groupedData[year].emergencyAppeal += entry.count; |
| 79 | + } else { |
| 80 | + groupedData[year].others += entry.count; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + return groupedData; |
| 85 | +}; |
| 86 | + |
| 87 | +function Stats(props: Props) { |
| 88 | + const { |
| 89 | + query, |
| 90 | + } = props; |
| 91 | + |
| 92 | + const strings = useTranslation(i18n); |
| 93 | + const alert = useAlert(); |
| 94 | + const [activePointKey, setActivePointKey] = useState<string>(); |
| 95 | + |
| 96 | + const { |
| 97 | + response: learningStatsResponse, |
| 98 | + pending: learningStatsPending, |
| 99 | + } = useRequest({ |
| 100 | + url: '/api/v2/ops-learning/stats/', |
| 101 | + query, |
| 102 | + onFailure: () => { |
| 103 | + alert.show( |
| 104 | + strings.failedToFetchStats, |
| 105 | + { variant: 'danger' }, |
| 106 | + ); |
| 107 | + }, |
| 108 | + }); |
| 109 | + |
| 110 | + const sourcesOverTimeData = useMemo( |
| 111 | + () => { |
| 112 | + if (isNotDefined(learningStatsResponse)) { |
| 113 | + return undefined; |
| 114 | + } |
| 115 | + return transformSourcesOverTimeData(learningStatsResponse.sources_overtime); |
| 116 | + }, |
| 117 | + [learningStatsResponse], |
| 118 | + ); |
| 119 | + const dateList = useMemo(() => { |
| 120 | + if (isNotDefined(sourcesOverTimeData)) { |
| 121 | + return undefined; |
| 122 | + } |
| 123 | + const dates = Object.keys(sourcesOverTimeData).map((year) => new Date(Number(year), 0, 1)); |
| 124 | + const oldestDate = new Date(Math.min(...dates.map((date) => date.getTime()))); |
| 125 | + const latestDate = new Date(Math.max(...dates.map((date) => date.getTime()))); |
| 126 | + return getDatesSeparatedByYear(oldestDate, latestDate); |
| 127 | + }, [sourcesOverTimeData]); |
| 128 | + |
| 129 | + const sourcesOverTimeValueSelector = useCallback( |
| 130 | + (key: SourceType, date: Date) => { |
| 131 | + const value = sourcesOverTimeData?.[date.getFullYear()]?.[key]; |
| 132 | + if (isDefined(value) && value > 0) { |
| 133 | + return value; |
| 134 | + } |
| 135 | + return undefined; |
| 136 | + }, |
| 137 | + [sourcesOverTimeData], |
| 138 | + ); |
| 139 | + |
| 140 | + return ( |
| 141 | + <div className={styles.stats}> |
| 142 | + {learningStatsPending && <BlockLoading />} |
| 143 | + <div className={styles.keyFigureCard}> |
| 144 | + <KeyFigure |
| 145 | + className={styles.keyFigure} |
| 146 | + value={learningStatsResponse?.operations_included} |
| 147 | + label={strings.operationsIncluded} |
| 148 | + labelClassName={styles.keyFigureDescription} |
| 149 | + /> |
| 150 | + <div className={styles.separator} /> |
| 151 | + <KeyFigure |
| 152 | + className={styles.keyFigure} |
| 153 | + value={learningStatsResponse?.sources_used} |
| 154 | + label={strings.sourcesUsed} |
| 155 | + labelClassName={styles.keyFigureDescription} |
| 156 | + /> |
| 157 | + <div className={styles.separator} /> |
| 158 | + <KeyFigure |
| 159 | + className={styles.keyFigure} |
| 160 | + value={learningStatsResponse?.learning_extracts} |
| 161 | + label={strings.learningExtract} |
| 162 | + labelClassName={styles.keyFigureDescription} |
| 163 | + /> |
| 164 | + <div className={styles.separator} /> |
| 165 | + <KeyFigure |
| 166 | + className={styles.keyFigure} |
| 167 | + value={learningStatsResponse?.sectors_covered} |
| 168 | + label={strings.sectorsCovered} |
| 169 | + labelClassName={styles.keyFigureDescription} |
| 170 | + /> |
| 171 | + </div> |
| 172 | + <div className={styles.learningOverview}> |
| 173 | + <OperationalLearningMap |
| 174 | + learning={learningStatsResponse} |
| 175 | + /> |
| 176 | + <div className={styles.charts}> |
| 177 | + <Container |
| 178 | + heading={strings.learningBySector} |
| 179 | + className={styles.learningChart} |
| 180 | + withHeaderBorder |
| 181 | + withInternalPadding |
| 182 | + compactMessage |
| 183 | + empty={isDefined(learningStatsResponse?.learning_by_sector) && ( |
| 184 | + learningStatsResponse?.learning_by_sector.length < 1 |
| 185 | + )} |
| 186 | + > |
| 187 | + <BarChart |
| 188 | + data={learningStatsResponse?.learning_by_sector} |
| 189 | + keySelector={sectorKeySelector} |
| 190 | + valueSelector={sectorValueSelector} |
| 191 | + labelSelector={sectorLabelSelector} |
| 192 | + /> |
| 193 | + </Container> |
| 194 | + <Container |
| 195 | + heading={strings.learningByRegions} |
| 196 | + className={styles.learningChart} |
| 197 | + withHeaderBorder |
| 198 | + withInternalPadding |
| 199 | + compactMessage |
| 200 | + empty={isDefined(learningStatsResponse?.learning_by_region) && ( |
| 201 | + learningStatsResponse?.learning_by_region?.length < 1 |
| 202 | + )} |
| 203 | + > |
| 204 | + <BarChart |
| 205 | + data={learningStatsResponse?.learning_by_region} |
| 206 | + keySelector={regionKeySelector} |
| 207 | + valueSelector={regionValueSelector} |
| 208 | + labelSelector={regionLabelSelector} |
| 209 | + /> |
| 210 | + </Container> |
| 211 | + <Container |
| 212 | + heading={strings.sourcesOverTime} |
| 213 | + className={styles.learningChart} |
| 214 | + withHeaderBorder |
| 215 | + withInternalPadding |
| 216 | + compactMessage |
| 217 | + empty={isDefined(learningStatsResponse?.sources_overtime) && ( |
| 218 | + learningStatsResponse?.sources_overtime?.length < 1 |
| 219 | + )} |
| 220 | + > |
| 221 | + {isDefined(dateList) && ( |
| 222 | + <TimeSeriesChart |
| 223 | + className={styles.timeSeriesChart} |
| 224 | + timePoints={dateList} |
| 225 | + dataKeys={dataKeys} |
| 226 | + valueSelector={sourcesOverTimeValueSelector} |
| 227 | + classNameSelector={sourceClassNameSelector} |
| 228 | + activePointKey={activePointKey} |
| 229 | + onTimePointClick={setActivePointKey} |
| 230 | + xAxisFormatter={xAxisFormatter} |
| 231 | + /> |
| 232 | + )} |
| 233 | + |
| 234 | + </Container> |
| 235 | + </div> |
| 236 | + </div> |
| 237 | + </div> |
| 238 | + ); |
| 239 | +} |
| 240 | + |
| 241 | +export default Stats; |
0 commit comments