-
Notifications
You must be signed in to change notification settings - Fork 396
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
Make add-on reducer state more explicit #3073
Conversation
Codecov Report
@@ Coverage Diff @@
## master mozilla/addons-frontend#3073 +/- ##
==========================================
+ Coverage 95.02% 95.06% +0.04%
==========================================
Files 152 152
Lines 2773 2778 +5
Branches 541 543 +2
==========================================
+ Hits 2635 2641 +6
+ Misses 119 118 -1
Partials 19 19
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a few questions.
src/core/reducers/addons.js
Outdated
|
||
|
||
export const ADDONS_LOADED = 'ADDONS_LOADED'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have LOAD_ADDON
instead? See: #3025 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, right, thanks
src/core/reducers/addons.js
Outdated
name: apiAddon.name, | ||
previews: apiAddon.previews, | ||
public_stats: apiAddon.public_stats, | ||
ratings: apiAddon.ratings || undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this one need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, whoops, good catch. That was leftover from me battling with Flow :/
} | ||
|
||
export default function addon( | ||
const initialState = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No Flow type here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a type for it -- you can see it assigned in the function signature: https://github.com/mozilla/addons-frontend/pull/3073/files/39fb66595b5b8033cd43288e61a19f35d9f32dbf#diff-21aa4f8ea4fba6406233649d0ce65beaR193
return addon.guid; | ||
} | ||
|
||
export function removeUndefinedProps(object: Object): Object { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this method needed because there are spreads in different places? Otherwise I would argue that the state should have a consistent shape.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is needed for spreads. I added a comment about it below. It's going to be hard to remove the spreads and I think it's a low priority fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a low priority fix.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, this is super-great. It makes the code a lot more readable and easy to grok.
r+wc from me.
type: string, | ||
|}; | ||
|
||
export function loadEntities(entities: Array<Object>): LoadEntitiesAction { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay!
type Action = Object; | ||
type AddonState = Object; | ||
export function loadAddons( | ||
entities: {| addons?: ExternalAddonMap |} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned previously I think it would be great to keep flow definitions out of the function signatures if they're being included. They really crowd the function signature and make it very difficult to parse exactly what's happening inside it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue I have with making them separate is that it slows down the reader. If you want to know what the type is, you have to cross-reference it from somewhere else. I understand how separation can be helpful for big definitions but this one only spans about 30 characters so I don't think that warrants separation.
|
||
// These are custom properties not in the API response. | ||
|
||
// TODO: remove this if possible. I think it was added by mistake |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
return apiAddon; | ||
|
||
if (apiAddon.current_version && apiAddon.current_version.files.length > 0) { | ||
// TODO: support specific platform files. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All these comments are very helpful 🥂!
src/core/reducers/addons.js
Outdated
const { addons } = action.payload; | ||
const newState = { ...state }; | ||
Object.keys(addons).forEach((key) => { | ||
newState[key] = createInternalAddon(addons[key]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉💃🎉 THANK YOU THIS IS SO MUCH EASIER TO READ 🎉💃🎉
src/core/sagas/addons.js
Outdated
import { fetchAddon as fetchAddonFromApi } from 'core/api'; | ||
import { FETCH_ADDON } from 'core/reducers/addons'; | ||
import { loadAddons, FETCH_ADDON } from 'core/reducers/addons'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick but I think we usually load constants first. It technically is alphabetical that way but I guess until we have a rule for sorting imports... 😞
@@ -25,7 +24,7 @@ export function* fetchAddon( | |||
yield put(showLoading()); | |||
const state = yield select(getState); | |||
const response = yield call(fetchAddonFromApi, { api: state.api, slug }); | |||
yield put(loadEntities(response.entities)); | |||
yield put(loadAddons(response.entities)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just way easier to read too. Thanks a million.
src/core/types/addons.js
Outdated
/* | ||
* This is the external API representation of an add-on. | ||
* | ||
* This is a detailed API response. Not all API responses |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linking to the API docs from here would be cool just so developers could understand better the varied details in responses. Maybe just http://addons-server.readthedocs.io/en/latest/topics/api/addons.html ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea
}); | ||
|
||
it('sets `isRestartRequired` to `true` when at least one of the files declares it', () => { | ||
it('sets `isRestartRequired` to `true` when some files declare it', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the aim here was to shorten this spec I'd say "when any file declares it".
…wap import orderswap import orderswap import orderswap import order
Thanks for the reviews! Let's start testing this on dev... |
Fixes mozilla/addons#10708
This will hopefully make the central add-on reducer easier to deal with for features and bug fixes but it has potential to break a lot of stuff. It would help if you could test out add-on/theme installs. I've manually tested AMO and the Discopane.