11import React from 'react'
22import times from 'lodash/times'
33import get from 'lodash/get'
4+ import { find , forEach } from 'lodash'
45import TextField from '@mui/material/TextField'
56import Typography from '@mui/material/Typography'
67import Button from '@mui/material/Button'
78
8- const Propose = ( { onChange, proposed, onSubmit} ) => {
9+ const Propose = ( { onChange, proposed, onSubmit, repo , row , columns } ) => {
910 const [ attributes , setAttributes ] = React . useState ( 1 )
11+ const repoLabel = `${ repo ?. short_code || repo ?. id } :${ repo ?. version || repo . id } `
12+
13+ const getStateFromRow = ( ) => {
14+ let _state = { id : '' , name : '' , attributes : [ ] }
15+ forEach ( row , ( value , key ) => {
16+ if ( ! key . startsWith ( '__' ) ) {
17+ const col = find ( columns , { dataKey : key } )
18+ if ( col ?. label ) {
19+ if ( [ 'id' , 'name' ] . includes ( col . label . toLowerCase ( ) ) )
20+ _state [ col . label . toLowerCase ( ) ] = value
21+ else
22+ _state . attributes . push ( { name : col . label , value : value } )
23+ } else {
24+ _state . attributes . push ( { name : key , value : value } )
25+ }
26+ }
27+ } )
28+ return _state
29+ }
30+
31+ const [ state , setState ] = React . useState ( { } )
32+
33+ const _onChange = event => {
34+ event . persist ( )
35+ let newState ;
36+ if ( event . target . id . includes ( 'attributes.' ) ) {
37+ const idParts = event . target . id . split ( '.' )
38+ const attributeIndex = parseInt ( idParts [ 1 ] )
39+ const attributeType = idParts [ 2 ]
40+ const newAttributes = [ ...state . attributes ]
41+ newAttributes [ attributeIndex ] [ attributeType ] = event . target . value
42+ newState = { ...state , attributes : newAttributes }
43+ console . log ( newState )
44+ setState ( newState )
45+ } else {
46+ newState = { ...state , [ event . target . id ] : event . target . value }
47+ setState ( newState )
48+ }
49+ onChange ( newState )
50+ }
51+
52+ React . useEffect ( ( ) => {
53+ const stateFromRow = getStateFromRow ( )
54+ let _attributes = proposed ?. attributes ?. length ? [ ...proposed . attributes ] || [ ] : [ ...stateFromRow . attributes ] || [ ]
55+ let _proposed = { ...proposed }
56+ _proposed . attributes = [ ...( proposed . attributes || [ ] ) ]
57+ forEach ( _proposed , ( value , key ) => {
58+ if ( key . includes ( 'attributes.' ) ) {
59+ let parts = key . split ( '.' )
60+ _proposed . attributes [ parseInt ( parts [ 1 ] ) ] [ parts [ 2 ] ] = value
61+ delete _proposed [ key ]
62+ }
63+ } )
64+ setState ( {
65+ ..._proposed ,
66+ source : proposed ?. source || repoLabel || '' ,
67+ id : proposed ?. id || stateFromRow ?. id || '' ,
68+ name : proposed ?. name || stateFromRow ?. name || '' ,
69+ attributes : _attributes ,
70+ map_type : proposed ?. map_type || 'SAME-AS' ,
71+ note : proposed ?. note || ''
72+ } )
73+ setAttributes ( _attributes ?. length && _attributes . length > 0 ? _attributes . length : 1 )
74+ } , [ row ] )
75+
76+ const _onSubmit = event => {
77+ event . persist ( )
78+ onSubmit ( event , state )
79+ }
80+
1081 return (
1182 < div className = 'col-xs-12 padding-0' style = { { margin : '12px 0' } } >
1283 < div className = 'col-xs-12 padding-0' >
13- < TextField id = 'source' sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Target Source' value = { proposed ?. source || '' } onChange = { onChange } />
14- < TextField id = 'id' sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Concept ID' value = { proposed ?. id || '' } onChange = { onChange } />
15- < TextField id = 'name' sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' , } } label = 'Name' value = { proposed ?. name || '' } onChange = { onChange } />
84+ < TextField id = 'source' sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Target Source' value = { state . source } onChange = { _onChange } />
85+ < TextField id = 'id' sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Concept ID' value = { state ?. id || '' } onChange = { _onChange } />
86+ < TextField id = 'name' sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' , } } label = 'Name' value = { state ?. name || '' } onChange = { _onChange } />
87+ < TextField id = 'map_type' sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' , } } label = 'Map Type' value = { state ?. map_type || 'SAME-AS' } onChange = { _onChange } />
1688 < Typography sx = { { fontWeight : 'bold' , margin : '10px 10px 4px' } } > Attributes</ Typography >
1789 {
1890 times ( attributes , i => {
1991 return (
2092 < div className = 'col-xs-12 padding-0' key = { i } >
21- < TextField id = { `attributes.${ i } .name` } sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Attribute Name' value = { get ( proposed , `attributes.${ i } .name` ) || '' } onChange = { onChange } />
22- < TextField id = { `attributes.${ i } .value` } sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Attribute Value' value = { get ( proposed , `attributes.${ i } .value` ) || '' } onChange = { onChange } />
93+ < TextField id = { `attributes.${ i } .name` } sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Attribute Name' value = { get ( state , `attributes.${ i } .name` ) || '' } onChange = { _onChange } />
94+ < TextField id = { `attributes.${ i } .value` } sx = { { width : 'calc(50% - 12px)' , margin : '4px 6px' } } label = 'Attribute Value' value = { get ( state , `attributes.${ i } .value` ) || '' } onChange = { _onChange } />
2395 </ div >
2496 )
2597 } )
@@ -35,12 +107,12 @@ const Propose = ({onChange, proposed, onSubmit}) => {
35107 label = "Proposal note"
36108 multiline
37109 rows = { 5 }
38- value = { proposed ?. note || '' }
39- onChange = { onChange }
110+ value = { state ?. note || '' }
111+ onChange = { _onChange }
40112 />
41113 </ div >
42114 < div className = 'col-xs-12 padding-0' style = { { margin : '16px 0' , display : 'flex' , alignItems : 'center' } } >
43- < Button color = 'primary' onClick = { onSubmit } variant = 'contained' sx = { { textTransform : 'none' } } >
115+ < Button color = 'primary' onClick = { _onSubmit } variant = 'contained' sx = { { textTransform : 'none' } } >
44116 Propose
45117 </ Button >
46118 </ div >
0 commit comments