-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from folio-org/UIOR-218-ui-validation-is-asyn…
…c-with-backend UIOR-218 UI validation is unsynced with back-end for location quantities
- Loading branch information
Showing
6 changed files
with
146 additions
and
37 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
src/common/RepeatableFieldWithErrorMessage/RepeatableFieldWithErrorMessage.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@import '@folio/stripes-components/lib/variables'; | ||
|
||
.feedbackError { | ||
color: var(--error); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/common/RepeatableFieldWithErrorMessage/RepeatableFieldWithErrorMessage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import { get } from 'lodash'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { RepeatableField } from '@folio/stripes-components'; | ||
import css from './RepeatableFieldWithErrorMessage.css'; | ||
|
||
export const RepeatableFieldWithErrorMessage = (props) => { // eslint-disable-line import/prefer-default-export | ||
const error = get(props, 'meta.error'); | ||
|
||
return ( | ||
<React.Fragment> | ||
{error && ( | ||
<div className={css.feedbackError}> | ||
<FormattedMessage id={`ui-orders.location.${error}`} /> | ||
</div> | ||
)} | ||
<RepeatableField {...props} /> | ||
</React.Fragment> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import React from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { get } from 'lodash'; | ||
import { | ||
ERESOURCES, | ||
INVENTORY_RECORDS_TYPE, | ||
OTHER, | ||
PHRESOURCES, | ||
} from '../const'; | ||
|
||
export const REQUIRED_LOCATION_QUANTITY_MAP_TO_INVENTORY = { | ||
[INVENTORY_RECORDS_TYPE.all]: true, | ||
[INVENTORY_RECORDS_TYPE.instance]: false, | ||
[INVENTORY_RECORDS_TYPE.instanceAndHolding]: true, | ||
[INVENTORY_RECORDS_TYPE.none]: false, | ||
}; | ||
|
||
export const isLocationPhysicalQuantityRequired = (orderFormat, inventory) => { | ||
return [...PHRESOURCES, OTHER].includes(orderFormat) | ||
? REQUIRED_LOCATION_QUANTITY_MAP_TO_INVENTORY[inventory] | ||
: false; | ||
}; | ||
|
||
export const isLocationEresourceQuantityRequired = (orderFormat, inventory) => { | ||
return ERESOURCES.includes(orderFormat) | ||
? REQUIRED_LOCATION_QUANTITY_MAP_TO_INVENTORY[inventory] | ||
: false; | ||
}; | ||
|
||
const LOCATION_MUST_BE_SPECIFIED = 'required'; | ||
|
||
const getTotalLocationsQuantities = (locations, propName) => { | ||
const reducer = (accumulator, d) => accumulator + (d[propName] || 0); | ||
|
||
return locations.reduce(reducer, 0); | ||
}; | ||
|
||
export const validateNotNegative = value => { | ||
return value < 0 | ||
? <FormattedMessage id="ui-orders.validation.quantity.canNotBeLess0" /> | ||
: undefined; | ||
}; | ||
|
||
export const validateQuantityPhysical = (value, { cost, locations = [], physical, orderFormat }) => { | ||
const allLocationsQuantity = getTotalLocationsQuantities(locations, 'quantityPhysical'); | ||
const overallLineQuantity = Number(get(cost, 'quantityPhysical', 0)); | ||
const inventoryType = get(physical, 'createInventory', ''); | ||
const isQuantityRequired = isLocationPhysicalQuantityRequired(orderFormat, inventoryType); | ||
|
||
if (isQuantityRequired) { | ||
return allLocationsQuantity !== overallLineQuantity | ||
? <FormattedMessage id="ui-orders.location.quantityPhysical.notMatch" /> | ||
: undefined; | ||
} else { | ||
return value && value !== 0 && allLocationsQuantity !== overallLineQuantity | ||
? <FormattedMessage id="ui-orders.location.quantityPhysical.notMatchOrEmpty" /> | ||
: undefined; | ||
} | ||
}; | ||
|
||
export const validateQuantityElectronic = (value, { cost, locations = [], eresource, orderFormat }) => { | ||
const allLocationsQuantity = getTotalLocationsQuantities(locations, 'quantityElectronic'); | ||
const overallLineQuantity = Number(get(cost, 'quantityElectronic', 0)); | ||
const inventoryType = get(eresource, 'createInventory', ''); | ||
const isQuantityRequired = isLocationEresourceQuantityRequired(orderFormat, inventoryType); | ||
|
||
if (isQuantityRequired) { | ||
return allLocationsQuantity !== overallLineQuantity | ||
? <FormattedMessage id="ui-orders.location.quantityElectronic.notMatch" /> | ||
: undefined; | ||
} else { | ||
return value && value !== 0 && allLocationsQuantity !== overallLineQuantity | ||
? <FormattedMessage id="ui-orders.location.quantityElectronic.notMatchOrEmpty" /> | ||
: undefined; | ||
} | ||
}; | ||
|
||
export const parseQuantity = (value) => { | ||
return value | ||
? Number(value) | ||
: 0; | ||
}; | ||
|
||
export const isLocationsRequired = (values, valuesAll) => { | ||
const orderFormat = get(valuesAll, 'orderFormat'); | ||
const physicalInventory = get(valuesAll, 'physical.createInventory', ''); | ||
const eresourceInventory = get(valuesAll, 'eresource.createInventory', ''); | ||
const isPhysicalQuantityRequired = isLocationPhysicalQuantityRequired(orderFormat, physicalInventory); | ||
const isElectronicQuantityRequired = isLocationEresourceQuantityRequired(orderFormat, eresourceInventory); | ||
const isLocationRequired = (isPhysicalQuantityRequired || isElectronicQuantityRequired) && !values.length; | ||
|
||
return isLocationRequired | ||
? LOCATION_MUST_BE_SPECIFIED | ||
: undefined; | ||
}; | ||
|
||
export const validateLocation = (value, { locations }) => { | ||
const { quantityPhysical, quantityElectronic } = locations.find(({ locationId }) => locationId === value); | ||
const isQuantitiesNeed = | ||
(!quantityPhysical || quantityPhysical === 0) && (!quantityElectronic || quantityElectronic === 0); | ||
|
||
return isQuantitiesNeed | ||
? <FormattedMessage id="ui-orders.location.emptyQuantities" /> | ||
: undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters