Skip to content

Commit 3191716

Browse files
committed
chore: refactor to set loading states
1 parent 036441f commit 3191716

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

src/form/form-data-api.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { IParseOptions, IStringifyOptions } from "qs";
21
import { parse, stringify } from "qs";
32

43
const sort = (a: string, b: string) => a.localeCompare(b);

src/form/form.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,40 @@ const fromResponse = (ctx: ContextProps, response: Response) => {
4949
export const Form = forwardRef<HTMLFormElement, Props>(function InnerForm(props, externalRef) {
5050
const router = useRouter();
5151
const method = (props.method || "get").toLowerCase() as HttpMethods;
52+
5253
const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
54+
router.setLoading(true);
5355
event.preventDefault();
5456
const form = event.currentTarget;
55-
await props.onSubmit?.(event);
57+
if (props.onSubmit) {
58+
await props.onSubmit(event);
59+
}
5660
const page = router.page;
5761
if (method === "get" && page?.loader) {
58-
return fromResponse(
59-
router,
60-
await page.loader({
61-
paths: router.paths,
62-
data: page.data ?? {},
63-
path: router.href as PathFormat,
64-
request: new Request(router.href),
65-
queryString: fetchQs(router.location.search, page.originalPath),
66-
})
67-
);
62+
const body = {
63+
paths: router.paths,
64+
data: page.data ?? {},
65+
path: router.href as PathFormat,
66+
request: new Request(router.href),
67+
queryString: fetchQs(router.location.search, page.originalPath),
68+
};
69+
return fromResponse(router, await page.loader(body));
6870
}
6971
if (page?.actions && method !== "get") {
70-
const actions = await page.actions()
72+
const actions = await page.actions();
7173
if (has(actions, method)) {
7274
const fn = actions[method];
7375
const body = parseFromEncType(props.encType, form);
7476
const headers = new Headers();
7577
if (props.encType) headers.set("Content-Type", props.encType);
76-
return fromResponse(
77-
router,
78-
await fn!({
79-
paths: router.paths,
80-
data: page.data ?? {},
81-
path: router.href as PathFormat,
82-
request: new Request(router.href, { body, method, headers }),
83-
queryString: fetchQs(router.location.search, page.originalPath),
84-
})
85-
);
78+
const response = await fn!({
79+
paths: router.paths,
80+
data: page.data ?? {},
81+
path: router.href as PathFormat,
82+
request: new Request(router.href, { body, method, headers }),
83+
queryString: fetchQs(router.location.search, page.originalPath),
84+
});
85+
return fromResponse(router, response);
8686
}
8787
}
8888
};

0 commit comments

Comments
 (0)