Skip to content

Commit

Permalink
feat: create new tei from relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikhaugstulen committed Aug 14, 2023
1 parent 14a2530 commit 72ac535
Show file tree
Hide file tree
Showing 24 changed files with 420 additions and 183 deletions.
10 changes: 8 additions & 2 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-07-28T10:44:57.813Z\n"
"PO-Revision-Date: 2023-07-28T10:44:57.813Z\n"
"POT-Creation-Date: 2023-08-14T09:17:11.587Z\n"
"PO-Revision-Date: 2023-08-14T09:17:11.587Z\n"

msgid "Choose one or more dates..."
msgstr "Choose one or more dates..."
Expand Down Expand Up @@ -1365,6 +1365,12 @@ msgstr "New Relationship"
msgid "Link to an existing {{tetName}}"
msgstr "Link to an existing {{tetName}}"

msgid "An error occurred while adding the relationship"
msgstr "An error occurred while adding the relationship"

msgid "Relationship added"
msgstr "Relationship added"

msgid "Something went wrong while loading relationships. Please try again later."
msgstr "Something went wrong while loading relationships. Please try again later."

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useSelector } from 'react-redux';
import { EnrollmentRegistrationEntryComponent } from './EnrollmentRegistrationEntry.component';
import type { OwnProps } from './EnrollmentRegistrationEntry.types';
import { useLifecycle } from './hooks';
import { useCurrentOrgUnitInfo } from '../../../hooks/useCurrentOrgUnitInfo';
import { useRulesEngineOrgUnit } from '../../../hooks';
import { dataEntryHasChanges } from '../../DataEntry/common/dataEntryHasChanges';
import { useMetadataForRegistrationForm } from '../common/TEIAndEnrollment/useMetadataForRegistrationForm';
Expand All @@ -15,12 +14,12 @@ export const EnrollmentRegistrationEntry: ComponentType<OwnProps> = ({
id,
saveButtonText,
trackedEntityInstanceAttributes,
cachedOrgUnitId,
orgUnitId,
teiId,
...passOnProps
}) => {
const orgUnitId = useCurrentOrgUnitInfo().id;
const { orgUnit, error } = useRulesEngineOrgUnit(cachedOrgUnitId ?? orgUnitId);
const { teiId, ready, skipDuplicateCheck } = useLifecycle(selectedScopeId, id, trackedEntityInstanceAttributes, orgUnit);
const { orgUnit, error } = useRulesEngineOrgUnit(orgUnitId);
const { ready, skipDuplicateCheck } = useLifecycle(selectedScopeId, id, trackedEntityInstanceAttributes, orgUnit, teiId);
const {
formId,
registrationMetaData: enrollmentMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { RenderFoundation } from '../../../metaData';

export type OwnProps = $ReadOnly<{|
id: string,
orgUnitId: string,
selectedScopeId: string,
fieldOptions?: Object,
onSave: SaveForDuplicateCheck,
Expand All @@ -21,7 +22,6 @@ export type OwnProps = $ReadOnly<{|
skipDuplicateCheck?: ?boolean,
trackedEntityInstanceAttributes?: Array<InputAttribute>,
saveButtonText: (trackedEntityName: string) => string,
cachedOrgUnitId?: string,
|}>;

type ContainerProps = {|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export const useLifecycle = (
dataEntryId: string,
trackedEntityInstanceAttributes?: Array<InputAttribute>,
orgUnit: ?OrgUnit,
teiId: ?string,
) => {
const { teiId, programId } = useLocationQuery();
const { programId } = useLocationQuery();
const dataEntryReadyRef = useRef(false);
const dispatch = useDispatch();
const program = programId && getProgramThrowIfNotFound(programId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const TeiRegistrationEntry: ComponentType<OwnProps> = ({ selectedScopeId,
return (
<TeiRegistrationEntryComponent
id={id}
orgUnitId={orgUnitId}
teiRegistrationMetadata={teiRegistrationMetadata}
selectedScopeId={teiRegistrationMetadata.form?.id}
ready={ready && !!teiRegistrationMetadata}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ const RegistrationDataEntryPlain = ({
<Grid item md sm={9} xs={9} >
<EnrollmentRegistrationEntryWrapper
id={dataEntryId}
orgUnitId={reduxOrgUnitId}
teiId={teiId}
selectedScopeId={selectedScopeId}
onSave={() => onSaveWithEnrollment(formFoundation)}
saveButtonText={(trackedEntityTypeNameLC: string) => i18n.t('Save {{trackedEntityTypeName}}', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DATA_ENTRY_ID } from '../../registerTei.const';
import enrollmentClasses from './enrollment.module.css';
import { EnrollmentRegistrationEntry } from '../../../../../DataEntries';
import type { Props } from './dataEntryEnrollment.types';
import { useLocationQuery } from '../../../../../../utils/routing';

const NewEnrollmentRelationshipPlain =
({
Expand All @@ -17,13 +18,15 @@ const NewEnrollmentRelationshipPlain =
renderDuplicatesCardActions,
ExistingUniqueValueDialogActions,
}: Props) => {
const { orgUnitId, teiId } = useLocationQuery();
const fieldOptions = { theme, fieldLabelMediaBasedClass: enrollmentClasses.fieldLabelMediaBased };


return (
<EnrollmentRegistrationEntry
id={DATA_ENTRY_ID}
selectedScopeId={programId}
orgUnitId={orgUnitId}
teiId={teiId}
fieldOptions={fieldOptions}
saveButtonText={(trackedEntityTypeName: string) => i18n.t('Save new {{trackedEntityTypeName}} and link', {
trackedEntityTypeName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const NewEnrollmentRelationshipPlain =
<EnrollmentRegistrationEntry
id={DATA_ENTRY_ID}
selectedScopeId={programId}
cachedOrgUnitId={orgUnitId}
orgUnitId={orgUnitId}
fieldOptions={fieldOptions}
saveButtonText={(trackedEntityTypeName: string) => i18n.t('Save new {{trackedEntityTypeName}} and link', {
trackedEntityTypeName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class RegisterTeiDataEntryComponent extends React.Component<Props> {
if (!showDataEntry) {
return null;
}

if (programId) {
return (
<DataEntryEnrollment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const RelationshipTrackedEntityInstancePlain =
theme,
onSave,
trackedEntityTypeId,
// teiRegistrationMetadata = {},
duplicatesReviewPageSize,
renderDuplicatesDialogActions,
renderDuplicatesCardActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {

export type Props = {|
theme: Theme,
trackedEntityTypeId: string,
onSave: SaveForEnrollmentAndTeiRegistration,
teiRegistrationMetadata?: TeiRegistration,
duplicatesReviewPageSize: number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { RegisterTeiDataEntry } from './DataEntry/RegisterTeiDataEntry.container
import { RegistrationSection } from './RegistrationSection';
import { DataEntryWidgetOutput } from '../../../../DataEntryWidgetOutput/DataEntryWidgetOutput.container';
import { ResultsPageSizeContext } from '../../../shared-contexts';
import type { Props } from './RegisterTei.types';
import type { ComponentProps } from './RegisterTei.types';
import { withErrorMessageHandler } from '../../../../../HOC';

const getStyles = () => ({
Expand Down Expand Up @@ -57,15 +57,14 @@ const DialogButtons = ({ onCancel, onSave, trackedEntityName }) => (

const RegisterTeiPlain = ({
dataEntryId,
itemId,
onLink,
onSave,
onGetUnsavedAttributeValues,
trackedEntityName,
trackedEntityTypeId,
selectedProgramId,
selectedScopeId,
classes,
}: Props) => {
}: ComponentProps) => {
const { resultsPageSize } = useContext(ResultsPageSizeContext);

const renderDuplicatesCardActions = useCallback(({ item }) => (
Expand Down Expand Up @@ -94,10 +93,6 @@ const RegisterTeiPlain = ({
</Button>
), [onLink]);

const handleSave = useCallback(() => {
onSave(itemId, dataEntryId);
}, [onSave, itemId, dataEntryId]);

return (
<div className={classes.container}>
<div className={classes.leftContainer}>
Expand All @@ -106,7 +101,7 @@ const RegisterTeiPlain = ({
/>
<RegisterTeiDataEntry
onLink={onLink}
onSave={handleSave}
onSave={onSave}
trackedEntityTypeId={trackedEntityTypeId}
onGetUnsavedAttributeValues={onGetUnsavedAttributeValues}
duplicatesReviewPageSize={resultsPageSize}
Expand All @@ -117,7 +112,7 @@ const RegisterTeiPlain = ({
</div>
<DataEntryWidgetOutput
dataEntryId={dataEntryId}
selectedScopeId={selectedProgramId}
selectedScopeId={selectedScopeId}
renderCardActions={({ item }) =>
<CardListButton teiId={item.id} values={item.values} handleOnClick={onLink} />
}
Expand All @@ -126,7 +121,7 @@ const RegisterTeiPlain = ({
);
};

export const RegisterTeiComponent: ComponentType<$Diff<Props, CssClasses>> =
export const RegisterTeiComponent: ComponentType<$Diff<ComponentProps, CssClasses>> =
compose(
withErrorMessageHandler(),
withStyles(getStyles),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { RegisterTeiComponent } from './RegisterTei.component';
import type { OwnProps } from './RegisterTei.types';
import type { ContainerProps } from './RegisterTei.types';
import { useScopeInfo } from '../../../../../hooks/useScopeInfo';
import { useDataEntryReduxConverter } from '../TrackedEntityRelationshipsWrapper/hooks/useDataEntryReduxConverter';

export const RegisterTei = ({
onLink,
onSave,
onGetUnsavedAttributeValues,
trackedEntityTypeId,
suggestedProgramId,
}: OwnProps) => {
}: ContainerProps) => {
const dataEntryId = 'relationship';
const itemId = useSelector(({ dataEntries }) => dataEntries[dataEntryId]?.itemId);
const error = useSelector(({ newRelationshipRegisterTei }) => (newRelationshipRegisterTei.error));
const newRelationshipProgramId = suggestedProgramId || trackedEntityTypeId;
const { trackedEntityName } = useScopeInfo(newRelationshipProgramId);
const selectedScopeId = suggestedProgramId || trackedEntityTypeId;
const { trackedEntityName } = useScopeInfo(selectedScopeId);
const { buildTeiPayload } = useDataEntryReduxConverter({
dataEntryId,
itemId,
trackedEntityTypeId,
});

const onCreateNewTei = () => {
const teiPayload = buildTeiPayload();
onSave(teiPayload);
};

return (
<RegisterTeiComponent
dataEntryId={dataEntryId}
itemId={itemId}
onLink={onLink}
onSave={onSave}
onSave={onCreateNewTei}
onGetUnsavedAttributeValues={onGetUnsavedAttributeValues}
trackedEntityName={trackedEntityName}
newRelationshipProgramId={newRelationshipProgramId}
selectedScopeId={selectedScopeId}
error={error}
trackedEntityTypeId={trackedEntityTypeId}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// @flow
type PropsFromRedux = {|
dataEntryId: string,
itemId: string,
trackedEntityName: ?string,
error: string,
|};

export type OwnProps = {|
export type SharedProps = {|
onLink: (teiId: string, values: Object) => void,
onGetUnsavedAttributeValues?: ?Function,
onSave: (itemId: string, dataEntryId: string) => void,
suggestedProgramId: string,
trackedEntityTypeId: string,
|};

export type Props = {|...PropsFromRedux, ...OwnProps, ...CssClasses |}
export type ContainerProps = {|
suggestedProgramId: string,
onSave: (teiPayload: Object) => void,
...SharedProps,
|};

export type ComponentProps = {|
selectedScopeId: string,
error: string,
dataEntryId: string,
trackedEntityName: ?string,
onSave: () => void,
...SharedProps,
...CssClasses,
|};
Loading

0 comments on commit 72ac535

Please sign in to comment.