1111 * limitations under the License.
1212 * */
1313
14- import React , { useCallback , useEffect , useRef , useState } from "react" ;
14+ import React , { useCallback , useEffect , useState } from "react" ;
1515import T from "i18n-react/dist/i18n-react" ;
16- import "awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" ;
17- import Input from "openstack-uicore-foundation/lib/components/inputs/text-input" ;
16+ import TextField from "@mui/material/TextField" ;
17+ import Button from "@mui/material/Button" ;
18+ import Grid2 from "@mui/material/Grid2" ;
19+ import Box from "@mui/material/Box" ;
20+ import Stack from "@mui/material/Stack" ;
1821import SimpleLinkList from "openstack-uicore-foundation/lib/components/simple-link-list" ;
1922import { queryTicketTypes } from "openstack-uicore-foundation/lib/utils/query-actions" ;
20- import { isEmpty , scrollToError , shallowEqual } from "../../utils/methods" ;
21- import { MILLISECONDS_TO_SECONDS } from "../../utils/constants" ;
23+ import { scrollToError , shallowEqual } from "../../utils/methods" ;
2224
2325const TaxTypeForm = ( {
2426 entity : entityProp ,
@@ -30,52 +32,38 @@ const TaxTypeForm = ({
3032} ) => {
3133 const [ entity , setEntity ] = useState ( { ...entityProp } ) ;
3234 const [ errors , setErrors ] = useState ( errorsProp ) ;
33- const prevEntityRef = useRef ( entityProp ) ;
34- const prevErrorsRef = useRef ( errorsProp ) ;
3535
3636 useEffect ( ( ) => {
37- scrollToError ( errorsProp ) ;
37+ setEntity ( { ...entityProp } ) ;
38+ setErrors ( { } ) ;
39+ } , [ entityProp . id ] ) ;
3840
39- const state = { } ;
41+ useEffect ( ( ) => {
42+ const nextTicketTypes = entityProp . ticket_types ?? [ ] ;
4043
41- if ( ! shallowEqual ( prevEntityRef . current , entityProp ) ) {
42- state . entity = { ...entityProp } ;
43- state . errors = { } ;
44- }
44+ setEntity ( ( prev ) => {
45+ if ( prev . id !== entityProp . id ) return prev ;
46+ if ( shallowEqual ( prev . ticket_types ?? [ ] , nextTicketTypes ) ) return prev ;
4547
46- if ( ! shallowEqual ( prevErrorsRef . current , errorsProp ) ) {
47- state . errors = { ...errorsProp } ;
48- }
48+ return {
49+ ...prev ,
50+ ticket_types : [ ...nextTicketTypes ]
51+ } ;
52+ } ) ;
53+ } , [ entityProp . id , entityProp . ticket_types ] ) ;
4954
50- if ( ! isEmpty ( state ) ) {
51- if ( state . entity ) {
52- setEntity ( state . entity ) ;
53- }
54- if ( state . errors ) {
55- setErrors ( state . errors ) ;
56- }
55+ useEffect ( ( ) => {
56+ scrollToError ( errorsProp ) ;
57+ if ( ! shallowEqual ( errors , errorsProp ) ) {
58+ setErrors ( { ...errorsProp } ) ;
5759 }
58-
59- prevEntityRef . current = entityProp ;
60- prevErrorsRef . current = errorsProp ;
61- } , [ entityProp , errorsProp ] ) ;
60+ } , [ errorsProp ] ) ;
6261
6362 const handleChange = useCallback (
6463 ( ev ) => {
65- const newEntity = { ...entity } ;
66- const newErrors = { ...errors } ;
67- let { value, id } = ev . target ;
68-
69- if ( ev . target . type === "checkbox" ) {
70- value = ev . target . checked ;
71- }
72-
73- if ( ev . target . type === "datetime" ) {
74- value = value . valueOf ( ) / MILLISECONDS_TO_SECONDS ;
75- }
76-
77- newErrors [ id ] = "" ;
78- newEntity [ id ] = value ;
64+ const { value, id } = ev . target ;
65+ const newEntity = { ...entity , [ id ] : value } ;
66+ const newErrors = { ...errors , [ id ] : "" } ;
7967 setEntity ( newEntity ) ;
8068 setErrors ( newErrors ) ;
8169 } ,
@@ -137,64 +125,66 @@ const TaxTypeForm = ({
137125 } ;
138126
139127 return (
140- < form className = "tax-type- form">
128+ < Box component = " form">
141129 < input type = "hidden" id = "id" value = { entity . id } />
142- < div className = "row form-group" >
143- < div className = "col-md-4" >
144- < label > { T . translate ( "edit_tax_type.name" ) } * </ label >
145- < Input
130+
131+ < Grid2 container spacing = { 2 } sx = { { mb : 2 } } >
132+ < Grid2 size = { { xs : 12 , sm : 6 , md : 4 } } >
133+ < TextField
146134 id = "name"
147- className = "form-control"
148- error = { hasErrors ( "name" ) }
149- onChange = { handleChange }
135+ label = { T . translate ( "edit_tax_type.name" ) }
150136 value = { entity . name }
137+ onChange = { handleChange }
138+ error = { ! ! hasErrors ( "name" ) }
139+ helperText = { hasErrors ( "name" ) }
140+ fullWidth
141+ required
151142 />
152- </ div >
153- </ div >
154- < div className = "row form-group" >
155- < div className = "col-md-4" >
156- < label > { T . translate ( "edit_tax_type.rate" ) } </ label >
157- < Input
158- className = "form-control"
159- type = "number"
160- error = { hasErrors ( "rate" ) }
143+ </ Grid2 >
144+ </ Grid2 >
145+
146+ < Grid2 container spacing = { 2 } sx = { { mb : 3 } } >
147+ < Grid2 size = { { xs : 12 , sm : 6 , md : 4 } } >
148+ < TextField
161149 id = "rate"
150+ label = { T . translate ( "edit_tax_type.rate" ) }
151+ type = "number"
162152 value = { entity . rate }
163153 onChange = { handleChange }
154+ error = { ! ! hasErrors ( "rate" ) }
155+ helperText = { hasErrors ( "rate" ) }
156+ fullWidth
164157 />
165- </ div >
166- < div className = "col-md-4" >
167- < label > { T . translate ( "edit_tax_type.tax_id" ) } </ label >
168- < Input
169- className = "form-control"
170- error = { hasErrors ( "tax_id" ) }
158+ </ Grid2 >
159+ < Grid2 size = { { xs : 12 , sm : 6 , md : 4 } } >
160+ < TextField
171161 id = "tax_id"
162+ label = { T . translate ( "edit_tax_type.tax_id" ) }
172163 value = { entity . tax_id }
173164 onChange = { handleChange }
165+ error = { ! ! hasErrors ( "tax_id" ) }
166+ helperText = { hasErrors ( "tax_id" ) }
167+ fullWidth
174168 />
175- </ div >
176- </ div >
169+ </ Grid2 >
170+ </ Grid2 >
177171
178- < hr />
179172 { entity . id !== 0 && (
180- < SimpleLinkList
181- values = { entity . ticket_types }
182- columns = { ticketColumns }
183- options = { ticketOptions }
184- />
173+ < Box sx = { { mb : 3 } } >
174+ < SimpleLinkList
175+ values = { entity . ticket_types }
176+ columns = { ticketColumns }
177+ options = { ticketOptions }
178+ />
179+ </ Box >
185180 ) }
186181
187- < div className = "row" >
188- < div className = "col-md-12 submit-buttons" >
189- < input
190- type = "button"
191- onClick = { handleSubmit }
192- className = "btn btn-primary pull-right"
193- value = { T . translate ( "general.save" ) }
194- />
195- </ div >
196- </ div >
197- </ form >
182+ < Stack direction = "row" justifyContent = "flex-end" >
183+ < Button variant = "contained" onClick = { handleSubmit } type = "submit" >
184+ { T . translate ( "general.save" ) }
185+ </ Button >
186+ </ Stack >
187+ </ Box >
198188 ) ;
199189} ;
200190
0 commit comments