|
| 1 | +import React from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | +import { useForm, SubmitHandler } from 'react-hook-form'; |
| 4 | +import { useMutation, useQueryClient } from '@tanstack/react-query'; |
| 5 | +import { useTranslation } from 'react-i18next'; |
| 6 | +import { useNavigate } from 'react-router-dom'; |
| 7 | + |
| 8 | +import Header from '../components/common/molecules/Header'; |
| 9 | +import Button from '../components/common/atoms/Button'; |
| 10 | +import Input from '../components/common/atoms/Input'; |
| 11 | +import Textarea from '../components/common/atoms/Textarea'; |
| 12 | +import FormGroup from '../components/common/molecules/FormGroup'; |
| 13 | + |
| 14 | +import type { ComplexInput } from '../types/complex'; |
| 15 | +import { createComplex } from '../services/api'; |
| 16 | + |
| 17 | +const PageWrapper = styled.div` |
| 18 | + display: flex; |
| 19 | + flex-direction: column; |
| 20 | + min-height: 100vh; |
| 21 | +`; |
| 22 | + |
| 23 | +const MainContent = styled.main` |
| 24 | + flex-grow: 1; |
| 25 | + max-width: 800px; /* フォーム用に少し狭く */ |
| 26 | + margin: 0 auto; |
| 27 | + padding: 2rem 1rem; /* 32px 16px */ |
| 28 | + width: 100%; |
| 29 | +`; |
| 30 | + |
| 31 | +const PageTitle = styled.h2` |
| 32 | + font-size: 2rem; /* 32px */ |
| 33 | + font-weight: 700; |
| 34 | + color: #1d1d1f; |
| 35 | + margin-bottom: 2rem; /* 32px */ |
| 36 | + text-align: center; |
| 37 | +`; |
| 38 | + |
| 39 | +const Form = styled.form` |
| 40 | + background-color: #ffffff; |
| 41 | + border-radius: 16px; |
| 42 | + box-shadow: 0 8px 16px rgba(0, 0, 0, 0.08); |
| 43 | + padding: 2rem; /* 32px */ |
| 44 | +`; |
| 45 | + |
| 46 | +const ActionsWrapper = styled.div` |
| 47 | + display: flex; |
| 48 | + gap: 1rem; /* 16px */ |
| 49 | + justify-content: flex-end; |
| 50 | + margin-top: 2rem; /* 32px */ |
| 51 | +`; |
| 52 | + |
| 53 | +const ComplexFormPage: React.FC = () => { |
| 54 | + const { t } = useTranslation(); |
| 55 | + const navigate = useNavigate(); |
| 56 | + const queryClient = useQueryClient(); |
| 57 | + |
| 58 | + const { |
| 59 | + register, |
| 60 | + handleSubmit, |
| 61 | + formState: { errors }, |
| 62 | + } = useForm<ComplexInput>(); |
| 63 | + |
| 64 | + const createComplexMutation = useMutation<Complex, Error, ComplexInput>({ |
| 65 | + mutationFn: createComplex, |
| 66 | + onSuccess: () => { |
| 67 | + queryClient.invalidateQueries({ queryKey: ['complexes'] }); // コンプレックス一覧キャッシュを無効化 |
| 68 | + alert(t('complexForm.successMessage')); // 成功メッセージ |
| 69 | + navigate('/'); // 一覧ページへ遷移 |
| 70 | + }, |
| 71 | + onError: (error) => { |
| 72 | + alert(`${t('complexForm.errorMessage')}: ${error.message}`); // エラーメッセージ |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + const onSubmit: SubmitHandler<ComplexInput> = (data) => { |
| 77 | + createComplexMutation.mutate(data); |
| 78 | + }; |
| 79 | + |
| 80 | + const handleCancel = () => { |
| 81 | + navigate('/'); // キャンセルで一覧ページへ戻る |
| 82 | + }; |
| 83 | + |
| 84 | + return ( |
| 85 | + <PageWrapper> |
| 86 | + <Header onAddNewComplex={() => navigate('/complexes/new')} />{' '} |
| 87 | + {/* ヘッダーのボタンは自身のページへ */} |
| 88 | + <MainContent> |
| 89 | + <PageTitle>{t('complexForm.title')}</PageTitle> |
| 90 | + <Form onSubmit={handleSubmit(onSubmit)}> |
| 91 | + <FormGroup |
| 92 | + label={t('complexForm.contentLabel')} |
| 93 | + htmlFor="content" |
| 94 | + error={errors.content?.message} |
| 95 | + > |
| 96 | + <Textarea |
| 97 | + id="content" |
| 98 | + {...register('content', { |
| 99 | + required: t('complexForm.contentRequired'), |
| 100 | + })} |
| 101 | + /> |
| 102 | + </FormGroup> |
| 103 | + <FormGroup |
| 104 | + label={t('complexForm.categoryLabel')} |
| 105 | + htmlFor="category" |
| 106 | + error={errors.category?.message} |
| 107 | + > |
| 108 | + <Input |
| 109 | + id="category" |
| 110 | + type="text" |
| 111 | + {...register('category', { |
| 112 | + required: t('complexForm.categoryRequired'), |
| 113 | + })} |
| 114 | + /> |
| 115 | + </FormGroup> |
| 116 | + <ActionsWrapper> |
| 117 | + <Button |
| 118 | + variant="secondary" |
| 119 | + onClick={handleCancel} |
| 120 | + disabled={createComplexMutation.isLoading} |
| 121 | + > |
| 122 | + {t('complexForm.cancelButton')} |
| 123 | + </Button> |
| 124 | + <Button |
| 125 | + variant="primary" |
| 126 | + type="submit" |
| 127 | + disabled={createComplexMutation.isLoading} |
| 128 | + > |
| 129 | + {createComplexMutation.isLoading |
| 130 | + ? t('complexForm.savingButton') |
| 131 | + : t('complexForm.submitButton')} |
| 132 | + </Button> |
| 133 | + </ActionsWrapper> |
| 134 | + </Form> |
| 135 | + </MainContent> |
| 136 | + {/* <Footer /> */} |
| 137 | + </PageWrapper> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +export default ComplexFormPage; |
0 commit comments