@@ -10,30 +10,62 @@ function UpdateConsumable({ record, setModal }) {
1010 const dispatch = useDispatch ( ) ;
1111 const postConsumableUpdateResult = useSelector ( state => state . bmConsumables . updateConsumables ) ;
1212 const { purchaseRecord, stockAvailable, updateRecord : _ , ...rest } = record ;
13-
1413 const recordInitialState = {
1514 date : moment ( new Date ( ) ) . format ( 'YYYY-MM-DD' ) ,
1615 quantityUsed : '0' ,
1716 quantityWasted : '0' ,
1817 qtyUsedLogUnit : 'unit' ,
1918 qtyWastedLogUnit : 'unit' ,
20- reasonWastage : '' ,
21- usedBy : '' ,
2219 consumable : rest ,
2320 newAvailable : undefined ,
2421 } ;
25-
2622 const validationsInitialState = {
2723 quantityUsed : '' ,
2824 quantityWasted : '' ,
2925 quantityTogether : '' ,
3026 } ;
31-
3227 const [ updateRecord , setUpdateRecord ] = useState ( recordInitialState ) ;
3328 const [ validations , setValidations ] = useState ( validationsInitialState ) ;
3429 const [ availableCount , setAvailableCount ] = useState ( undefined ) ;
3530 const [ changeOccured , setChangeOccured ] = useState ( false ) ;
3631
32+ useEffect ( ( ) => {
33+ setUpdateRecord ( { ...recordInitialState } ) ;
34+ setValidations ( { ...validationsInitialState } ) ;
35+ } , [ ] ) ;
36+
37+ useEffect ( ( ) => {
38+ if ( postConsumableUpdateResult . loading === false && postConsumableUpdateResult . error === true ) {
39+ toast . error ( `${ postConsumableUpdateResult . result } ` ) ;
40+ setModal ( false ) ;
41+ } else if (
42+ postConsumableUpdateResult . loading === false &&
43+ postConsumableUpdateResult . result !== null
44+ ) {
45+ toast . success ( `Updated ${ record ?. itemType ?. name } successfully` ) ;
46+ setModal ( false ) ;
47+ }
48+ } , [ postConsumableUpdateResult ] ) ;
49+
50+ useEffect ( ( ) => {
51+ const qtyUsedFloat = parseFloat ( updateRecord . quantityUsed ) ;
52+ const qtyWastedFloat = parseFloat ( updateRecord . quantityWasted ) ;
53+
54+ if ( qtyUsedFloat || qtyWastedFloat ) {
55+ setChangeOccured ( true ) ;
56+ } else {
57+ setChangeOccured ( false ) ;
58+ }
59+
60+ // eslint-disable-next-line no-use-before-define
61+ validate (
62+ updateRecord . quantityUsed ,
63+ updateRecord . quantityWasted ,
64+ updateRecord . qtyUsedLogUnit ,
65+ updateRecord . qtyWastedLogUnit ,
66+ ) ;
67+ } , [ updateRecord ] ) ;
68+
3769 const validate = ( _qtyUsed , _qtyWasted , qtyUsedLogUnit , qtyWastedLogUnit ) => {
3870 let unitsUsed = _qtyUsed === '' ? 0 : parseFloat ( _qtyUsed ) ;
3971 let unitsWasted = _qtyWasted === '' ? 0 : parseFloat ( _qtyWasted ) ;
@@ -50,16 +82,12 @@ function UpdateConsumable({ record, setModal }) {
5082
5183 if ( unitsUsed > stockAvailable ) {
5284 tempValidations . quantityUsed = 'Quantity Used exceeds the available stock' ;
53- } else if ( unitsUsed < 0 ) {
54- tempValidations . quantityUsed = 'Quantity Used cannot be negative' ;
5585 } else {
5686 tempValidations . quantityUsed = '' ;
5787 }
5888
5989 if ( unitsWasted > stockAvailable ) {
6090 tempValidations . quantityWasted = 'Quantity Wasted exceeds the available stock' ;
61- } else if ( unitsWasted < 0 ) {
62- tempValidations . quantityWasted = 'Quantity Wasted cannot be negative' ;
6391 } else {
6492 tempValidations . quantityWasted = '' ;
6593 }
@@ -80,42 +108,6 @@ function UpdateConsumable({ record, setModal }) {
80108 }
81109 } ;
82110
83- useEffect ( ( ) => {
84- setUpdateRecord ( { ...recordInitialState } ) ;
85- setValidations ( { ...validationsInitialState } ) ;
86- } , [ ] ) ;
87-
88- useEffect ( ( ) => {
89- if ( postConsumableUpdateResult . loading === false && postConsumableUpdateResult . error === true ) {
90- toast . error ( `${ postConsumableUpdateResult . result } ` ) ;
91- setModal ( false ) ;
92- } else if (
93- postConsumableUpdateResult . loading === false &&
94- postConsumableUpdateResult . result !== null
95- ) {
96- toast . success ( `Updated ${ record ?. itemType ?. name } successfully` ) ;
97- setModal ( false ) ;
98- }
99- } , [ postConsumableUpdateResult ] ) ;
100-
101- useEffect ( ( ) => {
102- const qtyUsedFloat = parseFloat ( updateRecord . quantityUsed ) ;
103- const qtyWastedFloat = parseFloat ( updateRecord . quantityWasted ) ;
104-
105- if ( qtyUsedFloat || qtyWastedFloat ) {
106- setChangeOccured ( true ) ;
107- } else {
108- setChangeOccured ( false ) ;
109- }
110-
111- validate (
112- updateRecord . quantityUsed ,
113- updateRecord . quantityWasted ,
114- updateRecord . qtyUsedLogUnit ,
115- updateRecord . qtyWastedLogUnit ,
116- ) ;
117- } , [ updateRecord ] ) ;
118-
119111 const submitHandler = ( ) => {
120112 if (
121113 validations . quantityUsed === '' &&
@@ -130,8 +122,6 @@ function UpdateConsumable({ record, setModal }) {
130122 quantityWasted :
131123 updateRecord . quantityWasted === '' ? 0 : parseFloat ( updateRecord . quantityWasted ) ,
132124 qtyWastedLogUnit : updateRecord . qtyWastedLogUnit ,
133- reasonWastage : updateRecord . reasonWastage ,
134- usedBy : updateRecord . usedBy ,
135125 stockAvailable,
136126 consumable : updateRecord . consumable ,
137127 } ;
@@ -146,6 +136,7 @@ function UpdateConsumable({ record, setModal }) {
146136 if ( Number ( value ) < 0 ) return ;
147137 const tempRecord = { ...updateRecord } ;
148138 tempRecord [ name ] = value ;
139+
149140 setUpdateRecord ( tempRecord ) ;
150141 } ;
151142
@@ -175,7 +166,6 @@ function UpdateConsumable({ record, setModal }) {
175166 { record ?. project . name }
176167 </ Col >
177168 </ FormGroup >
178-
179169 < FormGroup row className = "align-items-center justify-content-start" >
180170 < Label for = "updateConsumableDate" sm = { 4 } className = { `${ styles . consumableFormLabel } ` } >
181171 Date
@@ -212,7 +202,6 @@ function UpdateConsumable({ record, setModal }) {
212202 </ Col >
213203 </ FormGroup >
214204 ) }
215-
216205 < FormGroup row >
217206 < Label
218207 for = "updateConsumableQuantityUsed"
@@ -230,12 +219,6 @@ function UpdateConsumable({ record, setModal }) {
230219 value = { updateRecord . quantityUsed }
231220 onChange = { e => changeRecordHandler ( e ) }
232221 min = { 0 }
233- onKeyDown = { e => {
234- if ( e . key === '+' || e . key === '-' ) e . preventDefault ( ) ;
235- } }
236- onInput = { e => {
237- e . target . value = e . target . value . replace ( / [ ^ \d . ] / g, '' ) ;
238- } }
239222 />
240223 </ Col >
241224 < Col sm = { { size : 4 } } className = { `${ styles . consumableFormValue } ` } >
@@ -279,12 +262,6 @@ function UpdateConsumable({ record, setModal }) {
279262 value = { updateRecord . quantityWasted }
280263 onChange = { e => changeRecordHandler ( e ) }
281264 min = { 0 }
282- onKeyDown = { e => {
283- if ( e . key === '+' || e . key === '-' ) e . preventDefault ( ) ;
284- } }
285- onInput = { e => {
286- e . target . value = e . target . value . replace ( / [ ^ \d . ] / g, '' ) ;
287- } }
288265 />
289266 </ Col >
290267 < Col sm = { { size : 4 } } className = { `${ styles . consumableFormValue } ` } >
@@ -309,42 +286,6 @@ function UpdateConsumable({ record, setModal }) {
309286 </ Label >
310287 ) }
311288 </ FormGroup >
312-
313- < FormGroup row >
314- < Label for = "updateConsumableReasonWastage" sm = { 4 } className = "consumableFormLabel" >
315- Reason for Wastage
316- </ Label >
317- < Col sm = { 8 } className = "consumableFormValue" >
318- < Input
319- id = "updateConsumableReasonWastage"
320- name = "reasonWastage"
321- type = "select"
322- value = { updateRecord . reasonWastage }
323- onChange = { e => changeRecordHandler ( e ) }
324- >
325- < option value = "" > Select Reason</ option >
326- < option value = "expired" > Expired</ option >
327- < option value = "damaged" > Damaged</ option >
328- < option value = "overstock" > Overstock</ option >
329- </ Input >
330- </ Col >
331- </ FormGroup >
332-
333- < FormGroup row >
334- < Label for = "updateConsumableUsedBy" sm = { 4 } className = "consumableFormLabel" >
335- Used By
336- </ Label >
337- < Col sm = { 8 } className = "consumableFormValue" >
338- < Input
339- id = "updateConsumableUsedBy"
340- name = "usedBy"
341- type = "text"
342- value = { updateRecord . usedBy }
343- onChange = { e => changeRecordHandler ( e ) }
344- />
345- </ Col >
346- </ FormGroup >
347-
348289 { validations . quantityTogether !== '' &&
349290 validations . quantityUsed === '' &&
350291 validations . quantityWasted === '' && (
0 commit comments