@@ -5,27 +5,37 @@ import { Loading } from 'carbon-components-react';
55import createSchema from './user-form.schema' ;
66import miqRedirectBack from '../../helpers/miq-redirect-back' ;
77import { API } from '../../http_api' ;
8+ import { passwordValidation } from './helper' ;
89
9- const UserForm = ( { id, data } ) => {
10- const [ { initialValues, isLoading } , setState ] = useState ( { isLoading : true } ) ;
11- const [ editMode , setEditMode ] = useState ( false ) ;
12- const [ groups , setGroups ] = useState ( undefined ) ;
10+ const UserForm = ( { id, data, disabled } ) => {
11+ const [ {
12+ initialValues, isLoading, editMode, groups,
13+ } , setState ] = useState ( { isLoading : true } ) ;
14+ // const [editMode, setEditMode] = useState(false);
15+ // const [groups, setGroups] = useState(undefined);
1316
1417 useEffect ( ( ) => {
18+ console . log ( disabled ) ;
1519 if ( id ) {
20+ Promise . all ( [ API . get ( '/api/groups?&expand=resources' ) ] ) . then ( ( apiResult ) => {
21+ console . log ( apiResult ) ;
22+ } ) ;
1623 API . get ( '/api/groups?&expand=resources' ) . then ( ( { resources } ) => {
1724 const temp = [ ] ;
1825 resources . forEach ( ( group ) => {
1926 temp . push ( { label : group . description , value : group . id } ) ;
2027 } ) ;
21- setGroups ( temp ) ;
22- } ) . then ( ( ) => {
28+ // setGroups(temp);
29+ return temp ;
30+ } ) . then ( ( groups ) => {
2331 API . get ( `/api/users/${ id } ` ) . then ( ( values ) => {
2432 values . groups = [ values . current_group_id ] ;
2533 console . log ( values ) ;
2634 setState ( {
2735 initialValues : values ,
2836 isLoading : false ,
37+ editMode : false ,
38+ groups,
2939 } ) ;
3040 } ) ;
3141 } ) ;
@@ -35,7 +45,7 @@ const UserForm = ({ id, data }) => {
3545 resources . forEach ( ( group ) => {
3646 temp . push ( { label : group . description , value : group . id } ) ;
3747 } ) ;
38- setGroups ( temp ) ;
48+ // setGroups(temp);
3949 const values = { } ;
4050 if ( data . name ) {
4151 values . name = data . name ;
@@ -50,6 +60,8 @@ const UserForm = ({ id, data }) => {
5060 setState ( {
5161 initialValues : values ,
5262 isLoading : false ,
63+ editMode : false ,
64+ groups : temp ,
5365 } ) ;
5466 } ) ;
5567 }
@@ -141,69 +153,69 @@ const UserForm = ({ id, data }) => {
141153 miqRedirectBack ( message , 'success' , url ) ;
142154 } ;
143155
144- const areGroupsEqual = ( selectedGroups = [ ] ) => {
145- selectedGroups . sort ( ) ;
146- initialValues . groups . sort ( ) ;
147- if ( selectedGroups . length !== initialValues . groups . length ) {
148- return false ;
149- }
150- return selectedGroups . every ( ( group , index ) => group === initialValues . groups [ index ] ) ;
151- } ;
156+ // const areGroupsEqual = (selectedGroups = []) => {
157+ // selectedGroups.sort();
158+ // initialValues.groups.sort();
159+ // if (selectedGroups.length !== initialValues.groups.length) {
160+ // return false;
161+ // }
162+ // return selectedGroups.every((group, index) => group === initialValues.groups[index]);
163+ // };
152164
153- const passwordValidation = ( values ) => {
154- const errors = { } ;
155- const groupIds = [ ] ;
156- if ( values . groups ) {
157- values . groups . forEach ( ( group ) => {
158- if ( group . value ) {
159- groupIds . push ( group . value ) ;
160- } else {
161- groupIds . push ( group ) ;
162- }
163- } ) ;
164- }
165- if ( ! editMode && ! ! id ) {
166- values . password = undefined ;
167- values . confirmPassword = undefined ;
168- if ( values . name === initialValues . name
169- && values . userid === initialValues . userid
170- && values . email === initialValues . email
171- && areGroupsEqual ( groupIds ) ) {
172- errors . confirmPassword = '' ;
173- }
174- }
165+ // const passwordValidation = (values) => {
166+ // const errors = {};
167+ // const groupIds = [];
168+ // if (values.groups) {
169+ // values.groups.forEach((group) => {
170+ // if (group.value) {
171+ // groupIds.push(group.value);
172+ // } else {
173+ // groupIds.push(group);
174+ // }
175+ // });
176+ // }
177+ // if (!editMode && !!id) {
178+ // values.password = undefined;
179+ // values.confirmPassword = undefined;
180+ // if (values.name === initialValues.name
181+ // && values.userid === initialValues.userid
182+ // && values.email === initialValues.email
183+ // && areGroupsEqual(groupIds)) {
184+ // errors.confirmPassword = '';
185+ // }
186+ // }
175187
176- if ( values . password === undefined ) {
177- if ( ! ! id && editMode ) {
178- errors . password = 'Required' ;
179- }
180- }
188+ // if (values.password === undefined) {
189+ // if (!!id && editMode) {
190+ // errors.password = 'Required';
191+ // }
192+ // }
181193
182- if ( editMode && values . password !== values . confirmPassword ) {
183- errors . confirmPassword = 'Password/Verify Password do not match' ;
184- }
185- if ( ! ! id === false && values . password !== values . confirmPassword ) {
186- errors . confirmPassword = 'Password/Verify Password do not match' ;
187- }
188- return errors ;
189- } ;
194+ // if (editMode && values.password !== values.confirmPassword) {
195+ // errors.confirmPassword = 'Password/Verify Password do not match';
196+ // }
197+ // if (!!id === false && values.password !== values.confirmPassword) {
198+ // errors.confirmPassword = 'Password/Verify Password do not match';
199+ // }
200+ // return errors;
201+ // };
190202
191203 const onFormReset = ( ) => {
192- setState ( {
193- initialValues ,
194- isLoading : false ,
195- } ) ;
196- setEditMode ( false ) ;
204+ setState ( ( state ) => ( {
205+ ... state ,
206+ editMode : false ,
207+ } ) ) ;
208+ // setEditMode(false);
197209 add_flash ( __ ( 'All changes have been reset' ) , 'warn' ) ;
198210 } ;
199211
200212 if ( isLoading ) return < Loading className = "export-spinner" withOverlay = { false } small /> ;
201213 return ! isLoading && (
202214 < div className = "col-md-12 user-form" >
203215 < MiqFormRenderer
204- schema = { createSchema ( id , editMode , setEditMode , groups ) }
216+ schema = { createSchema ( id , editMode , setState , groups , disabled ) }
205217 initialValues = { initialValues }
206- validate = { passwordValidation }
218+ validate = { ( values ) => passwordValidation ( initialValues , id , editMode , values ) }
207219 onSubmit = { onSubmit }
208220 onCancel = { onCancel }
209221 canReset = { ! ! id }
@@ -219,11 +231,13 @@ const UserForm = ({ id, data }) => {
219231UserForm . propTypes = {
220232 id : PropTypes . number ,
221233 data : PropTypes . objectOf ( PropTypes . any ) ,
234+ disabled : PropTypes . bool ,
222235} ;
223236
224237UserForm . defaultProps = {
225238 id : undefined ,
226239 data : undefined ,
240+ disabled : false ,
227241} ;
228242
229243export default UserForm ;
0 commit comments