From d66e92adbc1281e34fd6d47f12198997caa58915 Mon Sep 17 00:00:00 2001 From: Sonny Date: Fri, 1 Nov 2024 19:58:48 +0100 Subject: [PATCH] feat: recreate form link (layout + create) --- app/controllers/links_controller.ts | 2 +- inertia/i18n/locales/en/common.json | 1 + inertia/i18n/locales/fr/common.json | 1 + .../components/form/mantine_form_link.tsx | 109 ++++++++++++++++++ inertia/pages/mantine/links/create.tsx | 51 ++++++++ 5 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 inertia/mantine/components/form/mantine_form_link.tsx create mode 100644 inertia/pages/mantine/links/create.tsx diff --git a/app/controllers/links_controller.ts b/app/controllers/links_controller.ts index ce36361..944244f 100644 --- a/app/controllers/links_controller.ts +++ b/app/controllers/links_controller.ts @@ -17,7 +17,7 @@ export default class LinksController { async showCreatePage({ auth, inertia }: HttpContext) { const collections = await this.collectionsController.getCollectionsByAuthorId(auth.user!.id); - return inertia.render('links/create', { collections }); + return inertia.render('mantine/links/create', { collections }); } async store({ auth, request, response }: HttpContext) { diff --git a/inertia/i18n/locales/en/common.json b/inertia/i18n/locales/en/common.json index db75931..af39f58 100644 --- a/inertia/i18n/locales/en/common.json +++ b/inertia/i18n/locales/en/common.json @@ -34,6 +34,7 @@ "description": "Description", "visibility": "Visibility", "visibility-warning": "The content will be visible to everyone", + "url": "URL", "create": "Create", "update": "Update", "delete": "Delete", diff --git a/inertia/i18n/locales/fr/common.json b/inertia/i18n/locales/fr/common.json index 8431010..f68e0ba 100644 --- a/inertia/i18n/locales/fr/common.json +++ b/inertia/i18n/locales/fr/common.json @@ -34,6 +34,7 @@ "description": "Description", "visibility": "Visibilité", "visibility-warning": "Le contenu sera visible par tout le monde", + "url": "URL", "create": "Créer", "update": "Mettre à jour", "delete": "Supprimer", diff --git a/inertia/mantine/components/form/mantine_form_link.tsx b/inertia/mantine/components/form/mantine_form_link.tsx new file mode 100644 index 0000000..c5ac7d7 --- /dev/null +++ b/inertia/mantine/components/form/mantine_form_link.tsx @@ -0,0 +1,109 @@ +import { Checkbox, Select, TextInput } from '@mantine/core'; +import { FormEvent } from 'react'; +import { useTranslation } from 'react-i18next'; +import BackToDashboard from '~/components/common/navigation/back_to_dashboard'; +import useSearchParam from '~/hooks/use_search_param'; +import { + FormLayoutProps, + MantineFormLayout, +} from '~/mantine/layouts/mantine_form_layout'; +import { Collection } from '~/types/app'; + +export type FormLinkData = { + name: string; + description: string | null; + url: string; + favorite: boolean; + collectionId: Collection['id']; +}; + +interface FormLinkProps extends FormLayoutProps { + data: FormLinkData; + errors?: Record>; + collections: Collection[]; + disableInputs?: boolean; + + setData: (name: string, value: any) => void; + handleSubmit: () => void; +} + +export default function MantineFormLink({ + data, + errors, + collections, + disableInputs = false, + setData, + handleSubmit, + ...props +}: FormLinkProps) { + const { t } = useTranslation('common'); + const collectionId = + Number(useSearchParam('collectionId')) ?? collections?.[0].id; + + const onSubmit = (event: FormEvent) => { + event.preventDefault(); + handleSubmit(); + }; + + return ( + + + setData('name', target.value)} + value={data.name} + readOnly={disableInputs} + error={errors?.name} + mt="md" + autoFocus + required + /> + setData('url', target.value)} + value={data.url ?? undefined} + readOnly={disableInputs} + error={errors?.link} + mt="md" + required + /> + setData('description', target.value)} + value={data.description ?? undefined} + readOnly={disableInputs} + error={errors?.description} + mt="md" + /> +