refactor: 登録・編集フォームを共通化する(重複解消) - #45
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughThe bottle registration and editing forms now share ChangesBottle Form Refactor
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/app/bottles/bottle-form.tsx (1)
53-71: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider logging the caught error before showing the generic message.
The
catchblock swallows any error thrown byonSubmit(network failures, unexpected runtime errors) and only surfaceserrorLabel. If a non-network bug occurs in a wrapper'sonSubmit, there's no trace to diagnose it.♻️ Suggested addition
const submit = handleSubmit(async (data) => { setServerError(null); try { await onSubmit(data); - } catch { + } catch (error) { + console.error(error); setServerError(errorLabel); } });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/app/bottles/bottle-form.tsx` around lines 53 - 71, Update the submit callback’s catch block in the form’s submit flow to capture the thrown error and log it before setting the generic serverError message. Preserve the existing errorLabel shown to users while ensuring unexpected onSubmit failures are retained for diagnosis.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/app/bottles/bottle-form.tsx`:
- Around line 53-71: Update the submit callback’s catch block in the form’s
submit flow to capture the thrown error and log it before setting the generic
serverError message. Preserve the existing errorLabel shown to users while
ensuring unexpected onSubmit failures are retained for diagnosis.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 0c6d82da-70c2-4eb7-b812-9345799a9f4f
📒 Files selected for processing (4)
src/app/bottles/[id]/edit/edit-bottle-form.tsxsrc/app/bottles/bottle-form.tsxsrc/app/bottles/new/create-bottle-form.tsxsrc/app/bottles/new/page.tsx
8acf73a to
de3ff3a
Compare
対応しました( |
- src/app/bottles/bottle-form.tsx を共有フォーム(純粋な UI+検証、region「未選択」常設)として新設 - 登録・編集は薄いラッパー(create-bottle-form / edit-bottle-form)で defaultValues・文言・onSubmit を渡す - null 正規化は編集ラッパーの onSubmit に閉じ込め、登録は生 data を送る(作成の挙動・route テスト不変) - #41
de3ff3a to
d926689
Compare
関連 issue
resolve #41
やったこと
概要
登録・編集でほぼ同一だったフォーム(約 180 行の重複)を、共有コンポーネント
bottles/bottle-form.tsxに集約した。登録/編集の差分(初期値・送信先/メソッド・文言・成功時遷移・null 正規化の有無)は、各ページの薄いクライアントラッパーが props / onSubmit で注入する。純粋なリファクタで挙動は不変。変更点
src/app/bottles/bottle-form.tsx(新設):useForm+全フィールド JSX+serverError表示+submit ボタンを一元化。region の「未選択」クリアを常設。props=defaultValues/submitLabel/submittingLabel/errorLabel/onSubmit。送信の実処理(fetch・遷移)は持たず、onSubmit(data)を呼び出し元に委ねる。new/create-bottle-form.tsx(新設):空の初期値・登録文言・POST(生 data、null 正規化なし)→/bottles。[id]/edit/edit-bottle-form.tsx(230→50 行):既存値・更新文言・PATCH(null 正規化は当ラッパーに閉じ込め)→/bottles/[id]。new/page.tsx:CreateBottleFormを使うよう import 変更。旧new/bottle-form.tsxは削除(共有フォームへ移設)。受け入れ条件(セルフチェック)
動作確認
pnpm lintが通るpnpm testが通る(23 本・不変。ルートテストは共通化の影響を受けない)pnpm buildが通るIssue 要件外の対応(あれば)
備考
modeフラグは使わず props 注入で差分を吸収。共有フォームは登録/編集を意識しない。onSubmitは boolean を返す契約(成功=true/想定内の失敗=false)。想定内の失敗(サーバ !ok)は例外にせず false で通知し、共有フォームは文言のみ表示。console.errorは想定外の例外(catch)だけに限定(レビュー対応。「例外を制御フローに使わない」result パターン)。onSubmitは関数のためサーバ→クライアントに渡せない。よって各ページの"use client"ラッパーがonSubmitを定義し共有フォームに渡す(refactor: 登録・編集フォームを共通化する(重複解消) #41 本文の「各ページ」=この薄いラッパー)。