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

fix: Map and Label breaking on back action #3825

Merged
merged 4 commits into from
Oct 25, 2024
Merged

fix: Map and Label breaking on back action #3825

merged 4 commits into from
Oct 25, 2024

Conversation

RODO94
Copy link
Contributor

@RODO94 RODO94 commented Oct 18, 2024

The initial bug centred on navigating back after completing the Map and Label component, and finding the active tab not selected, then when adding a new point, the tabs would break. My solution was to change how the ActiveIndex was assigned and handled, and when the previous data from the passport was added to features. From this, I found a couple other bugs on how we handle labels when copying data.

Should all be fixed now with testing around navigating back. There is no test coverage right now for navigating back, adding new data, continuing forward, then navigating back again.

I would be interested to know if I should be rendering a different component within the tests so I can test clicking "Continue" then clicking "Back" - at the moment, I just recreate rendering a component with the previouslySubmittedData. Would be great to have feedback on whether this would be overkill.

Copy link

github-actions bot commented Oct 18, 2024

Removed vultr server and associated DNS entries

Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

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

Looks like you're on the right track for this bug! Know this is still draft, but want to flag what's seeming like a possible new side-effect bug of your changes:

Here's steps to recreate:

  • Navigate forwards to Map&Label & draw two features
  • Click "reset all" button the map -> features and tabs should both clear
  • Draw two features again
  • "Continue" forwards this time
  • Navigate "back" to Map&Label
  • Confirm I can add another feature to map and tab is added (works now!)
  • Click "reset all" button on the map -> map is cleared but the tabs are not (new bug?!)

Can you recreate too?

First guess would be to double check that any changes you're making to the map context methods stay in sync with their form method counterpart !

move test files to test folder

tidy test expects

fix: remove unused imports
@RODO94 RODO94 force-pushed the rory/map-back-bug branch from a237a8d to 407e57e Compare October 21, 2024 09:46
@RODO94 RODO94 requested a review from a team October 21, 2024 09:48
@RODO94 RODO94 marked this pull request as ready for review October 21, 2024 09:48
);

const [minError, setMinError] = useState<boolean>(false);
const [maxError, setMaxError] = useState<boolean>(false);
const [loadPreviousValues, setLoadPreviousValues] = useState<boolean>(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went round the blocks with this trying to find an elegant solution, this felt like the best option to ensure previous values only loaded once

Copy link
Member

Choose a reason for hiding this comment

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

I don't really understand the addition of these local state variables - why can't we simply ensure addInitialFeaturesToMap is only called if when previouslySubmittedData is present? What's a scenario where we're trying to load initial values more than once?

If we do indeed need this, can we rename it to something like loadPreviousValuesOnMap because it's not actually handling the form tab data (formik.initialValues is, right?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From what I could understand, previouslySubmittedData is always present once you navigate backwards, so when you add a new point the code will run again and it will add the new point to features then hit the if() statement then reassign features to be previouslySubmittedData.data...

I initially tried to place it in a useEffect() to only run the if statement on initial mount then whenever previouslySubmittedData changed, but I couldn't get it to work so easily, and it felt like a larger change than the state variable.

Have I missed a simpler solution?

Copy link
Member

@jessicamcinchak jessicamcinchak Oct 25, 2024

Choose a reason for hiding this comment

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

Thanks for explanation - happy to keep this approach then, it seems to be achieving what we want !

formik.setFieldValue(`schemaData[${destinationIndex}]`, sourceFeature);
formik.setFieldValue(`schemaData[${destinationIndex}]`, {
...sourceFeature,
label: `${destinationIndex + 1}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The label was being assigned based on the object you were copying from rather than the object we were copying to

Copy link
Member

Choose a reason for hiding this comment

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

Good catch but I don't think this is quite working in all scenarios correctly yet!

For example:

  • I add two trees on the map
  • I fill out tree two tab
  • On tab 1, I "copy from" tree 2
  • I "continue" & my labels are correct
  • I come "back" and "remove tree" from tab 1 - this shifts the map & tab labels for previous "tree 2" to 1 now
  • I "continue" again and my passport data has a single feature with label: 2 when it should be label: 1 to reflect map/tab changes after a point was removed !

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks so much for catching this, I'll add some adjustments

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Testing added for this and hope I have solved it. I couldn't find another bug through manual testing...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file was becoming quite long, so it felt right to move the back navigation tests to separate file, and relocate these files to the test folder on the Map and Label component

Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

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

On the right track - pizza is working more smoothly, but a few questions/comments while reading through the code!

);

const [minError, setMinError] = useState<boolean>(false);
const [maxError, setMaxError] = useState<boolean>(false);
const [loadPreviousValues, setLoadPreviousValues] = useState<boolean>(
Copy link
Member

Choose a reason for hiding this comment

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

I don't really understand the addition of these local state variables - why can't we simply ensure addInitialFeaturesToMap is only called if when previouslySubmittedData is present? What's a scenario where we're trying to load initial values more than once?

If we do indeed need this, can we rename it to something like loadPreviousValuesOnMap because it's not actually handling the form tab data (formik.initialValues is, right?)

if (loadPreviousValues) {
setFeatures(initialFeatures);
setLoadPreviousValues(false);
}
Copy link
Member

Choose a reason for hiding this comment

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

Again, see above comment - not following this change in particular !

export const previouslySubmittedDoubleData: PreviousData = {
auto: false,
...previousMockDoubleFeatureData,
};
Copy link
Member

Choose a reason for hiding this comment

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

Per your question in the PR description, mocking previouslySubmittedData is how I'd expect to see "back" navigation tested at the component level 👍 This is consistent with other @planx/components tests

formik.setFieldValue(`schemaData[${destinationIndex}]`, sourceFeature);
formik.setFieldValue(`schemaData[${destinationIndex}]`, {
...sourceFeature,
label: `${destinationIndex + 1}`,
Copy link
Member

Choose a reason for hiding this comment

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

Good catch but I don't think this is quite working in all scenarios correctly yet!

For example:

  • I add two trees on the map
  • I fill out tree two tab
  • On tab 1, I "copy from" tree 2
  • I "continue" & my labels are correct
  • I come "back" and "remove tree" from tab 1 - this shifts the map & tab labels for previous "tree 2" to 1 now
  • I "continue" again and my passport data has a single feature with label: 2 when it should be label: 1 to reflect map/tab changes after a point was removed !

const secondTabPanel = getByTestId("vertical-tabpanel-1");
expect(secondTabPanel).toBeVisible();

addMultipleFeatures([point1, point2, point3]);
Copy link
Member

@jessicamcinchak jessicamcinchak Oct 21, 2024

Choose a reason for hiding this comment

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

nit: aren't point1 and point2 already on the map? Am we actually mocking adding "multiple" features here or just one? I understand the mock data needs to reflect the full feature collection, but I wonder if there's a way we can make this more clear in helper function naming, a code comment etc?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is correct, it should just be adding one feature at a time. I'll adjust and switch the functions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Found that this was correct and just adding a single point deletes the others from the map, so I need them added like this. I've added a comment above each line explaining this need

refine naming of test mocks

remove test.only
@RODO94 RODO94 force-pushed the rory/map-back-bug branch from 9af3f9f to 960c1eb Compare October 22, 2024 12:51
Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

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

Working as expected for me now - thanks for iterating on this one !

@RODO94 RODO94 merged commit 8ef61d3 into main Oct 25, 2024
12 checks passed
@RODO94 RODO94 deleted the rory/map-back-bug branch October 25, 2024 09:22
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.

2 participants