Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [DHIS2-18632] Sorting stage detail table on orgunit breaks the app #3917

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
Tooltip,
} from '@dhis2/ui';
import log from 'loglevel';
import { sortDataFromEvent } from './hooks/sortFuntions';
import { sortDataFromEvent } from './hooks/sortFunctions';
import { StageCreateNewButton } from '../StageCreateNewButton';
import { useComputeDataFromEvent, useComputeHeaderColumn, formatRowForView } from './hooks/useEventList';
import { DEFAULT_NUMBER_OF_ROW, SORT_DIRECTION } from './hooks/constants';
Expand Down Expand Up @@ -115,7 +115,6 @@ const StageDetailPlain = (props: Props) => {
const headerColumns = useComputeHeaderColumn(dataElements, hideDueDate, enableUserAssignment, stage?.stageForm);
const { loading, value: dataSource, error } = useComputeDataFromEvent(dataElements, events);


const [{ columnName, sortDirection }, setSortInstructions] = useState(defaultSortState);
const [displayedRowNumber, setDisplayedRowNumber] = useState(DEFAULT_NUMBER_OF_ROW);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type { StageDataElement } from '../../../../types/common.types';
import { Notes } from '../Notes.component';
import type { QuerySingleResource } from '../../../../../../utils/api/api.types';
import { isEventOverdue } from '../../../../../../utils/isEventOverdue';
import { TooltipOrgUnit } from '../../../../../Tooltips/TooltipOrgUnit/TooltipOrgUnit.component';

const getEventStatus = (event: ApiEnrollmentEvent) => {
const today = moment().startOf('day');
Expand Down Expand Up @@ -63,7 +62,6 @@ const convertStatusForView = (event: ApiEnrollmentEvent) => {
};
};

const convertOrgUnitForView = (event: ApiEnrollmentEvent) => <TooltipOrgUnit orgUnitId={event.orgUnit} />;

const convertNoteForView = (event: ApiEnrollmentEvent) => <Notes event={event} />;

Expand Down Expand Up @@ -100,7 +98,6 @@ export {
isEventOverdue,
getEventStatus,
convertStatusForView,
convertOrgUnitForView,
convertNoteForView,
getValueByKeyFromEvent,
groupRecordsByType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow
import log from 'loglevel';
import { errorCreator } from 'capture-core-utils';
import { getCachedOrgUnitName } from 'capture-core/metadataRetrieval/orgUnitName';
import moment from 'moment';
import { dataElementTypes } from '../../../../../../metaData';
import { SORT_DIRECTION } from './constants';
Expand Down Expand Up @@ -40,7 +41,7 @@ const sortText = (clientValueA: Object, clientValueB: Object, direction: string,
if (!clientValueB) return -1;

if (clientValueA !== clientValueB) {
return clientValueA.localeCompare(clientValueB);
return clientValueB.localeCompare(clientValueA);
}

return moment(eventDateB).unix() - moment(eventDateA).unix();
Expand All @@ -49,7 +50,7 @@ const sortText = (clientValueA: Object, clientValueB: Object, direction: string,
if (!clientValueB) return 1;

if (clientValueA !== clientValueB) {
return clientValueB.localeCompare(clientValueA);
return clientValueA.localeCompare(clientValueB);
}

return moment(eventDateB).unix() - moment(eventDateA).unix();
Expand Down Expand Up @@ -78,7 +79,12 @@ const sortTime = (clientValueA: Object, clientValueB: Object, direction: string,
return 0;
};

const sortOrgUnit = (clientValueA: Object, clientValueB: Object, direction: string, options: Object) => sortText(clientValueA.name, clientValueB.name, direction, options);
const sortOrgUnit = (clientValueA: string, clientValueB: string, direction: string, options: Object) => {
const orgUnitNameA = getCachedOrgUnitName(clientValueA);
const orgUnitNameB = getCachedOrgUnitName(clientValueB);

return sortText(orgUnitNameA, orgUnitNameB, direction, options);
};

// desc: Scheduled -> Active -> Completed -> Skipped
const sortStatus = (clientValueA: Object, clientValueB: Object, direction: string, options: Object) => {
Expand Down Expand Up @@ -113,10 +119,11 @@ const sortStatus = (clientValueA: Object, clientValueB: Object, direction: strin
return 0;
};

const sortDataFromEvent = ({ dataA, dataB, type, columnName, direction }: Object) => {
export const sortDataFromEvent = ({ dataA, dataB, type, columnName, direction }: Object) => {
if (!type) {
log.error(errorCreator('Type is not defined')({ dataA, dataB }));
}
console.log('direction', direction);
henrikmv marked this conversation as resolved.
Show resolved Hide resolved
const clientValueA = dataA[columnName];
const clientValueB = dataB[columnName];
const options = {
Expand All @@ -128,11 +135,6 @@ const sortDataFromEvent = ({ dataA, dataB, type, columnName, direction }: Object
return sortForTypes[type](clientValueA, clientValueB, direction, options);
};

export {
sortDataFromEvent,
};


const sortForTypes = {
[dataElementTypes.EMAIL]: sortText,
[dataElementTypes.TEXT]: sortText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { convertValue as convertClientToList } from '../../../../../../converter
import { convertValue as convertServerToClient } from '../../../../../../converters/serverToClient';
import {
convertStatusForView,
convertOrgUnitForView,
convertNoteForView,
getValueByKeyFromEvent,
groupRecordsByType,
Expand All @@ -27,7 +26,7 @@ const basedFieldTypes = [
{ type: dataElementTypes.STATUS, resolveValue: convertStatusForView },
{ type: dataElementTypes.DATE },
{ type: 'ASSIGNEE' },
{ type: dataElementTypes.TEXT, resolveValue: convertOrgUnitForView },
{ type: dataElementTypes.ORGANISATION_UNIT },
{ type: dataElementTypes.DATE },
{ type: dataElementTypes.UNKNOWN, resolveValue: convertNoteForView },
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export {
useOrgUnitNameWithAncestors,
useOrgUnitNames,
getOrgUnitNames,
getCachedOrgUnitName,
} from './orgUnitName';
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,5 @@ export const useOrgUnitNameWithAncestors = (orgUnitId: ?string): {

return { error };
};

export const getCachedOrgUnitName = (orgUnitId: string): ?string => displayNameCache[orgUnitId]?.displayName;
Loading