11import React , { useState , useEffect } from 'react' ;
2+ import { node , string } from 'prop-types' ;
3+
4+ import { alignmentType } from '../../../types' ;
25
36import AlignmentContext from '../alignment-context' ;
47import SentenceContext from '../sentence-context' ;
@@ -63,7 +66,7 @@ const buildIdMap = (alignedText, sentence) => {
6366 idMap [ lnum2 ] [ n2 ] [ lnum2 ] = idMap [ lnum1 ] [ n1 ] [ lnum2 ] ;
6467 } ;
6568
66- ( sentence . wds || [ ] ) . forEach ( ( wd ) => {
69+ sentence . wds . forEach ( ( wd ) => {
6770 const { lnum } = wd . $ ;
6871
6972 wd . w . forEach ( ( word ) => {
@@ -90,16 +93,16 @@ const buildIdMap = (alignedText, sentence) => {
9093 return idMap ;
9194} ;
9295
96+ // eslint-disable-next-line react/prop-types
9397const WrappedSentence = ( { id, json, children } ) => {
94- const [ active , setActive ] = useState ( { } ) ;
98+ const [ active , setActive ] = useState ( null ) ;
9599 const [ idMap , setIdMap ] = useState ( { } ) ;
96100 const [ sentence , setSentence ] = useState ( { } ) ;
97101
98102 useEffect ( ( ) => {
99103 const alignedText = json [ 'aligned-text' ] ;
100- const newSentence = alignedText
101- ? alignedText . sentence . find ( ( { $ : { id : sentenceId } } ) => sentenceId === id )
102- : { } ;
104+ const newSentence = ( alignedText . sentence || [ ] )
105+ . find ( ( { $ : { id : sentenceId } } ) => sentenceId === id ) ;
103106
104107 setSentence ( newSentence ) ;
105108 setIdMap ( buildIdMap ( alignedText , newSentence ) ) ;
@@ -133,4 +136,23 @@ const Sentence = ({ id, children }) => (
133136 </ AlignmentContext . Consumer >
134137) ;
135138
139+ WrappedSentence . propTypes = {
140+ id : string . isRequired ,
141+ json : alignmentType . isRequired ,
142+ children : node ,
143+ } ;
144+
145+ WrappedSentence . defaultProps = {
146+ children : null ,
147+ } ;
148+
149+ Sentence . propTypes = {
150+ id : string . isRequired ,
151+ children : node ,
152+ } ;
153+
154+ Sentence . defaultProps = {
155+ children : null ,
156+ } ;
157+
136158export default Sentence ;
0 commit comments