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

Simplify annotation component by removing duplicated state #8

Merged
merged 7 commits into from
Jul 4, 2016

Conversation

robertknight
Copy link
Member

@robertknight robertknight commented Jul 1, 2016

Note: This PR depends on #7

This PR simplifies the annotation component by removing the private state (annotation object, modified tags/text/privacy, editing vs. viewing state) which duplicated data from the annotation it was displaying and the drafts service.

This PR makes the displayed annotation a function of:

  1. The current saved or just-created annotation (accessed via vm.annotation)
  2. The current draft (accessed via drafts.get(vm.annotation))
  3. A flag indicating whether the annotation is being saved. This is still kept in AnnotationController at the moment but will move to the app state in future

As a consequence of this cleanup, many issues related to drafts re-appearing when switching groups or when clicking Cancel and switching groups have been fixed. Many tests of external <-> private state syncing for the <annotation> component have now been rendered redundant.

I would recommend commit-by-commit review, but testing the whole set together.

Fixes hypothesis/h#3380
Fixes hypothesis/h#3560
Fixes hypothesis/h#3561

@robertknight
Copy link
Member Author

@seanh
Copy link
Contributor

seanh commented Jul 1, 2016

hypothesis/h#3559 doesn't seem to be fixed, still seeing it

@seanh
Copy link
Contributor

seanh commented Jul 1, 2016

Found one issue that I can reproduce on this branch but not on master:

  1. Create an annotation and save it.
  2. Create a second annotation - first annotation disappears from sidebar.
  3. If you save the second annotation then reload the page you can see both annotations at once.

untitled screencast - edited 27

@robertknight
Copy link
Member Author

hypothesis/h#3559 doesn't seem to be fixed, still seeing it

Indeed. I realized I missed out the fix for that. I think that would be best put in a separate PR after this one is merged. I've removed #3559 from the description.

As a step towards making <annotation> stateless or nearly-stateless and
instead just a display of an annotation derived from the current
annotation instance and its draft, remove the `domainModel` copy of
`vm.annotation`.
Instead of maintaining a state flag indicating whether an annotation is
being viewed, edited or created, derive that state from whether the
annotation has an ID and whether it has an unsaved draft or not.

In the process this commit simplifies the tests for reverting edits and
adds a missing test that the annotation is deleted if new when clicking
the Cancel button.
These variables duplicated the information from the current draft for an
annotation. Instead of restoring a draft when an annotation card is
created and saving it when the annotation card is destroyed, just update
the draft whenever the tags, text or privacy of the annotation is
changed. The text/tags/privacy shown in the card are determined by
`vm.state()` which merges the annotation and the draft.

Removing these variables lets us also remove or simplify the tests that
checked that everything remained in sync.
…lied

Replace `updateDomainModel()` with a simpler function which returns a
copy of the input annotation with the changes from the editor applied.
The `updateViewModel()` function was responsible for computing several
pieces of data (links, document domain and title) used by the annotation
template.

Since we can now rely on vm.annotation being immutable, we can just
compute this data lazily when we need it and use memoization to avoid
unnecessary recalculation.
@robertknight
Copy link
Member Author

Found one issue that I can reproduce on this branch but not on master:

Fixed by 6f747ba

@seanh
Copy link
Contributor

seanh commented Jul 1, 2016

Alright, the code changes all look like improvements to me. I've tested this thoroughly and though I found several issues that I could also reproduce on master (and opened GitHub issues for), I only found the one issue that I've GIFted you ( :) ) above that's unique to this branch.

There are so many different cases and paths to test with this sidebar that it's hard to comprehensively say that a change doesn't introduce any new bugs.

But I think if the one above is fixed then we should merge this.

The correct fix would be to remove new annotations with an empty draft
in the reducer function that handles new annotations being created. That
is not currently possible because drafts are not stored in the Redux
store. Therefore this commit just manually de-registers the listener
when <annotation> goes out of scope.
@seanh seanh merged commit 41904b1 into master Jul 4, 2016
@seanh seanh deleted the simplify-annot-cmp branch July 4, 2016 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants