Skip to content

Commit 450c66c

Browse files
committed
use instructure/debounce for form submit
1 parent 8b59643 commit 450c66c

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"extends @instructure/browserslist-config-instui"
44
],
55
"dependencies": {
6+
"@instructure/debounce": "^10.23.0",
67
"@instructure/emotion": "^10.23.0",
78
"@instructure/ui-a11y-content": "^10.23.0",
89
"@instructure/ui-alerts": "^10.23.0",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/SignupModal.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { debounce } from "@instructure/debounce";
12
import { Button, IconButton } from "@instructure/ui-buttons";
23
import { alpha } from "@instructure/ui-color-utils";
34
import { Flex } from "@instructure/ui-flex";
@@ -70,12 +71,20 @@ const SignupModal = ({
7071

7172
const hasFormData = !!readLocalStorage("formData");
7273

74+
const closeModalDebounced = debounce(
75+
() => {
76+
setOpen(false);
77+
window.location.hash = "#";
78+
},
79+
2000,
80+
{ leading: false, trailing: true },
81+
);
82+
7383
const handleFormSubmit = async (e: FormEvent) => {
7484
e.preventDefault();
7585
setIsLoading(true);
7686
setIsDisabled(true);
7787
try {
78-
const start = Date.now();
7988
const form = e.target as HTMLFormElement;
8089
const formData = new FormData(form);
8190
formData.set("features", featureValueOptionIDs.join(","));
@@ -85,12 +94,7 @@ const SignupModal = ({
8594
submitCallback(formData, { setError, setSuccess }).then(resolve);
8695
});
8796
});
88-
const elapsed = Date.now() - start;
89-
if (elapsed < 2000) {
90-
await new Promise((resolve) => setTimeout(resolve, 2000 - elapsed));
91-
}
92-
setOpen(false);
93-
window.location.hash = "#";
97+
closeModalDebounced();
9498
} finally {
9599
setShouldCleanup(true);
96100
}

0 commit comments

Comments
 (0)