1
+ import shortId from 'shortid'
1
2
import React , { PureComponent } from 'react' ;
2
3
import { Keyboard } from 'react-native'
3
4
import { NoteDetail } from '../components/NoteDetail'
@@ -8,9 +9,8 @@ import { actions } from '../redux/actions'
8
9
9
10
export class Note extends PureComponent {
10
11
static navigationOptions = ( { navigation } ) => ( {
11
- title : navigation . getParam ( 'name' , '' ) ,
12
12
headerRight : (
13
- < SaveButton onPress = { navigation . getParam ( 'onSaveButtonPress ' ) } />
13
+ < SaveButton onPress = { navigation . getParam ( 'onSaveNote ' ) } />
14
14
)
15
15
} )
16
16
@@ -19,25 +19,33 @@ export class Note extends PureComponent {
19
19
}
20
20
21
21
componentDidMount ( ) {
22
- this . props . navigation . setParams ( { onSaveButtonPress : this . onSaveNote } )
22
+ this . props . navigation . setParams ( { onSaveNote : this . onSaveNote } )
23
23
}
24
24
25
25
onSaveNote = ( ) => {
26
- const { projectId, note, editNote } = this . props
26
+ Keyboard . dismiss ( )
27
+
28
+ const { projectId, noteId, note, navigation, addNote, editNote } = this . props
27
29
const { noteText } = this . state
28
30
29
- if ( noteText && noteText !== note . text ) {
30
- editNote ( projectId , note . id , noteText )
31
+ if ( ! noteId ) {
32
+ const newNoteId = shortId . generate ( )
33
+ navigation . setParams ( { noteId : newNoteId } )
34
+ addNote ( projectId , newNoteId , noteText )
35
+ } else if ( noteText && noteText !== note . text ) {
36
+ editNote ( projectId , noteId , noteText )
31
37
}
32
- Keyboard . dismiss ( )
33
38
}
34
39
35
40
onChangeNote = noteText => {
36
41
this . setState ( { noteText } )
37
42
}
38
43
39
44
render ( ) {
40
- const noteText = this . state . noteText || this . props . note . text
45
+ const initialTextValue = this . props . note ?
46
+ this . props . note . text : ''
47
+ const noteText = this . state . noteText || initialTextValue
48
+
41
49
return (
42
50
< NoteDetail
43
51
noteText = { noteText }
@@ -48,9 +56,16 @@ export class Note extends PureComponent {
48
56
}
49
57
50
58
const mapStateToProps = ( state , ownProps ) => {
51
- const { noteId, projectId } = ownProps . navigation . state . params
52
- const project = state . projects . find ( project => project . id === projectId )
53
- const note = project . notes . find ( note => note . id === noteId )
59
+ const projectId = ownProps . navigation . getParam ( 'projectId' )
60
+ const noteId = ownProps . navigation . getParam ( 'noteId' , null )
61
+
62
+ const project = projectId ?
63
+ state . projects . find ( project => project . id === projectId ) :
64
+ null
65
+
66
+ const note = project ?
67
+ project . notes . find ( note => note . id === noteId ) :
68
+ null
54
69
55
70
return {
56
71
noteId,
0 commit comments