Skip to content

Commit

Permalink
fix: concurrency problem when elements were loading
Browse files Browse the repository at this point in the history
  • Loading branch information
katiestahl committed Sep 12, 2024
1 parent 626fc6c commit 8d6d11c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 21 deletions.
35 changes: 21 additions & 14 deletions client/src/components/Pages/Structure/Builder/Builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,37 @@ const Builder: React.FC = () => {
// TODO shouldn't need explicit autosave
if (STATIC_ELEMENT_TYPES.includes(newItem.type)) {
handleSave(
destination.index,
newItem as ClientMultiplePossibleGenesElement | ClientUnknownGeneElement
);
}
};

const reorder = (result: DropResult) => {
const { source, destination } = result;
const sourceClone = Array.from(fusion.structure);
const [movedElement] = sourceClone.splice(source.index, 1);
sourceClone.splice(destination.index, 0, movedElement);
setFusion({ ...fusion, ...{ structure: sourceClone } });

setFusion((prevFusion) => {
const sourceClone = Array.from(prevFusion.structure); // Use the latest state

// Remove the element from the source index and store it
const [movedElement] = sourceClone.splice(source.index, 1);

// Insert the moved element at the destination index
sourceClone.splice(destination.index, 0, movedElement);

// Return the updated fusion structure
return { ...prevFusion, structure: sourceClone };
});
};

// Update global fusion object
const handleSave = (index: number, newElement: ClientElementUnion) => {
const items = Array.from(fusion.structure);
const spliceLength = EDITABLE_ELEMENT_TYPES.includes(
newElement.type as ElementType
)
? 1
: 0;
items.splice(index, spliceLength, newElement);
setFusion({ ...fusion, ...{ structure: items } });
const handleSave = (newElement: ClientElementUnion) => {
setFusion((prevFusion) => {
const updatedStructure = prevFusion.structure.map((item) =>
item.elementId === newElement.elementId ? newElement : item
);

return { ...prevFusion, structure: updatedStructure };
});
};

const handleDelete = (uuid: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ interface GeneElementInputProps extends StructuralElementInputProps {

const GeneElementInput: React.FC<GeneElementInputProps> = ({
element,
index,
handleSave,
handleDelete,
icon,
Expand Down Expand Up @@ -56,7 +55,7 @@ const GeneElementInput: React.FC<GeneElementInputProps> = ({
elementId: element.elementId,
nomenclature: nomenclatureResponse.nomenclature,
};
handleSave(index, clientGeneElement);
handleSave(clientGeneElement);
setPendingResponse(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const LinkerElementInput: React.FC<LinkerElementInputProps> = ({
},
nomenclature: sequence,
};
handleSave(index, linkerElement);
handleSave(linkerElement);
};

const inputElements = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export interface BaseStructuralElementProps {
export interface StructuralElementInputProps
extends BaseStructuralElementProps {
index: number;
handleSave: (index: number, element: ClientElementUnion) => void;
handleSave: (element: ClientElementUnion) => void;
icon: JSX.Element;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface TemplatedSequenceElementInputProps

const TemplatedSequenceElementInput: React.FC<
TemplatedSequenceElementInputProps
> = ({ element, index, handleSave, handleDelete, icon }) => {
> = ({ element, handleSave, handleDelete, icon }) => {
const [chromosome, setChromosome] = useState<string>(
element.inputChromosome || ""
);
Expand Down Expand Up @@ -92,7 +92,7 @@ const TemplatedSequenceElementInput: React.FC<
inputStart: startPosition,
inputEnd: endPosition,
};
handleSave(index, templatedSequenceElement);
handleSave(templatedSequenceElement);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const TxSegmentCompInput: React.FC<TxSegmentElementInputProps> = ({
nomenclatureResponse.nomenclature
) {
finishedElement.nomenclature = nomenclatureResponse.nomenclature;
handleSave(index, finishedElement);
handleSave(finishedElement);
}
});
}
Expand Down

0 comments on commit 8d6d11c

Please sign in to comment.