Index: src/stock-adjustment-mobile/_adjustment-mobile.scss =================================================================== diff -u -N -r9b23add288958da43e8a4f8be30e48edda073463 -rf1d2995a4d578dc260625f857af59829041afa39 --- src/stock-adjustment-mobile/_adjustment-mobile.scss (.../_adjustment-mobile.scss) (revision 9b23add288958da43e8a4f8be30e48edda073463) +++ src/stock-adjustment-mobile/_adjustment-mobile.scss (.../_adjustment-mobile.scss) (revision f1d2995a4d578dc260625f857af59829041afa39) @@ -135,6 +135,25 @@ } + .form-field-with-button { + display: flex; + flex-direction: column; + width: 80%; + padding: 0.5em 0; + + input { + height: 50px; + width: 80%; + } + + select { + height: 50px; + width: 80%; + vertical-align: middle; + } + + } + .field-full-width-last { margin-bottom: 16px; } Index: src/stock-adjustment-mobile/add-products-page/add-product-page.jsx =================================================================== diff -u -N -r9341df0abcea429edf15dfdad5eed1196a85b2b6 -rf1d2995a4d578dc260625f857af59829041afa39 --- src/stock-adjustment-mobile/add-products-page/add-product-page.jsx (.../add-product-page.jsx) (revision 9341df0abcea429edf15dfdad5eed1196a85b2b6) +++ src/stock-adjustment-mobile/add-products-page/add-product-page.jsx (.../add-product-page.jsx) (revision f1d2995a4d578dc260625f857af59829041afa39) @@ -13,28 +13,30 @@ * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org.  */ -import React, { useMemo, useState, useEffect } from 'react'; -import { useHistory } from 'react-router-dom'; +import React, { useMemo, useEffect } from 'react'; +import { useHistory, useLocation } from 'react-router-dom'; import { useDispatch, useSelector } from 'react-redux'; import { Form } from 'react-final-form'; import arrayMutators from 'final-form-arrays'; import { FieldArray } from 'react-final-form-arrays'; import createDecorator from 'final-form-calculate'; import update from 'immutability-helper'; +import InlineField from '../../react-components/form-fields/inline-field'; import InputField from '../../react-components/form-fields/input-field'; import SelectField from '../../react-components/form-fields/select-field'; import ReadOnlyField from '../../react-components/form-fields/read-only-field'; import BaseField from '../../react-components/form-fields/base-field'; import DateInput from '../components/date-input.component'; import Input from '../../react-components/inputs/input'; -import { formatLot, formatDate, formatDateISO, isQuantityNotFilled, maxDateToday, removeProperty } from '../format-utils'; +import { formatLot, formatDateISO, isQuantityNotFilled, maxDateToday, removeProperty } from '../format-utils'; import AddButton from '../../react-components/buttons/add-button'; import { CREDIT, ISSUE, RECEIVE } from '../consts'; const AddProductsPage = ({ adjustmentType, appendToAdjustment }) => { const history = useHistory(); + const location = useLocation(); const dispatch = useDispatch(); const userHomeFacility = useSelector(state => state[`facilities${adjustmentType}`][`userHomeFacility${adjustmentType}`]); @@ -47,6 +49,10 @@ const menu = document.getElementsByClassName("header ng-scope")[0]; menu.style.display = "none"; + useEffect(() => { + location.state = location?.state ?? null; + }, [location]); + const decorator = useMemo(() => createDecorator({ field: /product/, updates: { @@ -58,7 +64,9 @@ items: (productVal, itemsVal) => { let newItemsVal = itemsVal; if (newItemsVal.items[0].hasOwnProperty('lot')) { - return update(newItemsVal.items, {0: { $unset: ['lot'] } }); + if ((newItemsVal.items[0].lot?.isFromAddLotCodePage ?? false) === false) { + return update(newItemsVal.items, {0: { $unset: ['lot'] } }); + } } return newItemsVal.items; } @@ -128,7 +136,7 @@ const cancel = () => { if (!adjustment.length) { - history.goBack(); + history.push(`/make${adjustmentType}AddProducts/submit${adjustmentType}/programChoice`); } else { history.push(`/make${adjustmentType}AddProducts/submit${adjustmentType}`); @@ -168,11 +176,13 @@ const onSubmit = (values) => { updateAdjustmentList(values); + location.state = null; history.push(`/make${adjustmentType}AddProducts/submit${adjustmentType}`); }; const onSubmitAddProduct = (values) => { updateAdjustmentList(values); + location.state = null; history.push(`/make${adjustmentType}AddProducts`); }; @@ -185,20 +195,57 @@ return _.map(lots, lot => ({ name: formatLot(lot), value: lot })); }; + const goToAddLotCodePage = (lotCodeOptions, values) => { + const stateLocation = { + lotCodeOptions: lotCodeOptions, + currentFormValues: values + }; + localStorage.setItem('stateLocationLotCode', JSON.stringify(stateLocation)); + history.push({ + pathname: `/makeReceiveAddProducts/addLotCode`, + state: stateLocation + }); + }; + const renderLotSelect = (fieldName, product, v) => { const options = getLotsOptions(product); const noOptions = !options?.length; - return ( - - ); + const noProduct = !product ? true : false; + if (adjustmentType === RECEIVE) { + return ( + + + {goToAddLotCodePage(options, v)}} + className="secondary" + disabled={noProduct} + style={{height: "50px"}} + > + + ); + } + else { + return ( + + ); + } }; const renderIssueSelectField = (fieldName, product, v) => { @@ -246,7 +293,7 @@ return (
{ + const history = useHistory(); + const location = useLocation(); + + const menu = document.getElementsByClassName("header ng-scope")[0]; + menu.style.display = "none"; + + useEffect(() => { + location.state = location?.state ?? JSON.parse(localStorage.getItem('stateLocationLotCode')); + }, [location]); + + const validate = values => { + const errors = {}; + + if (!values.lotCode) { + errors['lotCode'] = { lotCode: 'Required' }; + } + + if (!values.expirationDate) { + errors['expirationDate'] = { expirationDate: 'Required' }; + } + + return errors; + }; + + const cancel = () => { + const currentFormValues = location.state.currentFormValues.hasOwnProperty('lot') ? update(location.state.currentFormValues, {lot: {['isFromAddLotCodePage']: {$set: true}}}) : location.state.currentFormValues; + const stateLocation = { + currentFormValues: currentFormValues + }; + localStorage.setItem('stateLocationAddProduct', JSON.stringify(stateLocation)); + history.push({ + pathname: `/makeReceiveAddProducts`, + state: stateLocation + }); + }; + + const onSubmit = (valuesOfNewLot) => { + const orderable = location.state.currentFormValues.product; + const selectedItem = orderableGroupService.findByLotInOrderableGroup(orderable, valuesOfNewLot, true); + const selectedItemWithId = update(selectedItem, { lot: { ['isNewItem']: {$set: true}, ['tradeItemId']: {$set: orderable[0].orderable.identifiers.tradeItem}}}) + addLotCode(selectedItemWithId); + }; + + const addLotCode = (lineItem) => { + lotResource.create(lineItem.lot).then(createResponse => { + const updatedLineItem = update(lineItem, {lot: {['id']: {$set: createResponse.id}, ['isFromAddLotCodePage']: {$set: true}}}); + checkResponse(updatedLineItem, createResponse); + }).catch(function(response) { + checkResponse(lineItem, response); + }); + }; + + const checkResponse = (lineItem, response) => { + const isBadResponse = response?.data?.message ?? false; + if (isBadResponse !== false) { + const lineItemToReturn = update(location.state.currentFormValues, { $unset: ['lot'] }); + goToAddProductPage(lineItemToReturn); + } else { + const lineItemToReturn = update(location.state.currentFormValues, { product: { $push : [lineItem] }, ['lot']: { $set: lineItem.lot}}); + goToAddProductPage(lineItemToReturn); + } + }; + + const goToAddProductPage = (lineItem) => { + const stateLocation = { + currentFormValues: lineItem + }; + localStorage.setItem('stateLocationAddProduct', JSON.stringify(stateLocation)); + history.push({ + pathname: `/makeReceiveAddProducts`, + state: stateLocation + }); + } + + return ( +
+ ( + + + {({ fields }) => ( +
+
+
+

Add Lot Code

+
+