Skip to content

Commit

Permalink
Refactor state handling in QuerySequence component
Browse files Browse the repository at this point in the history
- Renamed `hasTextEntered` to `hasText` for clarity in `QuerySequence.jsx`.
- Added `lodash.has` for safe error handling.
- Adjusted conditionals to clear errors when no text is present.
- Streamlined error badge rendering logic.
- Updated button logic to utilize `hasText` state.

Avoid flickering and implement robust error handling.
  • Loading branch information
horta committed Nov 11, 2024
1 parent c47f345 commit bbfc94c
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions src/components/QuerySequence.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import loadWebComponent from "../utils/loadWebComponent";
import { useEffect, useRef, useState } from "react";
import exampleQuery from "../utils/exampleQuery";
import { useBoolean } from "react-use";
import { has } from "lodash";

const errorBadgeStyle = {
borderColor: "var(--vf-ui-color--red)",
Expand All @@ -22,22 +23,23 @@ const QuerySequence = ({ onStageSequence }) => {
loadWebComponent("textarea-sequence", TextareaSequence);
const textAreaSequenceRef = useRef();
const [errors, setErrors] = useState({});
const [hasTextEntered, setHasTextEntered] = useBoolean(false);
const [hasText, setHasText] = useBoolean(false);

useEffect(() => {
const handleErrorChange = (e) => {
setErrors(e.detail.errors);
if (!hasText) setErrors({});
else setErrors(e.detail.errors);
};
const currentTextArea = textAreaSequenceRef?.current;
if (!currentTextArea) return;
currentTextArea.addEventListener("error-change", handleErrorChange);
currentTextArea.quill.once("text-change", () => setHasTextEntered(true));
currentTextArea.quill.once("text-change", () => setHasText(true));
return () => {
if (currentTextArea) {
currentTextArea.removeEventListener("error-change", handleErrorChange);
}
};
}, [textAreaSequenceRef, setHasTextEntered]);
}, [hasText, textAreaSequenceRef, setHasText]);
return (
<div className="vf-stack vf-stack--400">
<div className="vf-stack vf-stack--200">
Expand All @@ -60,38 +62,35 @@ const QuerySequence = ({ onStageSequence }) => {
alphabet="ACTGU "
/>
</div>
{hasTextEntered && (
<div className="vf-cluster vf-cluster--200">
<div className="vf-cluster__inner">
{errors.hasInvalidCharacters && (
<span
className="vf-badge vf-badge--secondary"
style={errorBadgeStyle}
>
invalid alphabet
</span>
)}
{(errors.missingFirstHeader ||
errors.headerCheckRequiredForMultipleSequences) && (
<div className="vf-cluster vf-cluster--200" style={{ minHeight: 28 }}>
<div className="vf-cluster__inner">
{errors.hasInvalidCharacters && (
<span
className="vf-badge vf-badge--secondary"
style={errorBadgeStyle}
>
invalid alphabet
</span>
)}
{(errors.missingFirstHeader ||
errors.headerCheckRequiredForMultipleSequences) && (
<span
className="vf-badge vf-badge--secondary"
style={errorBadgeStyle}
>
missing headers
</span>
)}
{errors.tooShort && (
<span
className="vf-badge vf-badge--secondary"
style={errorBadgeStyle}
>
sequence length
</span>
)}
</div>
{errors.tooShort && (
<span
className="vf-badge vf-badge--secondary"
style={errorBadgeStyle}
>
sequence length
</span>
)}
</div>
)}

</div>
<div>
<button
className="vf-button vf-button--secondary vf-button--sm"
Expand All @@ -101,17 +100,18 @@ const QuerySequence = ({ onStageSequence }) => {
workaroundMultipleSequences(textAreaSequenceRef.current);
onStageSequence(textAreaSequenceRef.current.sequence);
}}
disabled={!hasTextEntered}
disabled={!hasText}
>
Check and autofix queries
</button>
<button
className="vf-button vf-button--tertiary vf-button--sm"
onClick={async () => {
await textAreaSequenceRef.current.quill.setContents([]);
setHasText(false);
setErrors({});
}}
disabled={!hasTextEntered}
disabled={!hasText}
>
Reset
</button>
Expand Down

0 comments on commit bbfc94c

Please sign in to comment.