Skip to content

Commit

Permalink
chore: pr-reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikhaugstulen committed Sep 28, 2023
1 parent d978ed8 commit f880324
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { RetrieverModeSelector } from './RetrieverModeSelector';
import type { ComponentProps, StyledComponentProps } from './NewTrackedEntityRelationship.types';
import { useAddRelationship } from './hooks/useAddRelationship';
import { TARGET_SIDES } from './common';
import { generateUID } from '../../../../utils/uid/generateUID';

const styles = {
container: {
Expand Down Expand Up @@ -52,7 +53,7 @@ const NewTrackedEntityRelationshipPlain = ({
const [currentStep, setCurrentStep] =
useState(NEW_TRACKED_ENTITY_RELATIONSHIP_WIZARD_STEPS.SELECT_LINKED_ENTITY_METADATA);
const [selectedLinkedEntityMetadata: LinkedEntityMetadata, setSelectedLinkedEntityMetadata] = useState(undefined);
const { mutate } = useAddRelationship({
const { addRelationship } = useAddRelationship({
teiId,
onMutate: () => onSave && onSave(),
});
Expand All @@ -61,7 +62,8 @@ const NewTrackedEntityRelationshipPlain = ({
const onLinkToTrackedEntityFromSearch = useCallback(
(linkedTrackedEntityId: string, attributes?: { [attributeId: string]: string }) => {
if (!selectedLinkedEntityMetadata) return;
const { relationshipId, targetSide } = selectedLinkedEntityMetadata;
const { relationshipId: relationshipTypeId, targetSide } = selectedLinkedEntityMetadata;
const relationshipId = generateUID();

const apiData = targetSide === TARGET_SIDES.TO ?
{ from: { trackedEntity: { trackedEntity: teiId } }, to: { trackedEntity: { trackedEntity: linkedTrackedEntityId } } } :
Expand All @@ -84,30 +86,34 @@ const NewTrackedEntityRelationshipPlain = ({
};
}

mutate({
addRelationship({
apiData: {
relationships: [{
relationshipType: relationshipId,
relationshipType: relationshipTypeId,
relationship: relationshipId,
...apiData,
}],
},
clientRelationship: {
relationshipType: relationshipId,
relationshipType: relationshipTypeId,
relationship: relationshipId,
...clientData,
},
});
}, [mutate, selectedLinkedEntityMetadata, teiId]);
}, [addRelationship, selectedLinkedEntityMetadata, teiId]);

const onLinkToTrackedEntityFromRegistration = useCallback((trackedEntity: Object) => {
if (!selectedLinkedEntityMetadata) return;
const { relationshipId, targetSide } = selectedLinkedEntityMetadata;
const { relationshipId: relationshipTypeId, targetSide } = selectedLinkedEntityMetadata;
const relationshipId = generateUID();

const relationshipData = targetSide === TARGET_SIDES.TO ?
{ from: { trackedEntity: { trackedEntity: teiId } }, to: { trackedEntity: { trackedEntity: trackedEntity.trackedEntity } } } :
{ from: { trackedEntity: { trackedEntity: trackedEntity.trackedEntity } }, to: { trackedEntity: { trackedEntity: teiId } } };

const clientData = {
relationshipType: relationshipId,
relationship: relationshipId,
relationshipType: relationshipTypeId,
createdAt: new Date().toISOString(),
pendingApiResponse: true,
...relationshipData,
Expand All @@ -121,14 +127,18 @@ const NewTrackedEntityRelationshipPlain = ({
},
};

mutate({
addRelationship({
apiData: {
trackedEntities: [trackedEntity],
relationships: [{ relationshipType: relationshipId, ...relationshipData }],
relationships: [{
relationship: relationshipId,
relationshipType: relationshipTypeId,
...relationshipData,
}],
},
clientRelationship: clientData,
});
}, [mutate, selectedLinkedEntityMetadata, teiId]);
}, [addRelationship, selectedLinkedEntityMetadata, teiId]);

const handleNavigation = useCallback(
(destination: $Values<typeof NEW_TRACKED_ENTITY_RELATIONSHIP_WIZARD_STEPS>) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// @flow
import i18n from '@dhis2/d2-i18n';
// $FlowFixMe - useAlert is exported from app-runtime
import { useDataEngine, useAlert } from '@dhis2/app-runtime';
import { useMutation, useQueryClient } from 'react-query';

type Props = {
teiId: string;
onMutate?: () => void;
onSuccess?: () => void;
onSuccess?: (apiResponse: Object, requestData: Object) => void;
}

const ReactQueryAppNamespace = 'capture';
Expand All @@ -34,9 +33,25 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => {
},
}),
{
onError: () => {
queryClient.invalidateQueries([ReactQueryAppNamespace, 'relationships']);
onError: (_, requestData) => {
showSnackbar();
const apiRelationshipId = requestData.clientRelationship.relationship;
const currentRelationships = queryClient.getQueryData([ReactQueryAppNamespace, 'relationships', teiId]);

if (!currentRelationships?.instances) return;

const newRelationships = currentRelationships.instances.reduce((acc, relationship) => {
if (relationship.relationship === apiRelationshipId) {
return acc;
}
acc.push(relationship);
return acc;
}, []);

queryClient.setQueryData(
[ReactQueryAppNamespace, 'relationships', teiId],
{ instances: newRelationships },
);
},
onMutate: (...props) => {
onMutate && onMutate(...props);
Expand All @@ -49,14 +64,31 @@ export const useAddRelationship = ({ teiId, onMutate, onSuccess }: Props) => {
return { instances: updatedInstances };
});
},
onSuccess: (...props) => {
queryClient.invalidateQueries([ReactQueryAppNamespace, 'relationships']);
onSuccess && onSuccess(...props);
onSuccess: async (apiResponse, requestData) => {
const apiRelationshipId = apiResponse.bundleReport.typeReportMap.RELATIONSHIP.objectReports[0].uid;
const currentRelationships = queryClient.getQueryData([ReactQueryAppNamespace, 'relationships', teiId]);
if (!currentRelationships?.instances) return;

const newRelationships = currentRelationships.instances.map((relationship) => {
if (relationship.relationship === apiRelationshipId) {
return {
...relationship,
pendingApiResponse: false,
};
}
return relationship;
});

queryClient.setQueryData(
[ReactQueryAppNamespace, 'relationships', teiId],
{ instances: newRelationships },
);
onSuccess && onSuccess(apiResponse, requestData);
},
},
);

return {
mutate,
addRelationship: mutate,
};
};
15 changes: 11 additions & 4 deletions src/core_modules/capture-core/flow/app-runtime_v2.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ declare module '@dhis2/app-runtime' {
message: string,
details: any,
|};

declare export type ApiAccessError = {|
type: 'access',
message: string,
Expand All @@ -127,15 +127,15 @@ declare module '@dhis2/app-runtime' {
status: string,
},
|};

declare export type ApiUnknownError = {|
type: 'unknown',
message: string,
details: any,
|};

declare export type ApiError = ApiNetworkError | ApiAccessError | ApiUnknownError;

declare type QueryRenderInput = {|
called: boolean,
loading: boolean,
Expand All @@ -148,4 +148,11 @@ declare module '@dhis2/app-runtime' {
declare export function useDataQuery(resourceQueries: ResourceQueries, queryOptions?: QueryOptions): QueryRenderInput;

declare export function useDataMutation(mutation: Mutation, mutationOptions?: QueryOptions): MutationRenderInput;

declare type AlertOptions = { [key: string]: mixed };

declare export function useAlert(message: string | ((props: any) => string), options?: AlertOptions | ((props: any) => AlertOptions)): {
show: (props?: any) => void;
hide: () => void;
};
}

0 comments on commit f880324

Please sign in to comment.