Skip to content

Commit

Permalink
feat: add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikhaugstulen committed Nov 4, 2023
1 parent 225f84e commit d854f0c
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 41 deletions.
8 changes: 4 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-11-04T15:34:51.698Z\n"
"PO-Revision-Date: 2023-11-04T15:34:51.698Z\n"
"POT-Creation-Date: 2023-11-04T16:21:37.396Z\n"
"PO-Revision-Date: 2023-11-04T16:21:37.396Z\n"

msgid "Choose one or more dates..."
msgstr "Choose one or more dates..."
Expand Down Expand Up @@ -1373,8 +1373,8 @@ msgstr "Scheduled date"
msgid "Report date"
msgstr "Report date"

msgid "Please provide a valid event"
msgstr "Please provide a valid event"
msgid "Please select a valid event"
msgstr "Please select a valid event"

msgid "New {{ eventName }} event"
msgstr "New {{ eventName }} event"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ValidatedPlain = ({
stage,
programName,
programId,
teiId,
enrollmentId,
eventSaveInProgress,
formFoundation,
classes,
Expand Down Expand Up @@ -49,7 +49,7 @@ const ValidatedPlain = ({
/>
<WidgetReferral
ref={referralRef}
teiId={teiId}
enrollmentId={enrollmentId}
programId={programId}
programStageId={stage?.id}
currentStageLabel={stage.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const Validated = ({
ready={ready}
id={dataEntryId}
itemId={itemId}
teiId={teiId}
enrollmentId={enrollmentId}
formFoundation={formFoundation}
referralRef={referralRef}
onSave={handleSave}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type ContainerProps = {|
export type Props = {|
programName: string,
programId: string,
teiId: string,
enrollmentId: string,
eventSaveInProgress: boolean,
stage: ProgramStage,
formFoundation: RenderFoundation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const LinkToExistingPlain = ({
setReferralDataValues,
linkableEvents,
referralProgramStageLabel,
errorMessages,
saveAttempted,
classes,
}: LinkToExistingProps) => {
const onChange = (value) => {
Expand All @@ -53,6 +55,8 @@ export const LinkToExistingPlain = ({
referralProgramStageLabel,
})}
className={classes.singleSelectField}
error={saveAttempted && !!errorMessages.linkedEventId}
validationText={saveAttempted && errorMessages.linkedEventId}
>
{linkableEvents.map(event => (
<SingleSelectOption
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// @flow

import type { LinkableEvent } from '../ReferralActions/ReferralActions.types';
import type { ErrorMessagesForReferral, LinkableEvent } from '../ReferralActions/ReferralActions.types';
import type { ReferralDataValueStates } from '../WidgetReferral.types';

export type LinkToExistingProps = {|
referralDataValues: ReferralDataValueStates,
setReferralDataValues: (ReferralDataValueStates) => void,
linkableEvents: Array<LinkableEvent>,
errorMessages: ErrorMessagesForReferral,
saveAttempted: boolean,
referralProgramStageLabel: string,
...CssClasses,
|}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ export const ReferralActionsPlain = ({
classes,
type,
scheduledLabel,
selectedType,
linkableEvents,
referralDataValues,
setReferralDataValues,
constraint,
currentStageLabel,
...passOnProps
errorMessages,
saveAttempted,
}: Props) => {
const { programStage } = useProgramStageInfo(constraint?.programStage?.id);

Expand Down Expand Up @@ -97,7 +97,8 @@ export const ReferralActionsPlain = ({
referralDataValues={referralDataValues}
setReferralDataValues={setReferralDataValues}
scheduledLabel={scheduledLabel}
{...passOnProps}
saveAttempted={saveAttempted}
errorMessages={errorMessages}
/>
)}

Expand All @@ -122,7 +123,8 @@ export const ReferralActionsPlain = ({
setReferralDataValues={setReferralDataValues}
linkableEvents={linkableEvents}
referralProgramStageLabel={programStage.stageForm.name}
{...passOnProps}
errorMessages={errorMessages}
saveAttempted={saveAttempted}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
// @flow
import type { ReferralDataValueStates } from '../WidgetReferral.types';

type ReferralRelationshipType = {|
id: string,
fromConstraint: {
entity: string,
programStageId?: {
id: string,
}
},
toConstraint: {
programStage?: {
id: string,
},
},
|}

type Constraint = {
programStage: {
id: string,
Expand All @@ -26,6 +11,7 @@ type Constraint = {
export type ErrorMessagesForReferral = {|
scheduledAt?: ?string,
orgUnit?: ?string,
linkedEventId?: ?string,
|}

export type LinkableEvent = {
Expand All @@ -35,7 +21,6 @@ export type LinkableEvent = {

export type Props = {|
type: string,
selectedType: ReferralRelationshipType,
referralDataValues: ReferralDataValueStates,
linkableEvents: Array<LinkableEvent>,
scheduledLabel: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useAvailableReferralEvents } from './hooks/useAvailableReferralEvents';

const WidgetReferralPlain = ({
programId,
teiId,
enrollmentId,
programStageId,
currentStageLabel,
...passOnProps
Expand All @@ -23,7 +23,7 @@ const WidgetReferralPlain = ({
relationshipTypeId: selectedRelationshipType?.id,
scheduledLabel,
occurredLabel,
teiId,
enrollmentId,
});
const [saveAttempted, setSaveAttempted] = useState(false);
const [errorMessages, setErrorMessages] = useState({});
Expand Down Expand Up @@ -85,7 +85,6 @@ const WidgetReferralPlain = ({
return (
<ReferralActions
scheduledLabel={scheduledLabel}
selectedType={selectedRelationshipType}
type={currentReferralStatus}
linkableEvents={linkableEvents}
referralDataValues={referralDataValues}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { actions as ReferralModes } from './constants';

export type Props = {|
programId: string,
teiId: string,
enrollmentId: string,
programStageId: string,
currentStageLabel: string,
|}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useApiDataQuery } from '../../../utils/reactQueryHelpers';

type Props = {
stageId: ?string,
teiId: string,
enrollmentId: string,
scheduledLabel: string,
occurredLabel: string,
relationshipTypeId: ?string,
Expand All @@ -22,7 +22,7 @@ type ReturnType = {

export const useAvailableReferralEvents = ({
stageId,
teiId,
enrollmentId,
relationshipTypeId,
scheduledLabel,
occurredLabel,
Expand All @@ -32,19 +32,20 @@ export const useAvailableReferralEvents = ({
resource: 'tracker/events',
params: {
programStage: stageId,
trackedEntity: teiId,
fields: 'event,occurredAt,scheduledAt,relationships',
enrollments: enrollmentId,
fields: 'event,occurredAt,scheduledAt,status,relationships',
},
}), [stageId, teiId]);
}), [stageId, enrollmentId]);
const { data, isLoading, isError } = useApiDataQuery<Array<LinkableEvent>>(
['availableReferralEvents', stageId, teiId, relationshipTypeId],
['availableReferralEvents', stageId, enrollmentId, relationshipTypeId],
query,
{
enabled: !!stageId && enabled,
cacheTime: 0,
staleTime: 0,
select: (response: any) => {
const events = response?.instances;
const events = response?.instances.filter(instance => ['SCHEDULE', 'ACTIVE'].includes(instance.status));

if (events.length === 0) return [];

return events.reduce((acc, event) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const linkToExistingResponse = (props) => {

if (!linkedEventIdIsValid) {
setErrorMessages({
linkedEventId: i18n.t('Please provide a valid event'),
linkedEventId: i18n.t('Please select a valid event'),
});
} else {
setErrorMessages({
Expand Down

0 comments on commit d854f0c

Please sign in to comment.