Skip to content

Commit 148aa14

Browse files
committed
Fix error where nested objects are immutable
1 parent 71af4a7 commit 148aa14

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/components/AssignmentTexter/Controls.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
import { dataTest } from "../../lib/attributes";
3636
import ContactToolbar from "./ContactToolbar";
3737
import { getCookie, setCookie } from "../../lib/cookie";
38+
import { deepCopy } from "../utils";
3839

3940
export class AssignmentTexterContactControls extends React.Component {
4041
constructor(props) {
@@ -124,7 +125,10 @@ export class AssignmentTexterContactControls extends React.Component {
124125
let currentInteractionStep = null;
125126

126127
if (availableSteps.length > 0) {
127-
currentInteractionStep = availableSteps[availableSteps.length - 1];
128+
const currentInteractionStep = deepCopy(
129+
availableSteps[availableSteps.length - 1]
130+
);
131+
128132
currentInteractionStep.question.filteredAnswerOptions =
129133
currentInteractionStep.question.answerOptions;
130134
}

src/components/utils.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,16 @@ export function getButtonProps(props) {
2727
];
2828
return pick(props, validProps);
2929
}
30+
31+
// Create a deep copy of an object so nested properties are also mutable
32+
export function deepCopy(obj) {
33+
if (Array.isArray(obj)) {
34+
return obj.map(item => deepCopy(item));
35+
} else if (typeof obj === "object" && obj !== null) {
36+
return Object.fromEntries(
37+
Object.entries(obj).map(([key, value]) => [key, deepCopy(value)])
38+
);
39+
} else {
40+
return obj;
41+
}
42+
}

0 commit comments

Comments
 (0)