Skip to content

Refactor indentation and improve portfolio form functionality#120

Merged
erandatheewijeratne merged 1 commit intomasterfrom
portfolio-optimization-theekshana
May 20, 2025
Merged

Refactor indentation and improve portfolio form functionality#120
erandatheewijeratne merged 1 commit intomasterfrom
portfolio-optimization-theekshana

Conversation

@TheekshanaWijerathne
Copy link
Copy Markdown
Contributor

Adjusted code indentation for better readability and consistency across the file. Improved the portfolio form by adding validation to ensure required fields are filled and enhancing data handling for user interactions like stock selection and form submission.

Adjusted code indentation for better readability and consistency across the file. Improved the portfolio form by adding validation to ensure required fields are filled and enhancing data handling for user interactions like stock selection and form submission.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors indentation for consistency and enhances the portfolio optimization form by adding client-side validation and a custom submit handler.

  • Standardized indentation and spacing across the component
  • Introduced handleSubmit with field validation before form submission
  • Switched <form> to use onSubmit and built out FormData handling for all inputs
Comments suppressed due to low confidence (1)

src/app/(dashboard)/dashboard/portfolio/_components/port-opt-form.tsx:211

  • The duplicate-check branch (if (selectedTickers.includes(ticker)) return;) was removed, allowing users to select the same ticker multiple times. Consider re-adding or relocating this guard to prevent duplicates.
const handleTickerSelect = (ticker: string) => {

Comment on lines +258 to +262
!years ||
!investmentAmount ||
!targetAmount ||
exceedsBalance ||
(optimizationMethod === "custom_risk" && !riskScorePercent)
Copy link

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation checks use truthy/falsy evaluation (e.g., !years, !investmentAmount) which will reject valid zero values and might misinterpret empty strings. Use explicit comparisons or ensure proper type checks.

Suggested change
!years ||
!investmentAmount ||
!targetAmount ||
exceedsBalance ||
(optimizationMethod === "custom_risk" && !riskScorePercent)
years === undefined || years === null || years === "" ||
investmentAmount === undefined || investmentAmount === null || investmentAmount === "" ||
targetAmount === undefined || targetAmount === null || targetAmount === "" ||
exceedsBalance ||
(optimizationMethod === "custom_risk" && (riskScorePercent === undefined || riskScorePercent === null || riskScorePercent === ""))

Copilot uses AI. Check for mistakes.
Comment on lines +270 to +272
formData.append('years', years);
formData.append('investmentAmount', investmentAmount);
formData.append('targetAmount', targetAmount);
Copy link

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appending non-string values (years, investmentAmount, targetAmount) directly to FormData can lead to incorrect serialization. Convert all input values to strings before appending.

Suggested change
formData.append('years', years);
formData.append('investmentAmount', investmentAmount);
formData.append('targetAmount', targetAmount);
formData.append('years', years.toString());
formData.append('investmentAmount', investmentAmount.toString());
formData.append('targetAmount', targetAmount.toString());

Copilot uses AI. Check for mistakes.
if (state.success && state.data) {
const encodedData = encodeURIComponent(JSON.stringify(state.data));
router.push(`/dashboard/portfolio/results?data=${encodedData}`);
const handleSubmit = (e: React.FormEvent) => {
Copy link

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The inline validation block is long and complex. Extracting the validation logic into a separate helper (e.g., validateForm()) would improve readability and testability.

Copilot uses AI. Check for mistakes.
exceedsBalance ||
(optimizationMethod === "custom_risk" && !riskScorePercent)
) {
return;
Copy link

Copilot AI May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning silently on validation failure provides no user feedback. Consider disabling the submit button when the form is invalid or displaying inline error messages.

Copilot uses AI. Check for mistakes.
@erandatheewijeratne erandatheewijeratne merged commit 0e8bc0a into master May 20, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants