Skip to content

Commit

Permalink
fix: impl max dims rule (#517)
Browse files Browse the repository at this point in the history
* fix: swapping based on rules

* fix: Updated Analytics dep to v2.6.13

* fix: add comments

* fix: update tests
  • Loading branch information
janhenrikoverland committed Dec 2, 2019
1 parent aa96e73 commit 241de69
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 271 deletions.
2 changes: 1 addition & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"redux-mock-store": "^1.5.3"
},
"dependencies": {
"@dhis2/analytics": "^2.6.12",
"@dhis2/analytics": "^2.6.13",
"@dhis2/d2-ui-core": "^6.2.1",
"@dhis2/d2-ui-file-menu": "^6.2.1",
"@dhis2/d2-ui-interpretations": "^6.2.1",
Expand Down
67 changes: 34 additions & 33 deletions packages/app/src/modules/layout.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import i18n from '@dhis2/d2-i18n';
import i18n from '@dhis2/d2-i18n'
import {
AXIS_ID_COLUMNS,
AXIS_ID_ROWS,
AXIS_ID_FILTERS,
} from '@dhis2/analytics';
getAxisMaxNumberOfDimensions,
} from '@dhis2/analytics'

// Names for dnd sources
export const SOURCE_DIMENSIONS = 'dimensions';
export const SOURCE_DIMENSIONS = 'dimensions'

// Keys and displayName for adding dimensions to layout
export const ADD_TO_LAYOUT_OPTIONS = [
{ axisId: AXIS_ID_COLUMNS, name: i18n.t('Add to series') },
{ axisId: AXIS_ID_ROWS, name: i18n.t('Add to category') },
{ axisId: AXIS_ID_FILTERS, name: i18n.t('Add to filter') },
];
]

export const menuLabels = {
columns: i18n.t('series'),
rows: i18n.t('category'),
filters: i18n.t('filter'),
};
}

// Layout utility functions

// Exclude one or many dimensions from layout
export const getFilteredLayout = (layout, excludedIds) => {
const ids = Array.isArray(excludedIds) ? excludedIds : [excludedIds];
const ids = Array.isArray(excludedIds) ? excludedIds : [excludedIds]

return {
[AXIS_ID_COLUMNS]: layout[AXIS_ID_COLUMNS].filter(
Expand All @@ -35,44 +36,44 @@ export const getFilteredLayout = (layout, excludedIds) => {
[AXIS_ID_FILTERS]: layout[AXIS_ID_FILTERS].filter(
dim => !ids.includes(dim)
),
};
};
}
}

// Accepts layout: { columns: ['dx'] }
// Returns inverse layout: { dx: 'columns' }
export const getInverseLayout = layout => {
const entries = Object.entries(layout);
const map = {};
const entries = Object.entries(layout)
const map = {}

entries.forEach(([axisId, dimensionIds]) => {
dimensionIds.forEach(id => {
map[id] = axisId;
});
});
map[id] = axisId
})
})

return map;
};
return map
}

// Accepts layout and modification object
// Returns modObj with swapped dimensions
export const getSwapModObj = (layout, modObj) => {
const inverseLayout = getInverseLayout(layout);
const dimensionIds = Object.keys(modObj);
const swappedModObj = {};
// Accepts layout and transfer object
// Returns transfer with possible retransfers
export const getRetransfer = (layout, transfer, visType) => {
const inverseLayout = getInverseLayout(layout)
const dimensionIds = Object.keys(transfer)
const retransfer = {}

dimensionIds.forEach(id => {
const existsAt = inverseLayout[id];
const destinationAxis = modObj[id];
const dimensionAtDestination = layout[destinationAxis][0];
const sourceAxis = inverseLayout[id] || null
const destinationAxisId = transfer[id]
const dimensionsAtDestination = layout[destinationAxisId] || []

if (
existsAt &&
destinationAxis !== AXIS_ID_FILTERS &&
dimensionAtDestination
) {
swappedModObj[dimensionAtDestination] = existsAt;
const axisIsFull =
dimensionsAtDestination.length ===
getAxisMaxNumberOfDimensions(visType, destinationAxisId)

if (axisIsFull) {
retransfer[dimensionsAtDestination[0]] = sourceAxis
}
});
})

return swappedModObj;
};
return retransfer
}
Loading

0 comments on commit 241de69

Please sign in to comment.