Skip to content

Commit

Permalink
fix: useProgramInfo hook breaks the app
Browse files Browse the repository at this point in the history
  • Loading branch information
alaa-yahia committed Aug 29, 2024
1 parent 7b6f5ef commit 2ea3511
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ export const EnrollmentAddEventPageDefault = ({

const dataEntryHasChanges = useSelector(state => getDataEntryHasChanges(state, widgetReducerName));
const { program } = useProgramInfo(programId);
const selectedProgramStage = [...program.stages.values()].find(item => item.id === stageId);
const selectedProgramStage = [...program?.stages.values() ?? []].find(item => item.id === stageId);
const outputEffects = useWidgetDataFromStore(widgetReducerName);
const hideWidgets = useHideWidgetByRuleLocations(program.programRules.concat(selectedProgramStage?.programRules ?? []));
const hideWidgets = useHideWidgetByRuleLocations(program?.programRules.concat(selectedProgramStage?.programRules ?? []));
// $FlowFixMe
const trackedEntityName = program?.trackedEntityType?.name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import { Program } from '../../../../metaData';

export type Props = {|
program: Program,
program: ?Program,
stageId: string,
orgUnitId: string,
teiId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const EnrollmentEditEventPageComponent = ({
mode={mode}
programStage={programStage}
enrollmentId={enrollmentId}
programId={program.id}
programId={program?.id}
enrollmentsAsOptions={enrollmentsAsOptions}
trackedEntityName={trackedEntityName}
teiDisplayName={teiDisplayName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ const EnrollmentEditEventPageWithContextPlain = ({
}, [dispatch]);

const { program } = useProgramInfo(programId);
const programStage = [...program.stages?.values()].find(item => item.id === stageId);
const hideWidgets = useHideWidgetByRuleLocations(program.programRules.concat(programStage?.programRules));
const programStage = [...program?.stages?.values() ?? []].find(item => item.id === stageId);
const hideWidgets = useHideWidgetByRuleLocations(program?.programRules.concat(programStage?.programRules));

const onDeleteTrackedEntitySuccess = useCallback(() => {
history.push(`/?${buildUrlQueryString({ orgUnitId, programId })}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type PlainProps = {|
enrollmentId: string,
eventId: string,
stageId: string,
program: Program,
program: ?Program,
trackedEntityTypeId: string,
mode: string,
orgUnitId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { TopBarActions } from '../../TopBarActions';
type Props = {|
programStage: ?ProgramStage,
enrollmentId: string,
programId: string,
programId: ?string,
mode: string,
orgUnitId: string,
trackedEntityName: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const WithoutCategorySelectedMessagePlain = ({ programId, classes }) => {
categories: currentSelections.categories,
}), shallowEqual);

if (!program.categoryCombination) {
if (!program?.categoryCombination) {
log.error(errorCreator(errorMessages.MISSING_CATEGORY)({ programId }));
throw Error(i18n.t(errorMessages.GENERIC_ERROR));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ type Props = {|
|}

const WithoutOrgUnitSelectedMessagePlain = ({ programId, setShowAccessible, classes }: Props) => {
// TODO - this hook breaks the app when the program is not found
const { program, programType } = useProgramInfo(programId);
const IncompleteSelectionMessage = useMemo(() => (programType === programTypes.TRACKER_PROGRAM ? (
i18n.t('Or see all records accessible to you in {{program}} ', {
program: program.name,
program: program?.name,
interpolation: { escapeValue: false },
})
) : i18n.t('Or see all events accessible to you in {{program}}',
{ program: program.name, interpolation: { escapeValue: false } })),
[program.name, programType]);
{ program: program?.name, interpolation: { escapeValue: false } })),
[program?.name, programType]);

return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RegUnitSelectorPlain extends React.Component<Props> {
return;
}

onUpdateSelectedOrgUnit(orgUnit, program.organisationUnits ? !program.organisationUnits[orgUnit.id] : false);
onUpdateSelectedOrgUnit(orgUnit, program?.organisationUnits ? !program.organisationUnits[orgUnit.id] : false);
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function getCategoriesDataFromEventAsync(

const program = getProgramFromProgramIdThrowIfNotFound(event.programId);

const categoryCombination = program.categoryCombination;
const categoryCombination = program?.categoryCombination;
if (!categoryCombination) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RegUnitSelectorPlain extends React.Component<Props> {
return;
}

onUpdateSelectedOrgUnit(orgUnit, program.organisationUnits ? !program.organisationUnits[orgUnit.id] : false);
onUpdateSelectedOrgUnit(orgUnit, program?.organisationUnits ? !program.organisationUnits[orgUnit.id] : false);
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Node } from 'react';

export type Props = {
selectedOrgUnitId?: string,
selectedProgramId?: string,
selectedProgramId?: ?string,
selectedCategories: Object,
selectedOrgUnit: Object,
previousOrgUnitId?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type OwnProps = $ReadOnly<{|
isUserInteractionInProgress?: boolean,
pageToPush?: string,
selectedOrgUnitId?: string,
selectedProgramId?: string,
selectedProgramId?: ?string,
previousOrgUnitId?: string,
selectedCategories?: { [categoryId: string]: { writeAccess: boolean } },
onSetProgramId?: (id: string) => void,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// @flow

export type Props = {
selectedProgramId?: string,
selectedProgramId?: ?string,
selectedOrgUnitId?: string,
isUserInteractionInProgress?: boolean,
};

export type PlainProps = {
selectedProgramId?: string,
selectedProgramId?: ?string,
onNewClick: () => void,
onNewClickWithoutProgramId: () => void,
onFindClick: () => void,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { getProgramFromProgramIdThrowIfNotFound, TrackerProgram } from '../../me
import { programTypes } from './programTypes.const';

export const useProgramInfo = (programId: string) => useMemo(() => {
const program = getProgramFromProgramIdThrowIfNotFound(programId);
return {
program,
programType: program instanceof TrackerProgram ? programTypes.TRACKER_PROGRAM : programTypes.EVENT_PROGRAM,
};
try {
const program = getProgramFromProgramIdThrowIfNotFound(programId);
return {
program,
programType: program instanceof TrackerProgram ? programTypes.TRACKER_PROGRAM : programTypes.EVENT_PROGRAM,
};
} catch (error) {
return {
program: undefined,
programType: undefined,
};
}
}, [programId]);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function getProgramEventAccess(
if (program instanceof EventProgram) {
stage = program.stage;
} else if (programStageId) {
stage = program.getStage(programStageId);
stage = program?.getStage(programStageId);
}
if (!stage) {
log.error(errorCreator('stage not found')({ programId, programStageId }));
Expand Down

0 comments on commit 2ea3511

Please sign in to comment.