Skip to content

Commit

Permalink
fix: pr-review
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikhaugstulen committed Sep 12, 2023
1 parent f943323 commit 04e93ed
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 38 deletions.
11 changes: 7 additions & 4 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2023-08-24T06:53:32.074Z\n"
"PO-Revision-Date: 2023-08-24T06:53:32.074Z\n"
"POT-Creation-Date: 2023-09-12T06:24:49.265Z\n"
"PO-Revision-Date: 2023-09-12T06:24:49.265Z\n"

msgid "Choose one or more dates..."
msgstr "Choose one or more dates..."
Expand Down Expand Up @@ -1371,8 +1371,11 @@ msgstr "An error occurred while adding the relationship"
msgid "Something went wrong while loading relationships. Please try again later."
msgstr "Something went wrong while loading relationships. Please try again later."

msgid "TEI's Relationships"
msgstr "TEI's Relationships"
msgid "{{trackedEntityTypeName}} relationships"
msgstr "{{trackedEntityTypeName}} relationships"

msgid "To open this relationship, please wait until saving is complete"
msgstr "To open this relationship, please wait until saving is complete"

msgid "Type"
msgstr "Type"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { WidgetError } from '../../../WidgetErrorAndWarning/WidgetError';
import { WidgetIndicator } from '../../../WidgetIndicator';
import { WidgetEnrollmentComment } from '../../../WidgetEnrollmentComment';
import { EnrollmentQuickActions } from './EnrollmentQuickActions';
import { TrackedEntityRelationshipsWrapper } from '../../common/TEIRelationshipsWidget/TrackedEntityRelationshipsWrapper';
import {
TrackedEntityRelationshipsWrapper,
} from '../../common/TEIRelationshipsWidget/TrackedEntityRelationshipsWrapper';
import { AddRelationshipRefWrapper } from '../../EnrollmentEditEvent/AddRelationshipRefWrapper';

const getStyles = () => ({
Expand Down Expand Up @@ -90,15 +92,17 @@ export const EnrollmentPageDefaultPlain = ({
<EnrollmentQuickActions
stages={stages}
events={events}
ruleEffects={ruleEffects}/>
ruleEffects={ruleEffects}
/>
<WidgetStagesAndEvents
programId={program.id}
stages={stages}
events={events}
onViewAll={onViewAll}
onCreateNew={onCreateNew}
onEventClick={onEventClick}
ruleEffects={ruleEffects}/>
ruleEffects={ruleEffects}
/>
</div>
<div className={classes.rightColumn}>
{addRelationShipContainerElement &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const NewTrackedEntityRelationshipPlain = ({

const clientData = {
createdAt: new Date().toISOString(),
pendingApiResponse: true,
...apiData,
};

Expand Down Expand Up @@ -108,6 +109,7 @@ const NewTrackedEntityRelationshipPlain = ({
const clientData = {
relationshipType: relationshipId,
createdAt: new Date().toISOString(),
pendingApiResponse: true,
...relationshipData,
[targetSide.toLowerCase()]: {
trackedEntity: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => {
{ critical: true },
);

// $FlowFixMe
const { mutate } = useMutation(
({ apiData }) => dataEngine.mutate(addRelationshipMutation, {
variables: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { WidgetTrackedEntityRelationshipProps } from './WidgetTrackedEntity
import { RelationshipsWidget } from '../common/RelationshipsWidget';
import { RelationshipSearchEntities, useRelationships } from '../common/useRelationships';
import { NewTrackedEntityRelationship } from './NewTrackedEntityRelationship';
import { useTrackedEntityTypeName } from './hooks/useTrackedEntityTypeName';

export const WidgetTrackedEntityRelationship = ({
relationshipTypes: cachedRelationshipTypes,
Expand All @@ -21,6 +22,7 @@ export const WidgetTrackedEntityRelationship = ({
renderTrackedEntityRegistration,
}: WidgetTrackedEntityRelationshipProps) => {
const { data: relationships, isError } = useRelationships(teiId, RelationshipSearchEntities.TRACKED_ENTITY);
const { data: trackedEntityTypeName } = useTrackedEntityTypeName(trackedEntityTypeId);

if (isError) {
return (
Expand All @@ -32,7 +34,10 @@ export const WidgetTrackedEntityRelationship = ({

return (
<RelationshipsWidget
title={i18n.t("TEI's Relationships")}
title={i18n.t('{{trackedEntityTypeName}} relationships', {
trackedEntityTypeName,
interpolation: { escapeValue: false },
})}
relationships={relationships}
cachedRelationshipTypes={cachedRelationshipTypes}
sourceId={teiId}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @flow
import { useMemo } from 'react';
import { useApiDataQuery } from '../../../../utils/reactQueryHelpers';

type ReturnData = {
displayName: string,
}

export const useTrackedEntityTypeName = (tetId: string) => {
const query = useMemo(() => ({
resource: 'trackedEntityTypes',
id: tetId,
params: {
fields: 'displayName',
},
}), [tetId]);

const { data, isLoading, error } = useApiDataQuery<ReturnData>(
['trackedEntityTypeName', tetId],
query,
{
enabled: !!tetId,
select: ({ displayName }: any) => displayName,
});

return {
data,
isLoading,
error,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import {
DataTableBody,
DataTableRow,
DataTableCell,
Tooltip,
} from '@dhis2/ui';
import i18n from '@dhis2/d2-i18n';
import { convertServerToClient, convertClientToList } from '../../../../converters';
import type { Props, StyledProps } from './linkedEntityTableBody.types';

const styles = {
row: {
'&:hover': {
cursor: 'pointer',
},
cursor: 'pointer',
},
rowDisabled: {
cursor: 'not-allowed',
opacity: 0.5,
},
};

Expand All @@ -27,28 +31,54 @@ const LinkedEntityTableBodyPlain = ({
<DataTableBody>
{
linkedEntities
.map(({ id: entityId, values, baseValues, navigation }) => (
<DataTableRow key={entityId}>
{
// $FlowFixMe flow doesn't like destructering
columns.map(({ id, type, convertValue }) => {
const value = type ?
convertClientToList(convertServerToClient(values[id], type), type) :
convertValue(baseValues?.[id] ?? context.display[id]);
.map(({ id: entityId, values, baseValues, navigation }) => {
const { pendingApiResponse } = baseValues || {};
return (
<DataTableRow
key={entityId}
className={pendingApiResponse ? classes.rowDisabled : classes.row}
>
{
// $FlowFixMe flow doesn't like destructering
columns.map(({ id, type, convertValue }) => {
const value = type ?
convertClientToList(convertServerToClient(values[id], type), type) :
convertValue(baseValues?.[id] ?? context.display[id]);

return (
<DataTableCell
className={classes.row}
key={`${entityId}-${id}`}
// $FlowFixMe
onClick={() => onLinkedRecordClick({ ...context.navigation, ...navigation })}
>
{value}
</DataTableCell>
);
})}
</DataTableRow>
))
return (
<Tooltip
key={`${entityId}-${id}`}
content={i18n.t('To open this relationship, please wait until saving is complete')}
closeDelay={50}
>
{({ onMouseOver, onMouseOut, ref }) => (
<DataTableCell
className={classes.row}
key={`${entityId}-${id}`}
// $FlowFixMe flow doesn't like destructering
onClick={() => !pendingApiResponse && onLinkedRecordClick({ ...context.navigation, ...navigation })}
ref={(tableCell) => {
if (tableCell) {
if (pendingApiResponse) {
tableCell.onmouseover = onMouseOver;
tableCell.onmouseout = onMouseOut;
ref.current = tableCell;
} else {
tableCell.onmouseover = null;
tableCell.onmouseout = null;
}
}
}}
>
{value}
</DataTableCell>
)}
</Tooltip>
);
})}
</DataTableRow>
);
})
}
</DataTableBody>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type LinkedEntityData = $ReadOnly<{|
values: $ReadOnly<{| [id: string]: ?string |}>,
baseValues?: {
relatonshipCreatedAt?: string,
pendingApiResponse?: boolean,
},
navigation?: {
programId?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const getContext = ({ relationshipEntity, program, programStage, trackedEntityTy
};
};

const getEventData = ({ dataValues, event, program: programId }, relatonshipCreatedAt): LinkedEntityData => {
const getEventData = ({ dataValues, event, program: programId }, relatonshipCreatedAt, pendingApiResponse): LinkedEntityData => {
const values = dataValues.reduce((acc, dataValue) => {
acc[dataValue.dataElement] = dataValue.value;
return acc;
Expand All @@ -91,6 +91,7 @@ const getEventData = ({ dataValues, event, program: programId }, relatonshipCrea
values,
baseValues: {
relatonshipCreatedAt,
pendingApiResponse,
},
navigation: {
eventId: event,
Expand All @@ -99,7 +100,7 @@ const getEventData = ({ dataValues, event, program: programId }, relatonshipCrea
};
};

const getTrackedEntityData = ({ attributes, trackedEntity }, relatonshipCreatedAt): LinkedEntityData => {
const getTrackedEntityData = ({ attributes, trackedEntity }, relatonshipCreatedAt, pendingApiResponse?: boolean): LinkedEntityData => {
const values = attributes.reduce((acc, attribute) => {
acc[attribute.attribute] = attribute.value;
return acc;
Expand All @@ -109,6 +110,7 @@ const getTrackedEntityData = ({ attributes, trackedEntity }, relatonshipCreatedA
id: trackedEntity,
values,
baseValues: {
pendingApiResponse,
relatonshipCreatedAt,
},
navigation: {
Expand All @@ -117,13 +119,13 @@ const getTrackedEntityData = ({ attributes, trackedEntity }, relatonshipCreatedA
};
};

const getLinkedEntityData = (apiLinkedEntity, relatonshipCreatedAt) => {
const getLinkedEntityData = (apiLinkedEntity, relatonshipCreatedAt, pendingApiResponse) => {
if (apiLinkedEntity.trackedEntity) {
return getTrackedEntityData(apiLinkedEntity.trackedEntity, relatonshipCreatedAt);
return getTrackedEntityData(apiLinkedEntity.trackedEntity, relatonshipCreatedAt, pendingApiResponse);
}

if (apiLinkedEntity.event) {
return getEventData(apiLinkedEntity.event, relatonshipCreatedAt);
return getEventData(apiLinkedEntity.event, relatonshipCreatedAt, pendingApiResponse);
}

if (apiLinkedEntity.enrollment) {
Expand Down Expand Up @@ -165,6 +167,7 @@ export const useGroupedLinkedEntities = (
relationshipType: relationshipTypeId,
from: fromEntity,
to: toEntity,
pendingApiResponse,
createdAt: relationshipCreatedAt,
} = relationship;

Expand All @@ -181,7 +184,7 @@ export const useGroupedLinkedEntities = (
return accGroupedLinkedEntities;
}

const linkedEntityData = getLinkedEntityData(apiLinkedEntity, relationshipCreatedAt);
const linkedEntityData = getLinkedEntityData(apiLinkedEntity, relationshipCreatedAt, pendingApiResponse);
if (!linkedEntityData) {
return accGroupedLinkedEntities;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type InputRelationshipData = {
relationship: string,
createdAt: string,
bidirectional: string,
pendingApiResponse?: boolean,
from: ApiLinkedEntity,
to: ApiLinkedEntity,
}

0 comments on commit 04e93ed

Please sign in to comment.