296 - Refactor show/hide 'other' college field #310
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #296
Refactor of setupOtherCollegeCheckbox Function for Dynamic Form Interaction
Overview
This pull request introduces a refactor of the setupOtherCollegeCheckbox function. The refactor enhances the interaction with the "Other College" checkbox and related text field in our forms, specifically on the edit and new pages. The changes improve the code's readability and functionality, ensuring a more intuitive user experience.
Key Changes
Integration with Turbolinks:
The function is now explicitly executed only when the Turbolinks load event is triggered and on specific pages ("/edit" and "/new"). This ensures the code runs only in the relevant context, improving performance and preventing unnecessary executions.
Dynamic State Synchronization:
Implemented logic to check the "Other College" checkbox automatically if the corresponding text field has a pre-filled value. This is particularly useful in edit forms, ensuring that the state of the checkbox correctly reflects the existing data.
Visibility Control of the Text Field:
Adjusted the code to show or hide the "Other College" text field based on the state of the checkbox. This makes the form more dynamic and responsive to user input, improving the overall user experience.
Introduction of tempStorage for State Preservation:
Added a tempStorage property to the text field element to store its value temporarily. This allows for the text field's content to be preserved and restored if the checkbox is toggled, preventing data loss during user interaction.
Refined Event Handling:
Replaced inline event handling with a more robust event listener attached to the checkbox. This approach follows best practices for event handling in JavaScript and enhances maintainability.
Move repeated College fields and logic to its own partial
Each of the publication types' forms repeated the block of college selection and the "other" college field. I took this PR as an opportunity to move this block into a partial to DRY the code up a bit and to make future maintenance easier. It will also place the Other College selection field in a consistent place in each of the publication types.