Skip to content

Commit

Permalink
refactor: create AddGuestbookForm and add new onValid props for Guest…
Browse files Browse the repository at this point in the history
…bookForm
  • Loading branch information
ixartz committed May 6, 2024
1 parent 250fb06 commit df9fd9e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/app/[locale]/(unauth)/guestbook/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslations } from 'next-intl';
import { getTranslations } from 'next-intl/server';
import { Suspense } from 'react';

import { GuestbookForm } from '@/components/GuestbookForm';
import { AddGuestbookForm } from '@/components/AddGuestbookForm';
import { GuestbookList } from '@/components/GuestbookList';

export async function generateMetadata(props: { params: { locale: string } }) {
Expand All @@ -23,7 +23,7 @@ const Guestbook = () => {

return (
<>
<GuestbookForm />
<AddGuestbookForm />

<Suspense fallback={<p>{t('loading_guestbook')}</p>}>
<GuestbookList />
Expand Down
19 changes: 19 additions & 0 deletions src/components/AddGuestbookForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { GuestbookForm } from './GuestbookForm';

const AddGuestbookForm = () => (
<GuestbookForm
onValid={async (data) => {
await fetch(`/api/guestbook`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});
}}
/>
);

export { AddGuestbookForm };
19 changes: 14 additions & 5 deletions src/components/EditableGuestbookEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ const EditableGuestbookEntry = (props: {
setIsEditing((value) => !value);
};

const handleStopEditing = () => {
setIsEditing(false);
};

return (
<>
<button
Expand Down Expand Up @@ -49,7 +45,20 @@ const EditableGuestbookEntry = (props: {
username: props.username,
body: props.body,
}}
handleStopEditing={handleStopEditing}
onValid={async (data) => {
await fetch(`/api/guestbook`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: props.id,
...data,
}),
});

setIsEditing(false);
}}
/>
) : (
<>
Expand Down
35 changes: 8 additions & 27 deletions src/components/GuestbookForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { zodResolver } from '@hookform/resolvers/zod';
import { useRouter } from 'next/navigation';
import { useTranslations } from 'next-intl';
import { useForm } from 'react-hook-form';
import { type SubmitHandler, useForm } from 'react-hook-form';
import type { z } from 'zod';

import { GuestbookValidation } from '@/validations/GuestbookValidation';
Expand All @@ -13,9 +13,12 @@ type IGuestbookFormProps =
edit: true;
id: number;
defaultValues: z.infer<typeof GuestbookValidation>;
handleStopEditing: () => void;
onValid: SubmitHandler<z.infer<typeof GuestbookValidation>>;
}
| { edit?: false };
| {
edit?: false;
onValid: SubmitHandler<z.infer<typeof GuestbookValidation>>;
};

const GuestbookForm = (props: IGuestbookFormProps) => {
const {
Expand All @@ -31,31 +34,9 @@ const GuestbookForm = (props: IGuestbookFormProps) => {
const t = useTranslations('GuestbookForm');

const handleCreate = handleSubmit(async (data) => {
if (props.edit) {
await fetch(`/api/guestbook`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
id: props.id,
...data,
}),
});

props.handleStopEditing();
} else {
await fetch(`/api/guestbook`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
});

reset();
}
await props.onValid(data);

reset();
router.refresh();
});

Expand Down

0 comments on commit df9fd9e

Please sign in to comment.