[IMPAC-757] Added radio mode for organization#457
[IMPAC-757] Added radio mode for organization#457xaun merged 4 commits intomaestrano:release/v1.6.9from
Conversation
xaun
left a comment
There was a problem hiding this comment.
Thanks for your PR @MAhsenArif, some changes on this one, mainly just to refactor the big block in the initializer. Cheers
| w.selectedOrganizations = {} | ||
|
|
||
|
|
||
| $scope.organizationMode ||= 'multiple' |
There was a problem hiding this comment.
@MAhsenArif is it apart of the specification for this "single" mode to be not used anywhere in the project? Just as it's being set here, and when I search the project there are no widgets that bind an organizationMode property to this directive.
There was a problem hiding this comment.
@xaun Currently I've have added the ability to use single mode but yes it's not being used anywhere. As per discussion with @cesar-tonnoir , this is something the developers will activate when coding a specific widget.
I've also added a message in PR description on how it can be used for a widget.
|
|
||
|
|
||
| $scope.organizationMode ||= 'multiple' | ||
| mode = $scope.organizationMode |
There was a problem hiding this comment.
Is it worth adding another line here when the below functions could access $scope.organizationMode directly?
There was a problem hiding this comment.
Added it as a bit of readability. $scope.organizationMode was too long to be used everywhere :)
| w.selectedOrganizations[orgUid] = !w.selectedOrganizations[orgUid] | ||
| $scope.onSelect({orgs: w.selectedOrganizations}) if angular.isDefined( $scope.onSelect ) | ||
| else if singleOrgMode() | ||
| angular.forEach w.selectedOrganizations, (value, key) -> |
There was a problem hiding this comment.
I think Lodash _.each outperforms angular.forEach, might as well use it as it's the standard for this stuff in impac-angular.
But for this circumstance, there is an even better Lodash method _.mapValues.
This could simply be _.mapValues(w.selectedOrganizations, -> false)
| if w.metadata? && w.metadata.organization_ids? | ||
| count = 0 | ||
| widgetOrgIds = w.metadata.organization_ids | ||
| currentOrganization = ImpacMainSvc.config.currentOrganization |
There was a problem hiding this comment.
This block could do with some refactoring, simplifying and commenting. The code is not very readable, and will cause future confusion for developers.
After looking into for a bit, Im not even sure it's necessary to be iterating over all the $scope.dashboardOrganizations and cramming all the logic into all the if's & else's, which Im not even sure we need all of.
I have tried to capture all the cases you have coded for in comments:
# single mode
# - has multiple data sources previously selected
# => deselect all & select current organization
# - none or 1 data source previously selected
# => apply the previously first widget data source organization, or select the first oneIs this correct?
We shouldn't have to "deselect" (e.g set all the key values to false), as this is inside an initialize method, no "checkboxes" are going to be ticked, right? This saves us having to iterate and set everything to false, and having to use a counter.
The thing with an ng-model binding, is that if it is undefined or null, it's falsey. We should only need to set it when it's been toggled for the first time.
I may not have capture all the cases, but this is a quick proposition of a refactor:
if w.metadata? && w.metadata.organization_ids?
if singleOrgMode()
currentOrganization = ImpacMainSvc.config.currentOrganization
unless _.isEmpty(currentOrganization)
w.selectedOrganizations[currentOrganization.uid] = true
else if (dhbOrg = $scope.dashboardOrganizations[0])?
w.selectedOrganizations[dhbOrg.uid] = trueAnyway I may be completely missing something, but if you could just define & code the logic in a clearer manner that would be awesome! Thanks :)
xaun
left a comment
There was a problem hiding this comment.
Thanks @MAhsenArif, LGTM..
Just a couple of suggestions & a question before merge. Let me know what you think
| currentOrganization = ImpacMainSvc.config.currentOrganization | ||
| if singleOrgMode() | ||
| resetSelectedOrgs() | ||
| if _.map(widgetOrgIds, -> true).length > 1 |
There was a problem hiding this comment.
why not just if widgetOrgIds.length > 1? What's the _.map for? If w.metadata.organization_ids is defined, it should always be an Array.
| @@ -36,21 +36,18 @@ module.controller('SettingOrganizationsCtrl', ($scope, $log, ImpacDashboardsSvc, | |||
| $scope.dashboardOrganizations = config.currentDashboard.data_sources | |||
|
|
|||
| if w.metadata? && w.metadata.organization_ids? | |||
There was a problem hiding this comment.
Might be nice to flatten this a bit by going:
return unless w.metadata? && w.metadata.organization_ids?Also I've noticed at the bottom of this callback, setting.isInitialized = true is nested within the if block, so if there are no w.metadata.organization_ids, it will never set itself as isInitialized.
Not sure if this is intended or not. But I think maybe we could move this into a .finally() callback?
ImpacDashboardSvc.load().then(
(config)->
...
).finally(->
settings.isInitialized = true
)What do you think?
There was a problem hiding this comment.
@xaun Agree with setting isInitialized in finally block. Though I'm not yet sure what it's purpose is. I searched for it but it is not used in any views.
There was a problem hiding this comment.
@MAhsenArif good point, it seems only the widgets-settings/accounts-list setting uses it in its settings.toMetadata method.
I think this was an old framework specific convention of sorts.. @cesar-tonnoir can you confirm if there is any reason to keep this property? It looks like our newer widgets-settings components are not implementing it? If not, we should remove these from the old widgets.
| else | ||
| w.selectedOrganizations[widgetOrgIds[0]] = true | ||
|
|
||
| if multiOrgMode() |
There was a problem hiding this comment.
Maybe use else if like you have in the $scope.toggleSelectOrganization method.
67f9bf9 to
42b4822
Compare
Radio mode can be set on Widgets by adding parameter
organization-mode="'single'"tosetting-organizationsdirective.