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

Dedupe saved themes #231

Merged
merged 6 commits into from May 8, 2018
Merged

Dedupe saved themes #231

merged 6 commits into from May 8, 2018

Conversation

ghost
Copy link

@ghost ghost commented Apr 14, 2018

It Fixes #166
Disallows to save a theme, if it is present in the saved theme already.

@pdehaan @johngruen please review this.

@ghost ghost mentioned this pull request Apr 14, 2018
for (const key of Object.keys(themesList)) {
isThemeDuplicate = themesEqual(theme, themesList[key].theme);
if (isThemeDuplicate) {
callback(isThemeDuplicate);
Copy link
Contributor

Choose a reason for hiding this comment

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

From the looks of this, wouldn't the callback be called twice here? Once on L42 if a match was found, and again on L46 after the break statement breaks out of the for..of loop.

Copy link
Author

Choose a reason for hiding this comment

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

That's a nice catch. Thanks.
I pushed the latest changes, please review it.

);
notifySelfForStorage(storageKey);
checkDuplicateTheme(theme, (isThemeDuplicate) => {
if (!isThemeDuplicate) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since localStorage.setItem is syncronous, you don't need a callback here. You can just return the result from isThemeDuplicate

Copy link
Author

Choose a reason for hiding this comment

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

Thank you. I presumed it was asynchronous. Will make this change.

if (isThemeDuplicate) {
break;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be written a little more cleanly, maybe something like:

return Object.keys(listThemes()).filter((key) => {
  return themesEqual(theme, themesList[key].theme);
}).length > 0;

Copy link
Contributor

Choose a reason for hiding this comment

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

@meandavejustice: Wouldn't we want something like Array#some() [instead of Array#filter()] which should exit the loop as soon as a match is found?

Copy link
Contributor

Choose a reason for hiding this comment

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

@pdehaan Sure thing, some is even better!

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for this suggestion too. I learnt a new hack.


function checkDuplicateTheme(theme) {
const themesList = listThemes();
return Object.keys(listThemes()).some((key) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for being annoying...
Small nit, the listThemes() here should be themesList from L36 above, so we aren't calling the method twice.

Copy link
Author

Choose a reason for hiding this comment

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

Ahh, I missed that one. Thanks. Will push the changes.

})
);
notifySelfForStorage(storageKey);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Semi-related, but do we need an else statement?
Re-reading @johngruen's #166 (comment), it sounded like we may want to pop the newly saved duplicate theme to the front of the list if it was a duplicate. Not sure if the code is currently doing that and I'm just not reading it correctly, or if we need an else where we update the modified date to Date.now() and do whatever animations we wanted.

Copy link
Author

Choose a reason for hiding this comment

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

Since the functionality is the main focus here I did not make changes for animation effect. I need help with that. I'm still figuring out how to invoke the animation in SavedThemeSelector component when Save button is clicked. Could you please give a hint on how to proceed with that?

Copy link
Author

Choose a reason for hiding this comment

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

Can we merge this code and open a new issue to include animation effect?

Copy link
Contributor

Choose a reason for hiding this comment

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

@csd713 I'm cool with doing the animation in a separate issue, you wanna open one up with a link to john's comment from #166 ?

Also this needs a rebase after some changes from this morning before I can merge

Copy link
Author

Choose a reason for hiding this comment

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

Hi @meandavejustice that sounds good.
While testing the changes after resolving the conflicts, I found that 'Save' button is disabled if there is no change from 'currentSaved' theme.
One case where this functionality (of checking duplicate saved themes) would be useful is when a user presses 'undo/redo' button and tries to save the same theme again.
So, would you still recommend to add this functionality?
Please let me know if I'm missing something here.

Copy link
Author

Choose a reason for hiding this comment

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

@pdehaan @meandavejustice Could you please review if we still need to fix this issue?
If not, we can close this PR.

@lmorchard
Copy link
Contributor

Cleaned up the conflicts and think I'm going to merge this. There's some UI polish we can add on top of this, but it works as a start!

@lmorchard lmorchard merged commit 71d439b into mozilla:master May 8, 2018
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.

4 participants