Skip to content

Commit

Permalink
Removed default sorting from the table components in order to prevent…
Browse files Browse the repository at this point in the history
… UI errors
  • Loading branch information
DominikNoga committed Apr 22, 2024
1 parent d55c3cd commit e6b5f65
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 57 deletions.
3 changes: 0 additions & 3 deletions src/admin-orderable-list/admin-orderable-list.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
showInNavigation: true,
label: 'adminOrderableList.products',
url: '/orderables?code&name&description&program&page&size&sort',
params: {
sort: 'fullProductName,asc'
},
controller: 'OrderableListController',
templateUrl: 'admin-orderable-list/orderable-list.html',
controllerAs: 'vm',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@
openlmisTableSortingService.$inject = ['SORTING_SERVICE_CONSTANTS', '$state', '$stateParams', 'alertService'];

function openlmisTableSortingService(SORTING_SERVICE_CONSTANTS, $state, $stateParams, alertService) {
var sortingProperites = {
var EMPTY_SORTING_PROPERTIES = {
isSortedBy: undefined,
sortingOrder: undefined,
headerClass: undefined
sortingOrder: '',
headerClass: ''
};

var sortingProperties = angular.copy(EMPTY_SORTING_PROPERTIES);

return {
sortTable: sortTable,
setHeadersClasses: setHeadersClasses,
Expand All @@ -61,7 +63,7 @@

var propertyToOrder = propertyPathParts[0];
var stateParams = JSON.parse(JSON.stringify($stateParams));
setSortingProperties(selectedColumn.propertyPath);
setSortingProperties(propertyToOrder);
stateParams.sort = getSortingParamValue(propertyToOrder);
$state.go($state.current.name, stateParams);
} else {
Expand All @@ -81,38 +83,34 @@
}

function getSortingParamValue(propertyToOrder) {
if (typeof sortingProperites.isSortedBy === 'undefined') {
if (sortingProperties.isSortedBy === undefined) {
return undefined;
}
return propertyToOrder + ',' + sortingProperites.sortingOrder;
return propertyToOrder + ',' + sortingProperties.sortingOrder;
}

function setSortingProperties(propertyToOrder) {
if (sortingProperites.isSortedBy === undefined ||
sortingProperites.isSortedBy !== propertyToOrder
if (sortingProperties.isSortedBy === undefined ||
sortingProperties.isSortedBy !== propertyToOrder
) {
setSortingPropertiesValue(
propertyToOrder,
SORTING_SERVICE_CONSTANTS.ASC,
SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS
);
} else if (sortingProperites.sortingOrder === SORTING_SERVICE_CONSTANTS.ASC) {
setSortingPropertiesValue(
propertyToOrder,
SORTING_SERVICE_CONSTANTS.DESC,
SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS
);
setSortingPropertiesValue({
isSortedBy: propertyToOrder,
sortingOrder: SORTING_SERVICE_CONSTANTS.ASC,
headerClass: SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS
});
} else if (sortingProperties.sortingOrder === SORTING_SERVICE_CONSTANTS.ASC) {
setSortingPropertiesValue({
isSortedBy: propertyToOrder,
sortingOrder: SORTING_SERVICE_CONSTANTS.DESC,
headerClass: SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS
});
} else {
setSortingPropertiesValue(undefined, '', '');
setSortingPropertiesValue(angular.copy(EMPTY_SORTING_PROPERTIES));
}
}

function setSortingPropertiesValue(isSortedBy, sortingOrder, headerClass) {
sortingProperites = {
isSortedBy: isSortedBy,
sortingOrder: sortingOrder,
headerClass: headerClass
};
function setSortingPropertiesValue(sortingPropertiesValue) {
sortingProperties = angular.copy(sortingPropertiesValue);
}

/**
Expand All @@ -134,34 +132,45 @@
}

function setInitialSortingProperties() {
var sortParam = $stateParams.sort;
var sortParam = Array.isArray($stateParams.sort) ?
$stateParams.sort[0] :
$stateParams.sort;

if (sortParam) {
if (typeof sortParam === 'object' && sortParam.length !== undefined) {
sortParam = sortParam[0];
}
var sortParamParts = sortParam.split(',');
var isSortedBy = sortParamParts[0];
// Removing whitespaces from sorting param
var sortingOrder = sortParamParts[1].split(' ').join('');
var headerClass = sortingOrder === SORTING_SERVICE_CONSTANTS.ASC ?
SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS : SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS;

setSortingPropertiesValue(isSortedBy, sortingOrder, headerClass);
sortingProperties = getSortingPropertiesFromParam(sortParam);
} else {
setSortingPropertiesValue(undefined, '', '');
setSortingPropertiesValue(EMPTY_SORTING_PROPERTIES);
}
}

function getSortingPropertiesFromParam(sortParam) {
if (sortParam === undefined) {
setSortingPropertiesValue(EMPTY_SORTING_PROPERTIES);
}

var sortParamParts = sortParam.split(',');
var isSortedBy = sortParamParts[0];
// Removing whitespaces from sorting param
var sortingOrder = sortParamParts[1].split(' ').join('');
var headerClass = sortingOrder === SORTING_SERVICE_CONSTANTS.ASC ?
SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS : SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS;

return {
isSortedBy: isSortedBy,
sortingOrder: sortingOrder,
headerClass: headerClass
};
}

function getColumnClass(column) {
var baseClass = column.headerClasses ? column.headerClasses : '';

return isSortedByColumn(column.propertyPath) ?
baseClass + ' ' + sortingProperites.headerClass : baseClass;
baseClass + ' ' + sortingProperties.headerClass : baseClass;
}

function isSortedByColumn(propertyPath) {
return propertyPath === sortingProperites.isSortedBy;
return propertyPath === sortingProperties.isSortedBy;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/order-fulfillment/order-fulfillment.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
FULFILLMENT_RIGHTS.SHIPMENTS_VIEW,
FULFILLMENT_RIGHTS.SHIPMENTS_EDIT
],
params: {
sort: 'createdDate,desc'
},
areAllRightsRequired: false,
resolve: {
supervisedFacilities: function(facilityFactory) {
Expand Down
6 changes: 2 additions & 4 deletions src/order-fulfillment/order-fulfillment.routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ describe('openlmis.orders.fulfillment state', function() {
programId: undefined,
status: [ORDER_STATUS.FULFILLING, ORDER_STATUS.ORDERED],
page: '0',
size: '10',
sort: 'createdDate,desc'
size: '10'
});
});

Expand All @@ -121,8 +120,7 @@ describe('openlmis.orders.fulfillment state', function() {
programId: undefined,
status: [ORDER_STATUS.ORDERED],
page: '0',
size: '10',
sort: 'createdDate,desc'
size: '10'
});
});

Expand Down
3 changes: 0 additions & 3 deletions src/order-view/order-view.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@
orders: function(paginationService, orderRepository, $stateParams) {
return paginationService.registerUrl($stateParams, function(stateParams) {
if (stateParams.supplyingFacilityId) {
if (stateParams.sort === undefined) {
stateParams.sort = 'createdDate,desc';
}
return orderRepository.search(stateParams);
}
return undefined;
Expand Down
3 changes: 0 additions & 3 deletions src/requisition-approval/requisition-approval.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
isOffline: true,
label: 'requisitionApproval.approve',
url: '/approvalList?page&size&program&offline&sort',
params: {
sort: ['emergency,desc', 'authorizedDate,desc']
},
controller: 'RequisitionApprovalListController',
controllerAs: 'vm',
templateUrl: 'requisition-approval/requisition-approval-list.html',
Expand Down
166 changes: 166 additions & 0 deletions src/requisition-approval/requisition-approval.routes.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* This program is part of the OpenLMIS logistics management information system platform software.
* Copyright © 2017 VillageReach
*
* This program is free software: you can redistribute it and/or modify it under the terms
* of the GNU Affero General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*  
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
* See the GNU Affero General Public License for more details. You should have received a copy of
* the GNU Affero General Public License along with this program. If not, see
* http://www.gnu.org/licenses.  For additional information contact [email protected]
*/

describe('openlmis.requisitions.approvalList', function() {

beforeEach(function() {
module('requisition-approval');
module('requisition-view-tab');

inject(function($injector) {
this.$state = $injector.get('$state');
this.$q = $injector.get('$q');
this.$rootScope = $injector.get('$rootScope');
this.$location = $injector.get('$location');

this.REQUISITION_RIGHTS = $injector.get('REQUISITION_RIGHTS');
this.BATCH_APPROVE_SCREEN_FEATURE_FLAG = $injector.get('BATCH_APPROVE_SCREEN_FEATURE_FLAG');
this.REQUISITION_STATUS = $injector.get('REQUISITION_STATUS');

this.requisitionService = $injector.get('requisitionService');
this.authorizationService = $injector.get('authorizationService');
this.alertService = $injector.get('alertService');
this.featureFlagService = $injector.get('featureFlagService');
this.requisitionApprovalService = $injector.get('requisitionApprovalService');
this.permissionService = $injector.get('permissionService');

this.UserDataBuilder = $injector.get('UserDataBuilder');
this.ProgramDataBuilder = $injector.get('ProgramDataBuilder');
this.RequisitionDataBuilder = $injector.get('RequisitionDataBuilder');
this.PageDataBuilder = $injector.get('PageDataBuilder');
});

this.goToUrl = goToUrl;
this.getResolvedValue = getResolvedValue;

this.requisitions = [
new this.RequisitionDataBuilder().build(),
new this.RequisitionDataBuilder().build()
];

this.cachedRequisitions = [
new this.RequisitionDataBuilder().build(),
new this.RequisitionDataBuilder().build()
];

this.programs = [
new this.ProgramDataBuilder().build(),
new this.ProgramDataBuilder().build()
];

this.user = new this.UserDataBuilder().build();
//eslint-disable-next-line camelcase
this.user.user_id = this.user.id;

this.requisitionsPage = new this.PageDataBuilder()
.withContent(this.requisitions)
.build();

this.cachedRequisitionsPage = new this.PageDataBuilder()
.withContent(this.cachedRequisitions)
.build();

spyOn(this.requisitionService, 'forApproval').andReturn(this.$q.resolve(this.requisitionsPage));
spyOn(this.requisitionService, 'search').andReturn(this.$q.resolve(this.cachedRequisitionsPage));
spyOn(this.authorizationService, 'getUser').andReturn(this.$q.resolve(this.user));
spyOn(this.featureFlagService, 'get').andReturn(true);
spyOn(this.alertService, 'error');
spyOn(this.requisitionApprovalService, 'getPrograms').andReturn(this.$q.resolve(this.programs));
spyOn(this.permissionService, 'hasRoleWithRightAndFacility').andReturn(this.$q.resolve(true));
});

it('should resolve isBatchApproveScreenActive', function() {
this.goToUrl('/requisitions/approvalList');

expect(this.getResolvedValue('isBatchApproveScreenActive')).toEqual(true);
expect(this.featureFlagService.get).toHaveBeenCalledWith(this.BATCH_APPROVE_SCREEN_FEATURE_FLAG);
});

describe('programs', function() {

it('should resolve programs', function() {
this.goToUrl('requisitions/approvalList');

expect(this.getResolvedValue('programs')).toEqual(this.programs);
expect(this.requisitionApprovalService.getPrograms).toHaveBeenCalled();
});

});

it('should resolve selected program', function() {
this.goToUrl('requisitions/approvalList?program=' + this.programs[0].id);

expect(this.getResolvedValue('selectedProgram')).toEqual(this.programs[0]);
});

describe('requisitions', function() {

//This seems to be a bug breaking this functionality in offline...
it('should fetch requisitions online if no program filter is specified', function() {
this.goToUrl('requisitions/approvalList');

expect(this.getResolvedValue('requisitions')).toEqual(this.requisitions);
expect(this.requisitionService.search).not.toHaveBeenCalled();
expect(this.requisitionService.forApproval).toHaveBeenCalledWith({
page: 0,
size: 10,
program: undefined,
offline: undefined
});
});

it('should fetch requisitions from cache if when offline', function() {
this.goToUrl('requisitions/approvalList?offline=true&program=' + this.programs[0].id);

expect(this.getResolvedValue('requisitions')).toEqual(this.cachedRequisitions);
expect(this.requisitionService.forApproval).not.toHaveBeenCalledWith();
expect(this.requisitionService.search).toHaveBeenCalledWith(true, {
page: 0,
size: 10,
program: this.programs[0].id,
offline: 'true',
requisitionStatus: [
this.REQUISITION_STATUS.AUTHORIZED,
this.REQUISITION_STATUS.IN_APPROVAL
],
showBatchRequisitions: true
});
});

it('should fetch requisitions when offline and program filter is specified', function() {
this.goToUrl('requisitions/approvalList?program=' + this.programs[0].id);

expect(this.getResolvedValue('requisitions')).toEqual(this.requisitions);
expect(this.requisitionService.search).not.toHaveBeenCalled();
expect(this.requisitionService.forApproval).toHaveBeenCalledWith({
page: 0,
size: 10,
program: this.programs[0].id,
offline: undefined
});
});

});

function goToUrl(url) {
this.$location.url(url);
this.$rootScope.$apply();
}

function getResolvedValue(name) {
return this.$state.$current.locals.globals[name];
}

});

0 comments on commit e6b5f65

Please sign in to comment.