Skip to content

Commit

Permalink
use transition for the contat form submit instead of a loading use state
Browse files Browse the repository at this point in the history
  • Loading branch information
ickynavigator committed Sep 10, 2024
1 parent b59e1c0 commit af851be
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/app/(root)/contact/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
IconAlertCircleFilled,
IconCircleCheckFilled,
} from '@tabler/icons-react';
import { useState } from 'react';
import { useTransition } from 'react';
import { z } from 'zod';
import useAlertManager from '~/hooks/useAlertManager';
import formSubmit from './actions';
Expand All @@ -38,27 +38,27 @@ const Page = () => {
const [formSuccess, toggleFormSuccess] = useAlertManager(false);
const [formError, toggleFormError] = useAlertManager(false);

const [loading, setLoading] = useState(false);
const [isPending, startTransition] = useTransition();

return (
<form
onSubmit={form.onSubmit(async values => {
toggleFormSuccess(false);
toggleFormError(false);
const handleSubmit = (values: typeof form.values) => {
startTransition(async () => {
toggleFormSuccess(false);
toggleFormError(false);

try {
await formSubmit(values);

try {
setLoading(true);
await formSubmit(values);
form.reset();
toggleFormSuccess(true);
} catch (e) {
console.error('from client', e);
toggleFormError(true);
}
});
};

form.reset();
toggleFormSuccess(true);
} catch (error) {
toggleFormError(true);
} finally {
setLoading(false);
}
})}
>
return (
<form onSubmit={form.onSubmit(handleSubmit)}>
<Title order={2} mb="sm" ta="right">
Contact Me
</Title>
Expand Down Expand Up @@ -97,7 +97,7 @@ const Page = () => {
{...form.getInputProps('message')}
/>
<Group justify="flex-end">
<Button type="submit" variant="outline" loading={loading}>
<Button type="submit" variant="outline" loading={isPending}>
Send
</Button>
</Group>
Expand Down

0 comments on commit af851be

Please sign in to comment.