mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-05-06 03:29:35 +00:00
66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
'use client';
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
import { useTranslations } from 'next-intl';
|
|
import { useRouter } from 'next/navigation';
|
|
import { useForm } from 'react-hook-form';
|
|
import { CounterValidation } from '@/validations/CounterValidation';
|
|
import { BASE_PATH } from '@/lib/app-config';
|
|
|
|
export const CounterForm = () => {
|
|
const t = useTranslations('CounterForm');
|
|
const form = useForm({
|
|
resolver: zodResolver(CounterValidation),
|
|
defaultValues: {
|
|
increment: 1,
|
|
},
|
|
});
|
|
const router = useRouter();
|
|
|
|
const handleIncrement = form.handleSubmit(async (data) => {
|
|
const response = await fetch(`${BASE_PATH}/api/counter`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(data),
|
|
});
|
|
await response.json();
|
|
|
|
router.refresh();
|
|
});
|
|
|
|
return (
|
|
<form onSubmit={handleIncrement}>
|
|
<p>{t('presentation')}</p>
|
|
<div>
|
|
<label className="text-sm font-bold text-gray-700" htmlFor="increment">
|
|
{t('label_increment')}
|
|
<input
|
|
id="increment"
|
|
type="number"
|
|
className="ml-2 w-32 appearance-none rounded-sm border border-gray-200 px-2 py-1 text-sm leading-tight text-gray-700 focus:ring-3 focus:ring-blue-300/50 focus:outline-hidden"
|
|
{...form.register('increment', { valueAsNumber: true })}
|
|
/>
|
|
</label>
|
|
|
|
{form.formState.errors.increment && (
|
|
<div className="my-2 text-xs text-red-500 italic">
|
|
{t('error_increment_range')}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="mt-2">
|
|
<button
|
|
className="rounded-sm bg-blue-500 px-5 py-1 font-bold text-white hover:bg-blue-600 focus:ring-3 focus:ring-blue-300/50 focus:outline-hidden disabled:pointer-events-none disabled:opacity-50"
|
|
type="submit"
|
|
disabled={form.formState.isSubmitting}
|
|
>
|
|
{t('button_increment')}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
);
|
|
};
|