Skip to content

[IMPAC-757] Added radio mode for organization#457

Merged
xaun merged 4 commits intomaestrano:release/v1.6.9from
MAhsenArif:organization_radio_mode
Feb 8, 2018
Merged

[IMPAC-757] Added radio mode for organization#457
xaun merged 4 commits intomaestrano:release/v1.6.9from
MAhsenArif:organization_radio_mode

Conversation

@MAhsenArif
Copy link
Copy Markdown
Contributor

@MAhsenArif MAhsenArif commented Jan 23, 2018

Radio mode can be set on Widgets by adding parameter organization-mode="'single'" to setting-organizations directive.

Copy link
Copy Markdown
Contributor

@xaun xaun left a comment

Choose a reason for hiding this comment

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

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'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it worth adding another line here when the below functions could access $scope.organizationMode directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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) ->
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 one

Is 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] = true

Anyway 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 xaun self-assigned this Feb 1, 2018
Copy link
Copy Markdown
Contributor

@xaun xaun left a comment

Choose a reason for hiding this comment

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

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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.

@xaun xaun assigned MAhsenArif and unassigned MAhsenArif Feb 5, 2018
else
w.selectedOrganizations[widgetOrgIds[0]] = true

if multiOrgMode()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe use else if like you have in the $scope.toggleSelectOrganization method.

@MAhsenArif MAhsenArif force-pushed the organization_radio_mode branch from 67f9bf9 to 42b4822 Compare February 6, 2018 05:24
@xaun xaun changed the base branch from 1.6 to release/v1.6.9 February 8, 2018 12:02
@xaun xaun merged commit de4522f into maestrano:release/v1.6.9 Feb 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.

2 participants