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

Show/hide action list item paperclip icon when attachment is manually added/removed by users #1372

Merged
merged 1 commit into from
Jan 3, 2025

Conversation

nmajor25
Copy link
Contributor

@nmajor25 nmajor25 commented Jan 2, 2025

Shortcut Story ID: [sc-58034]

Summary by CodeRabbit

  • New Features

    • Enhanced attachment management for actions.
    • Improved file upload and removal handling.
    • Added user feedback for upload success and failure.
  • Bug Fixes

    • Corrected file relationship tracking in actions.
    • Ensured UI reflects accurate attachment status.
  • Tests

    • Added integration test for action attachment functionality.
    • Verified attachment icon updates during file upload and removal.

@nmajor25 nmajor25 requested a review from paulfalgout January 2, 2025 18:45
Copy link

coderabbitai bot commented Jan 2, 2025

Walkthrough

This pull request introduces changes to the file attachment handling process across multiple JavaScript files. The modifications focus on improving the management of file relationships for actions, specifically in how attachments are added and removed. The changes include updating the onAddAttachment and onRemoveAttachment methods, introducing new methods for file relationship management, and enhancing event handling for file uploads. An accompanying integration test has been added to verify the new attachment management functionality.

Changes

File Change Summary
src/js/apps/patients/sidebar/action-sidebar_app.js - Updated onAddAttachment to handle upload success/failure events
- Modified onRemoveAttachment to remove file relationships
src/js/entities-service/entities/actions.js - Added addFile method
- Added removeFile method
- Renamed and refactored existing attachment-related methods
src/js/entities-service/entities/files.js - Updated FileRemoved message handler
- Added upload:success event trigger
test/integration/patients/sidebar/action-sidebar.js - Added new test specification for action attachments

Sequence Diagram

sequenceDiagram
    participant User
    participant UI
    participant FileUploader
    participant ActionModel
    
    User->>UI: Upload File
    UI->>FileUploader: Upload File
    FileUploader-->>UI: Upload Success
    UI->>ActionModel: Add File Relationship
    ActionModel-->>UI: Update Action
    UI->>User: Show Attachment Icon
Loading

Possibly related PRs

Suggested reviewers

  • paulfalgout

Poem

🐰 A Rabbit's Ode to File Attachments 🗂️
Hop, hop, through code so neat,

Attachments dance to a rhythm sweet,

Files added, removed with grace,

WebSocket whispers leave no trace,

Code hops on with joyful might! 🚀


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be2d4df and d0e03a0.

📒 Files selected for processing (4)
  • src/js/apps/patients/sidebar/action-sidebar_app.js (1 hunks)
  • src/js/entities-service/entities/actions.js (2 hunks)
  • src/js/entities-service/entities/files.js (2 hunks)
  • test/integration/patients/sidebar/action-sidebar.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/js/entities-service/entities/files.js
  • src/js/entities-service/entities/actions.js
🔇 Additional comments (3)
src/js/apps/patients/sidebar/action-sidebar_app.js (2)

239-246: Improved upload handling with proper success/failure states.

The changes enhance the attachment upload process by:

  1. Using proper event handling for upload states
  2. Adding error feedback for failed uploads
  3. Managing file relationships correctly

252-252: LGTM: Proper cleanup of file relationships.

The addition of removeFile ensures proper cleanup of file relationships when an attachment is removed.

test/integration/patients/sidebar/action-sidebar.js (1)

971-1124: Well-structured test for attachment icon visibility.

The test comprehensively covers the attachment icon visibility feature:

  1. Verifies icon is hidden initially
  2. Confirms icon appears after file upload
  3. Validates icon disappears after file removal
  4. Includes proper API mocking and assertions

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

cypress bot commented Jan 2, 2025

RoundingWell Care Ops Frontend    Run #7124

Run Properties:  status check passed Passed #7124  •  git commit d0e03a0d74: Show/hide action list item paperclip icon when attachment is manually added/remo...
Project RoundingWell Care Ops Frontend
Branch Review attachment-icons
Run status status check passed Passed #7124
Run duration 03m 04s
Commit git commit d0e03a0d74: Show/hide action list item paperclip icon when attachment is manually added/remo...
Committer Nick Major
View all properties for this run ↗︎

Test results
Tests that failed  Failures 0
Tests that were flaky  Flaky 0
Tests that did not run due to a developer annotating a test with .skip  Pending 0
Tests that did not run due to a failure in a mocha hook  Skipped 0
Tests that passed  Passing 313
View all changes introduced in this branch ↗︎

@@ -236,6 +236,7 @@ export default App.extend(extend({
});
attachment.upload(file);

this.action.addAttachment(attachment);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When passing the attachment to the addAttachment function here, attachment.id is undefined. Which will cause the test I created in this PR to fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we need to wait until the upload is finished.

We could do something like this:

this.listenTo(attachment, 'upload:success', uploadedAttachment => {
  this.action.addAttachment(uploadedAttachment);
  Radio.request('ws', 'add', uploadedAttachment);
});

Or:

attachment.upload(file).then(uploadedFile => {
  this.action.addAttachment(uploadedAttachment);
  Radio.request('ws', 'add', uploadedAttachment);
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And if an upload fails ('upload:failed' triggered), this would also stop the paperclip icon and websocket from being added when they shouldn't be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Decided to create a new test. I couldn't figure out how to weave the testing of this feature into an existing one.

Since this test needs to have an action with this sequence:

  • Start with zero attachments (icon hidden)
  • Add an attachment (icon switched to shown)
  • Remove that attachment (icon switch back to hidden)

@@ -250,6 +249,11 @@ const _Model = BaseModel.extend({

return !!size(programAction.get('allowed_uploads'));
},
addAttachment(resource) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are the names of these clear? Could cause confusion in the future (for me at least).

Maybe they should be addAttachmentRelationship()/removeAttachmentRelationshop() or addFileRelationship()/removeFileRelationship() 🤔

@nmajor25 nmajor25 marked this pull request as ready for review January 2, 2025 21:45
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/js/entities-service/entities/actions.js (1)

252-256: Efficient creation of new files array via spread operator.

Using the spread operator to produce a new _files relationship array is clear and concise. However, consider verifying the file does not already exist in _files if duplicates aren’t desired.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 57f3cb4 and 8050d47.

📒 Files selected for processing (4)
  • src/js/apps/patients/sidebar/action-sidebar_app.js (2 hunks)
  • src/js/entities-service/entities/actions.js (2 hunks)
  • src/js/entities-service/entities/files.js (2 hunks)
  • test/integration/patients/sidebar/action-sidebar.js (1 hunks)
🔇 Additional comments (7)
src/js/entities-service/entities/files.js (2)

31-31: Adoption of removeFileRelationship is appropriate.

Replacing the old removeAttachment call with removeFileRelationship aligns well with the centralized file-relationship handling introduced elsewhere in this PR.


69-71: Good event-based approach for successful uploads.

Emitting the upload:success event allows higher-level components to respond to upload completion in a decoupled manner. This pattern is clean and maintainable.

src/js/apps/patients/sidebar/action-sidebar_app.js (2)

239-242: Properly adding the file relationship after successful upload.

Listening for upload:success and then invoking action.addFileRelationship(uploadedAttachment) ensures the UI and data model remain in sync, preventing race conditions and incomplete references to attachments.


251-252: Consistency in removing file relationship alongside model destruction.

Destroying the attachment model and removing its file relationship on the action object maintains data integrity by ensuring there are no stale references.

src/js/entities-service/entities/actions.js (2)

67-67: Refined approach to adding attachments.

Using addFileRelationship(attachmentModel) in the AttachmentAdded message centralizes attachment handling for better consistency and maintainability.


257-257: Robust removal of file relationship.

Filtering out the relevant file by its id ensures correctness when removing the relationship object from _files.

test/integration/patients/sidebar/action-sidebar.js (1)

971-1124: Comprehensive test coverage for attachment behavior in the action list.

The newly added test thoroughly verifies the presence/absence of the paperclip icon upon file add/remove, ensuring accurate reflection of attachment status in the UI. This end-to-end approach helps safeguard against regressions.

@coveralls
Copy link

coveralls commented Jan 2, 2025

Pull Request Test Coverage Report for Build a888a9b5-d682-4839-832a-9fa460d05fe5

Details

  • 10 of 10 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 99.987%

Totals Coverage Status
Change from base Build 5a6df85f-1ef9-4b08-afc6-fe6f8644da5f: 0.0%
Covered Lines: 6077
Relevant Lines: 6077

💛 - Coveralls

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/js/entities-service/entities/actions.js (1)

Line range hint 257-263: Consider consistent implementation style

While the implementation is correct, consider making it consistent with addFileRelationship's style:

  removeFileRelationship(resource) {
-   const files = this.get('_files');
-   const newFilesRelationship = files.filter(file => {
-     return file.id !== resource.id;
-   });
+   const newFilesRelationship = this.get('_files').filter(file => file.id !== resource.id);

    this.set({ _files: newFilesRelationship });
  },
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8050d47 and be2d4df.

📒 Files selected for processing (4)
  • src/js/apps/patients/sidebar/action-sidebar_app.js (2 hunks)
  • src/js/entities-service/entities/actions.js (2 hunks)
  • src/js/entities-service/entities/files.js (2 hunks)
  • test/integration/patients/sidebar/action-sidebar.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/js/entities-service/entities/files.js
  • test/integration/patients/sidebar/action-sidebar.js
🔇 Additional comments (4)
src/js/apps/patients/sidebar/action-sidebar_app.js (2)

239-242: Great improvement in upload handling!

The code now correctly waits for the upload to complete before adding the file relationship and websocket subscription. This fixes the previous issue with undefined attachment.id.


251-252: Clean implementation of attachment removal!

The code properly maintains the file relationship state after attachment removal.

src/js/entities-service/entities/actions.js (2)

67-67: Good architectural choice!

Using the dedicated addFileRelationship method centralizes the file relationship management logic.


252-256: Well-implemented relationship management!

The method properly maintains immutability while adding new file relationships.

@@ -236,7 +236,10 @@ export default App.extend(extend({
});
attachment.upload(file);

Radio.request('ws', 'add', attachment);
this.listenTo(attachment, 'upload:success', uploadedAttachment => {
Copy link
Member

Choose a reason for hiding this comment

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

we should combine this with 244 this.listenTo(attachment, {}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep good call, updated to that 👍

@@ -250,7 +249,12 @@ const _Model = BaseModel.extend({

return !!size(programAction.get('allowed_uploads'));
},
removeAttachment(resource) {
addFileRelationship(resource) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove the "Relationship" from the name The other instances like this don't have it.. addTag removeTag 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.

Makes sense, updated to that 👍

@paulfalgout paulfalgout merged commit b23d218 into develop Jan 3, 2025
6 checks passed
@paulfalgout paulfalgout deleted the attachment-icons branch January 3, 2025 18:33
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