Skip to content

Commit 05db03f

Browse files
committed
Add new note on Note screen
1 parent 6adbfe5 commit 05db03f

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

containers/Note.js

+26-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import shortId from 'shortid'
12
import React, { PureComponent } from 'react';
23
import { Keyboard } from 'react-native'
34
import { NoteDetail } from '../components/NoteDetail'
@@ -8,9 +9,8 @@ import { actions } from '../redux/actions'
89

910
export class Note extends PureComponent {
1011
static navigationOptions = ({ navigation }) => ({
11-
title: navigation.getParam('name', ''),
1212
headerRight: (
13-
<SaveButton onPress={navigation.getParam('onSaveButtonPress')} />
13+
<SaveButton onPress={navigation.getParam('onSaveNote')} />
1414
)
1515
})
1616

@@ -19,25 +19,33 @@ export class Note extends PureComponent {
1919
}
2020

2121
componentDidMount() {
22-
this.props.navigation.setParams({ onSaveButtonPress: this.onSaveNote })
22+
this.props.navigation.setParams({ onSaveNote: this.onSaveNote })
2323
}
2424

2525
onSaveNote = () => {
26-
const { projectId, note, editNote } = this.props
26+
Keyboard.dismiss()
27+
28+
const { projectId, noteId, note, navigation, addNote, editNote } = this.props
2729
const { noteText } = this.state
2830

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)
3137
}
32-
Keyboard.dismiss()
3338
}
3439

3540
onChangeNote = noteText => {
3641
this.setState({ noteText })
3742
}
3843

3944
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+
4149
return (
4250
<NoteDetail
4351
noteText={noteText}
@@ -48,9 +56,16 @@ export class Note extends PureComponent {
4856
}
4957

5058
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
5469

5570
return {
5671
noteId,

containers/Project.js

+14-20
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import { bindActionCreators } from 'redux'
88
import { actions } from '../redux/actions'
99

1010
export class Project extends PureComponent {
11-
static navigationOptions = ({ navigation }) => ({
12-
title: navigation.getParam('name', ''),
13-
headerRight: (
14-
<AddButton onPress={navigation.getParam('onRightButtonPress')} />
15-
)
16-
})
17-
18-
addNote = () => {
19-
const { projectId, addNote } = this.props
20-
addNote(projectId, shortid.generate())
11+
static navigationOptions = ({ navigation }) => {
12+
const projectId = navigation.getParam('projectId')
13+
return {
14+
title: navigation.getParam('name', ''),
15+
headerRight: (
16+
<AddButton
17+
onPress={() => navigation.navigate('Note', { projectId })}
18+
/>
19+
)
20+
}
2121
}
2222

2323
removeNote = noteId => {
@@ -36,14 +36,10 @@ export class Project extends PureComponent {
3636
this.props.addProject(newProjectId, name)
3737
}
3838

39-
componentDidMount() {
40-
this.props.navigation.setParams({ onRightButtonPress: this.addNote })
41-
}
42-
4339
render() {
4440
const { projectId, project } = this.props
4541

46-
if (!projectId || !project) {
42+
if (!projectId) {
4743
return (
4844
<ProjectNameInput
4945
onSubmitEditing={this.createProject}
@@ -63,11 +59,9 @@ export class Project extends PureComponent {
6359

6460
const mapStateToProps = (state, ownProps) => {
6561
const projectId = ownProps.navigation.getParam('projectId', null)
66-
let project = null
67-
68-
if (projectId) {
69-
project = state.projects.find(project => projectId === project.id)
70-
}
62+
const project = projectId ?
63+
state.projects.find(project => projectId === project.id) :
64+
null
7165

7266
return {
7367
projectId,

containers/Projects.js

+6-15
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,22 @@ import { actions } from '../redux/actions'
88
export class Projects extends PureComponent {
99
static navigationOptions = ({ navigation }) => ({
1010
headerRight: (
11-
<AddButton
12-
onPress={navigation.getParam('onHeaderButtonPress')}
13-
/>
11+
<AddButton onPress={() => navigation.navigate('Project')} />
1412
)
1513
})
1614

17-
componentDidMount() {
18-
this.props.navigation.setParams({
19-
onHeaderButtonPress: this.navigateNewProject
15+
navigateProject = project => {
16+
this.props.navigation.navigate('Project', {
17+
projectId: project.id,
18+
name: project.name
2019
})
2120
}
2221

23-
navigateNewProject = () => {
24-
this.props.navigation.navigate('Project')
25-
}
26-
27-
navigateExistingProject = (project) => {
28-
this.props.navigation.navigate('Project', { projectId: project.id, name: project.name })
29-
}
30-
3122
render() {
3223
return (
3324
<ProjectList
3425
projects={this.props.projects}
35-
onPressProject={this.navigateExistingProject}
26+
onPressProject={this.navigateProject}
3627
/>
3728
)
3829
}

0 commit comments

Comments
 (0)