Add submit (next page) simulation on form preview#28
Conversation
|
Code ReviewSummaryThis PR adds submit/next page simulation to the form preview functionality by wrapping the preview content in a form element and implementing a handleSubmit function that navigates to the next page. This fixes the issue where the Next button wasn't functional in preview mode. Positive Aspects
Issues and Concerns1. Loss of Form Data (High Priority)The handleSubmit function in FormPreview.tsx:32-41 doesn't use the submitted form data. It only increments the page number without storing the user's input, updating the session state, or validating the form data before navigation. Impact: Users will lose all their input when navigating between pages in preview mode. Suggested fix: Update the session with form data before navigating using mergeSession to persist the data. 2. Type Safety Issue (Medium Priority)Line 36-39 in FormPreview.tsx spreads routeParams (type RouteData/qs.ParsedQs) into URLSearchParams constructor, which expects Record<string, string>. RouteData can contain nested objects or arrays, causing a type mismatch. 3. No Boundary Checking (Medium Priority)The navigation logic doesn't check if there's actually a next page to navigate to. This could lead to navigating to non-existent pages. Should check against the total number of pages in the form blueprint. 4. Action Handling Discrepancy (Low Priority)In Form.tsx:86-116, the non-preview form submission extracts the action from the submit button (submitEvent.submitter). This allows handling both Next and Back buttons. The preview mode doesn't support this, making it impossible to navigate backwards in preview. 5. Missing updatePrompt Call (Medium Priority)In the non-preview path (Form.tsx:105), updatePrompt(data) is called before onSubmit. The preview path doesn't call this, which might cause validation issues or component state inconsistencies. Test Coverage
Code Quality
Security and Performance
RecommendationsMust Address Before Merge:
Should Address: Nice to Have: Overall, this is a good start to fixing the preview functionality, but the form data loss issue needs to be addressed before merging to ensure the preview mode accurately simulates the real form experience. |
Pull Request Review: Add submit (next page) simulation on form previewSummaryThis PR successfully implements form submission and page navigation functionality in the preview mode, fixing issue #26 where the Next button wasn't working. The implementation follows a clean approach by adding a submit handler to FormPreview and ensuring proper form state management. ✅ Strengths
🔍 Potential Issues & Concerns1. Missing Dependency in useEffect (Form.tsx:83-87)The dependency array is missing formMethods, which could cause stale references. Since useForm typically returns a stable reference, this might not be a practical issue, but consider adding an ESLint disable comment with explanation if intentional. 2. Error Handling Inconsistency (FormPreview.tsx:39-42)Looking at the source code (response.ts:21-32), applyPromptResponse always returns success: true - it never fails. This makes the error check dead code. Either remove the error check since it's unreachable, or if future error handling is anticipated, consider how to communicate validation errors to the user. 3. No Bounds Checking for Page Navigation (FormPreview.tsx:48-54)CRITICAL: The code increments the page number without checking if there's actually a next page. This could navigate to non-existent pages. Add validation to prevent navigating beyond the last page. 4. URLSearchParams Spread Operation (FormPreview.tsx:50-52)Spreading routeParams into URLSearchParams might not work as expected if routeParams isn't a plain object. Verify that routeParams is a plain object or use URLSearchParams methods directly. 5. Missing Dependencies in useEffect (FormPreview.tsx:19-30)The useEffect dependency array is missing session, routeParams, and setSession. Add missing dependencies or use a ref pattern to avoid stale closures. |
🧪 Testing ConcernsNo tests were added for the new functionality. Recommendations:
Given the project uses Vitest and has Storybook integration, consider adding component tests using @vitest/browser. 🎨 Code Quality & Style
🔒 Security & Performance
📝 Recommendations SummaryHigh Priority:
Medium Priority:
✨ Overall AssessmentThis is a solid implementation that successfully fixes the reported issue. The code is clean and follows existing patterns. With the recommended fixes for edge cases and the addition of tests, this will be production-ready. Suggested Action: Address high-priority items before merging, particularly page bounds checking and dependency array issues. |
Code Review for PR #28: Add submit (next page) simulation on form previewSummaryThis PR fixes issue #26 by enabling the "Next" button functionality in the form preview mode. The implementation adds form submission handling and page navigation in the preview context. ✅ What Works Well
🔍 Potential Issues & Concerns1. Missing dependency in useEffect (Form.tsx:85-89)useEffect(() => {
if (isPreview && hasOnSubmit) {
formMethods.reset({});
}
}, [session.route?.params.page, isPreview, hasOnSubmit]);Issue: Missing Impact: May cause stale closures or unexpected behavior if Recommendation: Either add 2. Type safety concern with newParams (FormPreview.tsx:50-53)const newParams = new URLSearchParams({
...routeParams,
page: nextPage.toString(),
});Issue: Impact: Could fail at runtime if Recommendation: const newParams = new URLSearchParams({
...Object.fromEntries(
Object.entries(routeParams).map(([k, v]) => [k, String(v)])
),
page: nextPage.toString(),
});3. No boundary check for page navigation (FormPreview.tsx:47-49)const currentPage = Number(routeParams.page) || 0;
const nextPage = currentPage + 1;Issue: No validation that Impact: Could result in blank pages or errors when navigating past the final page. Recommendation: Add validation against the total number of pages in the session/blueprint before navigating. 4. Conditional rendering complexity (Form.tsx:96-144)The form now has three rendering paths:
Issue: The logic is becoming complex with nested ternaries. The middle branch (preview with submit) duplicates much of the non-preview form structure. Impact: Harder to maintain, potential for bugs if one branch is updated without the others. Recommendation: Consider extracting the form rendering logic into a helper component or refactor to reduce duplication: const FormWrapper = isPreview && !onSubmit ? 'div' : 'form';
const formProps = (isPreview && !onSubmit) ? {} : { /* form-specific props */ };5. Missing validation error displayIssue: When Impact: Poor UX - users won't know why the form didn't advance. Recommendation: Display validation errors in the UI, possibly using the existing error state mechanism. 🧪 Test CoverageConcern: No test files found for these components. Recommendation: Add tests covering:
🎯 Performance ConsiderationsMinor: The 🔒 SecurityNo security concerns identified. The code properly validates input through 📝 Suggestions
VerdictThe PR solves the immediate problem effectively but has several issues that should be addressed: Must fix before merge:
Should fix:
Nice to have:
Overall, the approach is sound and follows existing patterns in the codebase. With the critical fixes applied, this will be a solid addition. |
Fixes #26