33
44import React from 'react'
55import PropTypes from 'prop-types'
6- import { TwoLineEntry , QuickEditModel , SourcePicker , FileCountRenderer } from './'
6+ import { TwoLineEntry , QuickEditModel , FileCountRenderer } from './'
77import { Checkbox , OverlayTrigger , Tooltip , Popover } from 'react-bootstrap'
88import { Tag } from 'antd'
99import { get , isEqual , union } from 'lodash'
@@ -34,7 +34,7 @@ class DefinitionEntry extends React.Component {
3434 }
3535 }
3636 static propTypes = {
37- onChange : PropTypes . func ,
37+ onChange : PropTypes . func . isRequired ,
3838 onCurate : PropTypes . func ,
3939 onInspect : PropTypes . func ,
4040 activeFacets : PropTypes . array ,
@@ -60,7 +60,7 @@ class DefinitionEntry extends React.Component {
6060 const newChanges = { ...component . changes }
6161 if ( isChanged && proposedValue !== null ) newChanges [ field ] = proposedValue
6262 else delete newChanges [ field ]
63- onChange && onChange ( component , newChanges , field )
63+ onChange ( component , newChanges , field )
6464 }
6565 }
6666
@@ -90,10 +90,10 @@ class DefinitionEntry extends React.Component {
9090 < ScoreRenderer scores = { scores } definition = { definition } />
9191 </ span >
9292 ) : null
93- const releasedDate = definition ?. described ?. releaseDate ? (
94- < span className = "releasedDate-table" > { definition . described . releaseDate } </ span >
95- ) : (
96- < span className = "releasedDate-table" > -- -- -- < /span >
93+ const releasedDate = (
94+ < span className = "releasedDate-table" >
95+ { this . renderFieldWithToolTipIfDifferent ( 'described.releaseDate' , a => a || '-- -- --' ) }
96+ </ span >
9797 )
9898 const curationTag = isCurationPending ? (
9999 < span >
@@ -125,7 +125,7 @@ class DefinitionEntry extends React.Component {
125125 renderWithToolTipIfDifferent ( field , content , placement = 'right' , transform = x => x ) {
126126 const toolTip = (
127127 < Tooltip id = { `tooltip-${ field } ` } className = "definition__tooltip" >
128- Original: { transform ( get ( this . props . otherDefinition , field ) ) }
128+ Original: { transform ( this . getOriginalValue ( field ) ) }
129129 </ Tooltip >
130130 )
131131 return this . ifDifferent (
@@ -137,14 +137,19 @@ class DefinitionEntry extends React.Component {
137137 )
138138 }
139139
140+ renderFieldWithToolTipIfDifferent ( field , transform = a => a ) {
141+ const displayValue = transform ( this . getValue ( field ) )
142+ return this . renderWithToolTipIfDifferent (
143+ field ,
144+ < span className = { this . classIfDifferent ( field ) } > { displayValue } </ span > ,
145+ undefined ,
146+ transform
147+ )
148+ }
149+
140150 renderMessage ( definition ) {
141151 const licenseExpression = definition ? this . getValue ( 'licensed.declared' ) : null
142- return licenseExpression
143- ? this . renderWithToolTipIfDifferent (
144- 'licensed.declared' ,
145- < span className = { this . classIfDifferent ( 'licensed.declared' ) } > { licenseExpression } </ span >
146- )
147- : null
152+ return licenseExpression ? this . renderFieldWithToolTipIfDifferent ( 'licensed.declared' ) : null
148153 }
149154
150155 getPercentage ( count , total ) {
@@ -190,6 +195,23 @@ class DefinitionEntry extends React.Component {
190195 this . setState ( { modelOpen : ! this . state . modelOpen } )
191196 }
192197
198+ handleSaveEdit = updates => {
199+ const { onChange, definition, component } = this . props
200+
201+ const newChanges = Object . entries ( updates ) . reduce ( ( changes , [ key , value ] ) => {
202+ let field
203+ if ( key === 'declared' ) field = 'licensed.declared'
204+ if ( key === 'sourceComponent' ) field = 'described.sourceLocation'
205+ if ( key === 'release' ) field = 'described.releaseDate'
206+ return field ? Contribution . applyChanges ( definition , changes , field , value ) : changes
207+ } , { } )
208+
209+ if ( Object . keys ( newChanges ) . length !== 0 ) {
210+ const combinedChanges = { ...component . changes , ...newChanges }
211+ onChange ( component , combinedChanges )
212+ }
213+ }
214+
193215 renderPanel ( rawDefinition ) {
194216 if ( ! rawDefinition )
195217 return (
@@ -201,13 +223,12 @@ class DefinitionEntry extends React.Component {
201223 // TODO: find a way of calling this method less frequently. It's relatively expensive.
202224 const definition = this . foldFacets ( rawDefinition , this . props . activeFacets )
203225 const { licensed } = definition
204- const { readOnly, onRevert } = this . props
205226 return (
206227 < div className = "row row-panel-details" >
207228 < div className = "col-md-6 d-flex justify-content-start align-items-center" >
208229 < span className = "panel-details__title" > { this . renderLabel ( 'Declared' ) } :</ span >
209230 < div className = "panel-details__value" >
210- < p > { this . getValue ( 'licensed.declared' ) } </ p >
231+ { this . renderFieldWithToolTipIfDifferent ( 'licensed.declared' ) }
211232 { /* {this.renderWithToolTipIfDifferent(
212233 'licensed.declared',
213234 <LicensesRenderer
@@ -232,7 +253,7 @@ class DefinitionEntry extends React.Component {
232253 < div className = "col-md-6 d-flex justify-content-start align-items-center" >
233254 < span className = "panel-details__title" > { this . renderLabel ( 'Source' ) } :</ span >
234255 < div className = "panel-details__value" >
235- < p > { Contribution . printCoordinates ( this . getValue ( 'described.sourceLocation' ) ) } </ p >
256+ { this . renderFieldWithToolTipIfDifferent ( 'described.sourceLocation' , a => Contribution . printCoordinates ( a ) ) }
236257 { /* {this.renderWithToolTipIfDifferent(
237258 'described.sourceLocation',
238259 <ModalEditor
@@ -263,7 +284,10 @@ class DefinitionEntry extends React.Component {
263284 < div className = "col-md-6 d-flex justify-content-start align-items-center" >
264285 < span className = "panel-details__title" > { this . renderLabel ( 'Release' ) } :</ span >
265286 < div className = "panel-details__value" >
266- < p > { Contribution . printDate ( this . getValue ( 'described.releaseDate' ) ) } </ p >
287+ { this . renderFieldWithToolTipIfDifferent (
288+ 'described.releaseDate' ,
289+ a => Contribution . printDate ( a ) || '-- -- --'
290+ ) }
267291 { /* {this.renderWithToolTipIfDifferent(
268292 'described.releaseDate',
269293 <InlineEditor
@@ -292,33 +316,12 @@ class DefinitionEntry extends React.Component {
292316 < QuickEditModel
293317 open = { this . state . modelOpen }
294318 closeModel = { this . handleModel }
295- definition = { definition }
296- field = { 'described.sourceLocation' }
297- extraClass = { this . classIfDifferent ( 'described.sourceLocation' ) }
298- readOnly = { readOnly }
299- initialValue = { {
300- declared : this . getOriginalValue ( 'licensed.declared' ) ,
301- source : Contribution . printCoordinates ( this . getOriginalValue ( 'described.sourceLocation' ) ) ,
302- release : Contribution . printDate ( this . getOriginalValue ( 'described.releaseDate' ) ) ,
303- repo : ''
304- } }
305- values = { {
319+ initialValues = { {
306320 declared : this . getValue ( 'licensed.declared' ) ,
307- source : Contribution . printCoordinates ( this . getValue ( 'described.sourceLocation' ) ) ,
308- release : Contribution . printDate ( this . getValue ( 'described.releaseDate' ) ) ,
309- repo : ''
310- } }
311- onChange = { {
312- declared : this . fieldChange ( 'licensed.declared' ) ,
313- source : this . fieldChange ( 'described.sourceLocation' , isEqual , Contribution . toSourceLocation ) ,
314- release : this . fieldChange ( 'described.releaseDate' ) ,
315- repo : ''
321+ sourceComponent : this . getValue ( 'described.sourceLocation' ) ,
322+ release : Contribution . printDate ( this . getValue ( 'described.releaseDate' ) )
316323 } }
317- editor = { SourcePicker }
318- validator = { value => true }
319- placeholder = { 'Source location' }
320- revertable
321- onRevert = { ( ) => onRevert ( 'described.sourceLocation' ) }
324+ onSave = { this . handleSaveEdit }
322325 />
323326 < button onClick = { this . handleModel } className = "quick-edit-btn" >
324327 Edit
0 commit comments