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

Store Rubric in IndexedDB #9840

Merged
merged 16 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 4 additions & 12 deletions teachertool/src/components/ActiveRubricDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@ import { getCatalogCriteriaWithId } from "../state/helpers";
import { Button } from "react-common/components/controls/Button";
import { removeCriteriaFromRubric } from "../transforms/removeCriteriaFromRubric";
import { showCatalogModal } from "../transforms/showCatalogModal";
import { Input } from "react-common/components/controls/Input";
import { setRubricName } from "../transforms/setRubricName";
import { DebouncedInput } from "./DebouncedInput";

interface IProps {}

export const ActiveRubricDisplay: React.FC<IProps> = ({}) => {
const { state: teacherTool } = useContext(AppStateContext);

const [inProgressName, setInProgressName] = useState(teacherTool.rubric.name);

function handleConfirmName() {
setRubricName(inProgressName);
}

return (
<div className="rubric-display">
<Input
<DebouncedInput
label={lf("Rubric Name")}
ariaLabel={lf("Rubric Name")}
onChange={setInProgressName}
onChange={setRubricName}
placeholder={lf("Rubric Name")}
initialValue={inProgressName}
onEnterKey={handleConfirmName}
onBlur={handleConfirmName}
initialValue={teacherTool.rubric.name}
preserveValueOnBlur={true}
/>
{teacherTool.rubric.criteria?.map(criteriaInstance => {
Expand Down
28 changes: 28 additions & 0 deletions teachertool/src/components/DebouncedInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useRef } from "react";
import { Input, InputProps } from "react-common/components/controls/Input";

export interface DebouncedInputProps extends InputProps {
intervalMs?: number | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider assigning the default value here so it's less buried and available to intellisense.

    intervalMs = 500

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think I can set it here, since this is an interface, but I've moved it up to the DebouncedInput parameter definition and added a comment here instead.

}

// This functions like the React Common Input, but debounces onChange calls
// so they only fire once every `interval` milliseconds (defined in props)
export const DebouncedInput: React.FC<DebouncedInputProps> = props => {
const timerId = useRef<NodeJS.Timeout | undefined>(undefined);

const onChangeDebounce = (newValue: string) => {
if (timerId.current) {
clearTimeout(timerId.current);
}

timerId.current = setTimeout(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the expected behavior if the component unmounts while a timeout is pending?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm good question. I'll add some code to fire the onChange immediately if we're umounting and the timeout is pending.

if (!props.onChange) {
return;
}

props.onChange(newValue);
}, props.intervalMs ?? 500);
};

return <Input {...props} onChange={onChangeDebounce} />;
};
Loading