Skip to content

Commit ca30a46

Browse files
authored
feat: enable sort and pagination in LL plugin [DHIS2-20124] (#63)
1 parent 33a39f3 commit ca30a46

File tree

7 files changed

+59
-32
lines changed

7 files changed

+59
-32
lines changed

src/components/app/app.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CssVariables } from '@dhis2/ui'
22
import cx from 'classnames'
3-
import type { FC } from 'react'
3+
import { useCallback, type FC } from 'react'
44
import classes from './app.module.css'
55
import { AppWrapper } from '@components/app-wrapper'
66
import {
@@ -14,20 +14,34 @@ import {
1414
import { PluginWrapper } from '@components/plugin-wrapper/plugin-wrapper'
1515
import { StartScreen } from '@components/start-screen/start-screen'
1616
import { Toolbar } from '@components/toolbar/toolbar'
17-
import { useAppSelector, useCurrentUser } from '@hooks'
17+
import { useAppDispatch, useAppSelector, useCurrentUser } from '@hooks'
1818
import { isVisualizationEmpty } from '@modules/visualization'
19-
import { getCurrentVis } from '@store/current-vis-slice'
19+
import { getCurrentVis, setCurrentVis } from '@store/current-vis-slice'
2020
import {
2121
getUiDetailsPanelVisible,
2222
getUiMainSidebarVisible,
2323
} from '@store/ui-slice'
24+
import type { CurrentVisualization, Sorting } from '@types'
2425

2526
const EventVisualizer: FC = () => {
27+
const dispatch = useAppDispatch()
2628
const currentUser = useCurrentUser()
2729
const currentVis = useAppSelector(getCurrentVis)
2830
const isMainSidebarVisible = useAppSelector(getUiMainSidebarVisible)
2931
const isDetailsPanelVisible = useAppSelector(getUiDetailsPanelVisible)
3032

33+
const onDataSorted = useCallback(
34+
(sorting: Sorting) => {
35+
dispatch(
36+
setCurrentVis({
37+
...currentVis,
38+
sorting: sorting ? [sorting] : undefined,
39+
} as CurrentVisualization)
40+
)
41+
},
42+
[currentVis, dispatch]
43+
)
44+
3145
return (
3246
<GridContainer>
3347
<GridTopRow>
@@ -52,6 +66,7 @@ const EventVisualizer: FC = () => {
5266
<PluginWrapper
5367
visualization={currentVis}
5468
displayProperty={currentUser.settings.displayProperty}
69+
onDataSorted={onDataSorted}
5570
/>
5671
)}
5772
</GridCenterColumnBottom>

src/components/line-list/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ export type PaginatePayload = {
5656
page?: number
5757
pageSize?: number
5858
}
59-
export type DataSortFn = (payload?: DataSortPayload) => void
59+
export type DataSortFn = (payload: DataSortPayload) => void
6060
export type PaginateFn = (payload: PaginatePayload) => void
6161
export type ColumnHeaderClickFn = (cleanedHeaderName: string) => void

src/components/plugin-wrapper/hooks/query-tools-line-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const getAdaptedVisualization = (
5454
visualization: CurrentVisualization
5555
): {
5656
adaptedVisualization: Record<Axis, object[]> & {
57-
inputType: InputType
57+
outputType: InputType
5858
}
5959
headers: (string | string[])[]
6060
parameters: object
@@ -103,7 +103,7 @@ export const getAdaptedVisualization = (
103103
columns: adaptedColumns,
104104
rows: adaptedRows,
105105
filters: adaptedFilters,
106-
inputType,
106+
outputType: inputType,
107107
},
108108
headers,
109109
parameters,

src/components/plugin-wrapper/hooks/use-line-list-analytics-data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ const fetchAnalyticsDataForLL = async ({
113113

114114
if (sortField) {
115115
switch (sortDirection) {
116-
case 'asc':
116+
case 'ASC':
117117
req = req.withAsc(sortField)
118118
break
119-
case 'desc':
119+
case 'DESC':
120120
req = req.withDesc(sortField)
121121
break
122122
}

src/components/plugin-wrapper/line-list-plugin.tsx

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useLineListAnalyticsData } from './hooks/use-line-list-analytics-data'
44
import type { MetadataInput } from '@components/app-wrapper/metadata-helpers'
55
import { LineList } from '@components/line-list'
66
import type { LineListAnalyticsData } from '@components/line-list'
7+
import type { DataSortFn, DataSortPayload } from '@components/line-list/types'
78
import { transformVisualization } from '@modules/visualization'
89
import type { CurrentUser, CurrentVisualization, SortDirection } from '@types'
910

@@ -14,6 +15,7 @@ type LineListPluginProps = {
1415
isInDashboard: boolean
1516
isInModal: boolean
1617
isVisualizationLoading: boolean
18+
onDataSorted?: (sorting: DataSortPayload | undefined) => void
1719
onResponseReceived?: (metadata: MetadataInput) => void
1820
}
1921

@@ -27,6 +29,7 @@ export const LineListPlugin: FC<LineListPluginProps> = ({
2729
isInDashboard,
2830
isInModal,
2931
isVisualizationLoading,
32+
onDataSorted,
3033
onResponseReceived,
3134
}) => {
3235
console.log(
@@ -39,8 +42,7 @@ export const LineListPlugin: FC<LineListPluginProps> = ({
3942
isVisualizationLoading
4043
)
4144

42-
// TODO: add setter used in onDataSort
43-
const [visualization] = useState<CurrentVisualization>(
45+
const [visualization, setVisualization] = useState<CurrentVisualization>(
4446
transformVisualization(originalVisualization)
4547
)
4648

@@ -55,31 +57,36 @@ export const LineListPlugin: FC<LineListPluginProps> = ({
5557
}
5658
)
5759

58-
// TODO: get this from visualization.sorting
59-
const sortField = null
60-
const sortDirection = 'default'
60+
const { dimension: sortField, direction: sortDirection } = visualization
61+
.sorting?.length
62+
? visualization.sorting[0]
63+
: { dimension: undefined, direction: undefined }
6164

62-
// TODO: remove this comment once LineList component is used here
63-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
64-
const onPaginate = useCallback(
65-
({ page, pageSize }) =>
66-
setPagination({ page: pageSize ? FIRST_PAGE : page, pageSize }),
67-
[]
68-
)
65+
const onPaginate = useCallback(({ page, pageSize }) => {
66+
if (pageSize) {
67+
setPagination({ page: pageSize ? FIRST_PAGE : page, pageSize })
68+
} else if (page) {
69+
setPagination({ page })
70+
} else {
71+
throw new Error(
72+
'onPaginate was called with neither a page nor pageSize. At least one is expected'
73+
)
74+
}
75+
}, [])
6976

70-
// TODO: remove this comment once LineList component is used here
71-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
72-
const onDataSort = useCallback(
77+
const onDataSort: DataSortFn = useCallback(
7378
(sorting) => {
74-
console.log('onDataSort TBD', sorting)
75-
//setVisualization({
76-
// ...originalVisualization,
77-
// sorting,
78-
//})
79+
const newSorting =
80+
sorting.direction === undefined ? undefined : sorting
81+
82+
setVisualization({
83+
...visualization,
84+
sorting: newSorting ? [newSorting] : undefined,
85+
} as CurrentVisualization)
86+
87+
onDataSorted?.(newSorting)
7988
},
80-
[
81-
/*originalVisualization*/
82-
]
89+
[visualization, onDataSorted]
8390
)
8491

8592
const {
@@ -122,7 +129,7 @@ export const LineListPlugin: FC<LineListPluginProps> = ({
122129
)
123130
}}
124131
sortDirection={sortDirection as SortDirection}
125-
sortField={sortField ?? undefined}
132+
sortField={sortField}
126133
/>
127134
)
128135
}

src/components/plugin-wrapper/plugin-wrapper.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { FC } from 'react'
22
import { LineListPlugin } from './line-list-plugin'
33
import type { MetadataInput } from '@components/app-wrapper/metadata-helpers'
4+
import type { DataSortPayload } from '@components/line-list/types'
45
import type { CurrentUser, CurrentVisualization } from '@types'
56

67
type PluginWrapperProps = {
@@ -10,6 +11,7 @@ type PluginWrapperProps = {
1011
isInDashboard?: boolean
1112
isInModal?: boolean // passed when viewing an intepretation via the InterpretationModal from analytics
1213
isVisualizationLoading?: boolean
14+
onDataSorted?: (sorting: DataSortPayload | undefined) => void
1315
onResponseReceived?: (metadata: MetadataInput) => void
1416
}
1517

@@ -20,6 +22,7 @@ export const PluginWrapper: FC<PluginWrapperProps> = ({
2022
isInDashboard = false,
2123
isInModal = false,
2224
isVisualizationLoading = false,
25+
onDataSorted,
2326
onResponseReceived,
2427
}) => {
2528
if (visualization.type === 'LINE_LIST') {
@@ -31,6 +34,7 @@ export const PluginWrapper: FC<PluginWrapperProps> = ({
3134
isInDashboard={isInDashboard}
3235
isInModal={isInModal}
3336
isVisualizationLoading={isVisualizationLoading}
37+
onDataSorted={onDataSorted}
3438
onResponseReceived={onResponseReceived}
3539
/>
3640
)

src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type {
2121
ProgramType,
2222
RelativePeriodEnum,
2323
SortDirection,
24+
Sorting,
2425
ValueType,
2526
} from './dhis2-openapi-schemas'
2627
export type { PickWithFieldFilters } from './pick-with-field-filters'

0 commit comments

Comments
 (0)