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

fix: concurrency issue when elements were loading #331

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 16 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,32 @@ 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);
const [movedElement] = sourceClone.splice(source.index, 1);
sourceClone.splice(destination.index, 0, movedElement);

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 @@ -141,7 +141,6 @@ const TxSegmentCompInput: React.FC<TxSegmentElementInputProps> = ({
endingExon,
startingExonOffset,
endingExonOffset,
index,
]);

const handleTxElementResponse = (
Expand All @@ -164,7 +163,7 @@ const TxSegmentCompInput: React.FC<TxSegmentElementInputProps> = ({
nomenclatureResponse.nomenclature
) {
finishedElement.nomenclature = nomenclatureResponse.nomenclature;
handleSave(index, finishedElement);
handleSave(finishedElement);
}
});
}
Expand Down