Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle state change in TreatmentView.tsx #1923

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ITreatmentViewProps {
}
export interface ITreatmentViewState {
selectedPolicy?: ICausalPolicy;
selectedIndex: number;
}

export class TreatmentView extends React.Component<
Expand All @@ -31,6 +32,7 @@ export class TreatmentView extends React.Component<
public constructor(props: ITreatmentViewProps) {
super(props);
this.state = {
selectedIndex: 0,
selectedPolicy: props.data?.[0]
};
}
Expand Down Expand Up @@ -71,9 +73,28 @@ export class TreatmentView extends React.Component<
);
}

public componentDidUpdate(prevProps: ITreatmentViewProps): void {
if (
prevProps.data &&
this.props.data &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this check for the length of data being at least 1, too?

prevProps.data[0].local_policies &&
this.props.data[0].local_policies
) {
if (
prevProps.data[0].local_policies.length !==
this.props.data[0].local_policies.length
Copy link
Contributor

@vinuthakaranth vinuthakaranth Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could there be a case where length is same but the policies have been updated?
can we use _.isequal(prevprops, currentprops) in that case?
Why is it always comparing with 0th index in prevProps.data[0] and this.props.data[0]? Should it take selectedIndex from state?

) {
this.setState({
selectedPolicy: this.props.data[this.state.selectedIndex]
});
}
}
}

private onSelect = (index: number): void => {
if (this.props.data) {
this.setState({
selectedIndex: index,
selectedPolicy: this.props.data[index]
});
}
Expand Down