Skip to content

Commit

Permalink
Merge pull request #275 from ixartz/refactor-guestbook
Browse files Browse the repository at this point in the history
refactor: create AddGuestbookForm and add new onValid props for GuestbookForm
  • Loading branch information
ixartz committed May 6, 2024
2 parents 250fb06 + cdab092 commit 8b86b10
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/app/[locale]/(unauth)/guestbook/page.tsx
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
@@ -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
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
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
39 changes: 1 addition & 38 deletions tailwind.config.ts
Expand Up @@ -3,44 +3,7 @@ import type { Config } from 'tailwindcss';
export default {
content: ['./src/**/*.{js,ts,jsx,tsx}'],
theme: {
fontSize: {
xs: '0.75rem',
sm: '0.875rem',
base: '1rem',
lg: '1.125rem',
xl: '1.25rem',
'2xl': '1.5rem',
'3xl': '1.875rem',
'4xl': '2.25rem',
'5xl': '3rem',
'6xl': '4rem',
},
extend: {
colors: {
gray: {
100: '#f7fafc',
200: '#edf2f7',
300: '#e2e8f0',
400: '#cbd5e0',
500: '#a0aec0',
600: '#718096',
700: '#4a5568',
800: '#2d3748',
900: '#1a202c',
},
blue: {
100: '#ebf8ff',
200: '#bee3f8',
300: '#90cdf4',
400: '#63b3ed',
500: '#4299e1',
600: '#3182ce',
700: '#2b6cb0',
800: '#2c5282',
900: '#2a4365',
},
},
},
extend: {},
},
plugins: [],
} satisfies Config;

0 comments on commit 8b86b10

Please sign in to comment.