Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/requisition-order-create/order-create-summary-modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import getService from '../react-components/utils/angular-utils';
const OrderCreateSummaryModal = ({ isOpen, orders, onSaveClick, onModalClose }) => {
const { formatMessage } = useMemo(() => getService('messageService'), []);
const quantityUnitCalculateService = useMemo(() => getService('quantityUnitCalculateService'), []);
const columns = useMemo(() => orderTableColumns(true, formatMessage, true, null,
quantityUnitCalculateService), []);
const quantityUnitConfigService = useMemo(() => getService('quantityUnitConfigService'), []);
const QUANTITY_UNIT = useMemo(() => getService('QUANTITY_UNIT'), []);
const showInDoses = useMemo(
() => quantityUnitConfigService.getEffectiveUnit() === QUANTITY_UNIT.DOSES,
[quantityUnitConfigService, QUANTITY_UNIT.DOSES]
);
const columns = useMemo(() => orderTableColumns(true, formatMessage, showInDoses, null,
quantityUnitCalculateService), [formatMessage, showInDoses, quantityUnitCalculateService]);
const [currentTab, setCurrentTab] = useState(0);

return (
Expand Down
57 changes: 14 additions & 43 deletions src/requisition-order-create/order-create-tab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import { validateOrderItem } from './order-create-validation-helper-functions';
import { orderTableColumns } from './order-create.constant';
import { SearchSelect } from './search-select';

const QUANTITY_UNIT_KEY = 'quantityUnit';

const OrderCreateTab = ({
passedOrder,
updateOrderArray,
Expand All @@ -23,10 +21,6 @@ const OrderCreateTab = ({
cacheOrderableOptions,
cachedOrderableOptions,
}) => {
const localStorageService = useMemo(
() => getService('localStorageService'),
[]
);
const { formatMessage } = useMemo(() => getService('messageService'), []);
const orderCreatePrintService = useMemo(
() => getService('orderCreatePrintService'),
Expand All @@ -37,18 +31,15 @@ const OrderCreateTab = ({
[]
);
const QUANTITY_UNIT = useMemo(() => getService('QUANTITY_UNIT'), []);
const featureFlagService = useMemo(
() => getService('featureFlagService'),
[]
);
const DEFAULT_QUANTITY_UNIT_FEATURE_FLAG = useMemo(
() => getService('DEFAULT_QUANTITY_UNIT_FEATURE_FLAG'),
const quantityUnitConfigService = useMemo(
() => getService('quantityUnitConfigService'),
[]
);
const quantityUnitCalculateService = useMemo(
() => getService('quantityUnitCalculateService'),
[]
);
const $rootScope = useMemo(() => getService('$rootScope'), []);

const [order, setOrder] = useState({ orderLineItems: [], ...passedOrder });
const [selectedOrderable, setSelectedOrderable] = useState('');
Expand All @@ -63,41 +54,20 @@ const OrderCreateTab = ({
);

useEffect(() => {
const getDefaultUnit = () => {
if (featureFlagService && DEFAULT_QUANTITY_UNIT_FEATURE_FLAG) {
const flagValue = featureFlagService.get(
DEFAULT_QUANTITY_UNIT_FEATURE_FLAG
);
if (
flagValue === QUANTITY_UNIT.PACKS ||
flagValue === QUANTITY_UNIT.DOSES
) {
return flagValue;
}
}

return QUANTITY_UNIT.DOSES;
};

const cachedUnit = localStorageService.get(QUANTITY_UNIT_KEY);
const initialUnit = cachedUnit || getDefaultUnit();

if (!cachedUnit) {
localStorageService.add(QUANTITY_UNIT_KEY, initialUnit);
}
setCurrentQuantityUnit(initialUnit);
}, [
localStorageService,
QUANTITY_UNIT,
featureFlagService,
DEFAULT_QUANTITY_UNIT_FEATURE_FLAG,
]);
setCurrentQuantityUnit(quantityUnitConfigService.getEffectiveUnit());
}, [quantityUnitConfigService]);

const handleQuantityUnitChange = useCallback(
(newUnit) => {
localStorageService.add(QUANTITY_UNIT_KEY, newUnit);
quantityUnitConfigService.setSelectedUnit(newUnit);
setCurrentQuantityUnit(newUnit);

// Persist the user's choice server-side so it survives logout, mirroring the AngularJS
// toggle. Emitted inside a digest so the post-login-action listener's async save runs.
$rootScope.$applyAsync(function() {
$rootScope.$emit('openlmis.quantityUnit.selected', newUnit);
});

// Recalculate all existing order items for the new unit
const recalculatedOrderItems = order.orderLineItems.map((item) => {
return quantityUnitCalculateService.recalculateInputQuantity(
Expand All @@ -114,11 +84,12 @@ const OrderCreateTab = ({
updateOrderArray(updatedOrder);
},
[
localStorageService,
quantityUnitConfigService,
order,
quantityUnitCalculateService,
showInDoses,
updateOrderArray,
$rootScope,
]
);

Expand Down
14 changes: 5 additions & 9 deletions src/requisition/requisition.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@
requisitionFactory.$inject = [
'$q', '$resource', 'requisitionUrlFactory', 'RequisitionTemplate', 'LineItem', 'REQUISITION_STATUS',
'COLUMN_SOURCES', 'localStorageFactory', 'dateUtils', '$filter', 'TEMPLATE_COLUMNS', 'authorizationService',
'REQUISITION_RIGHTS', 'UuidGenerator', 'requisitionCacheService', 'localStorageService', 'QUANTITY_UNIT',
'quantityUnitCalculateService'
'REQUISITION_RIGHTS', 'UuidGenerator', 'requisitionCacheService', 'QUANTITY_UNIT',
'quantityUnitCalculateService', 'quantityUnitConfigService'
];

function requisitionFactory($q, $resource, requisitionUrlFactory, RequisitionTemplate, LineItem, REQUISITION_STATUS,
COLUMN_SOURCES, localStorageFactory, dateUtils, $filter, TEMPLATE_COLUMNS,
authorizationService, REQUISITION_RIGHTS, UuidGenerator, requisitionCacheService,
localStorageService, QUANTITY_UNIT, quantityUnitCalculateService) {
QUANTITY_UNIT, quantityUnitCalculateService,
quantityUnitConfigService) {

var offlineRequisitions = localStorageFactory('requisitions'),
resource = $resource(requisitionUrlFactory('/api/v2/requisitions/:id'), {}, {
Expand Down Expand Up @@ -161,12 +162,7 @@
}

this.showInDoses = showInDoses;
var cachedQuantityUnit = localStorageService.get('quantityUnit');
if (cachedQuantityUnit === null) {
this.quantityUnit = QUANTITY_UNIT.$getDefaultQuantityUnit();
} else {
this.quantityUnit = cachedQuantityUnit;
}
this.quantityUnit = quantityUnitConfigService.getEffectiveUnit();
}

/**
Expand Down
22 changes: 22 additions & 0 deletions src/requisition/requisition.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
this.requisitionCacheService = $injector.get('requisitionCacheService');
this.ProgramOrderableDataBuilder = $injector.get('ProgramOrderableDataBuilder');
this.ProgramDataBuilder = $injector.get('ProgramDataBuilder');
this.QUANTITY_UNIT = $injector.get('QUANTITY_UNIT');
this.quantityUnitConfigService = $injector.get('quantityUnitConfigService');
});

this.program = new this.ProgramDataBuilder().build();
Expand Down Expand Up @@ -1326,4 +1328,24 @@
};
}

describe('quantityUnit initialization', function() {

it('should set quantityUnit to the effective unit (PACKS) resolved by the config service', function() {
spyOn(this.quantityUnitConfigService, 'getEffectiveUnit').andReturn(this.QUANTITY_UNIT.PACKS);

var requisition = new this.Requisition(this.sourceRequisition);

Check failure on line 1336 in src/requisition/requisition.spec.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected var, use let or const instead.

See more on https://sonarcloud.io/project/issues?id=OpenLMIS_openlmis-requisition-ui&issues=AZ7NithaT0cOunanRNBb&open=AZ7NithaT0cOunanRNBb&pullRequest=82

expect(requisition.quantityUnit).toEqual(this.QUANTITY_UNIT.PACKS);
});

it('should set quantityUnit to the effective unit (DOSES) resolved by the config service', function() {
spyOn(this.quantityUnitConfigService, 'getEffectiveUnit').andReturn(this.QUANTITY_UNIT.DOSES);

var requisition = new this.Requisition(this.sourceRequisition);

Check failure on line 1344 in src/requisition/requisition.spec.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unexpected var, use let or const instead.

See more on https://sonarcloud.io/project/issues?id=OpenLMIS_openlmis-requisition-ui&issues=AZ7NithaT0cOunanRNBc&open=AZ7NithaT0cOunanRNBc&pullRequest=82

expect(requisition.quantityUnit).toEqual(this.QUANTITY_UNIT.DOSES);
});

});

});
Loading