From 95b4daf60b18ca95ae21e847519e7ec7de9491e8 Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Tue, 12 May 2020 14:09:56 +0500 Subject: [PATCH] fix topic and notebook editing --- .../src/components/AddNotebookDialog/index.js | 38 +++++++++++++------ .../src/components/AddTopicDialog/index.js | 3 ++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/apps/mobile/src/components/AddNotebookDialog/index.js b/apps/mobile/src/components/AddNotebookDialog/index.js index ffe4bb056..018114c6f 100644 --- a/apps/mobile/src/components/AddNotebookDialog/index.js +++ b/apps/mobile/src/components/AddNotebookDialog/index.js @@ -114,12 +114,17 @@ export class AddNotebookDialog extends React.Component { let id = toEdit && toEdit.id ? toEdit.id : null; let t = [...topics]; + console.log(this.prevIndex, this.prevItem); if ( this.currentInputValue && this.currentInputValue.trim().length !== 0 ) { - t.push(this.currentInputValue); - this.currentInputValue = null; + if (this.prevItem != null) { + t[this.prevIndex] = this.currentInputValue; + } else { + t.push(this.currentInputValue); + this.currentInputValue = null; + } } if (id) { await db.notebooks.add({ @@ -127,9 +132,16 @@ export class AddNotebookDialog extends React.Component { description: this.description, id: id, }); - console.log(t); - await db.notebooks.notebook(id).topics.add(...t); - + let allTopics = toEdit.topics; + t.forEach((title, index) => { + if (allTopics[index]) { + allTopics[index].title = title; + } else { + allTopics.push(title); + } + }); + console.log(allTopics); + await db.notebooks.notebook(id).topics.add(...allTopics); } else { await db.notebooks.add({ title: this.title, @@ -146,7 +158,7 @@ export class AddNotebookDialog extends React.Component { }, 100); }; - onSubmit = () => { + onSubmit = (forward = true) => { let {topics} = this.state; if (!this.currentInputValue || this.currentInputValue.trim().length === 0) return; @@ -171,7 +183,7 @@ export class AddNotebookDialog extends React.Component { topics: prevTopics, }); this.currentInputValue = null; - if (prevTopics[this.prevIndex + 1]) { + if (prevTopics[this.prevIndex + 1] && forward) { this.prevIndex = this.prevIndex + 1; this.prevItem = prevTopics[this.prevIndex]; this.currentInputValue = this.prevItem; @@ -188,9 +200,11 @@ export class AddNotebookDialog extends React.Component { this.topicInputRef.setNativeProps({ text: null, }); - setTimeout(() => { - this.listRef.scrollToEnd({animated: true}); - }, 30); + if (forward) { + setTimeout(() => { + this.listRef.scrollToEnd({animated: true}); + }, 30); + } } } }; @@ -301,7 +315,7 @@ export class AddNotebookDialog extends React.Component { (this.descriptionRef = ref)} style={{ - padding: pv -2 , + padding: pv - 2, borderWidth: 1.5, borderColor: descFocused ? colors.accent : colors.nav, paddingHorizontal: ph, @@ -364,7 +378,7 @@ export class AddNotebookDialog extends React.Component { }); }} onBlur={() => { - this.onSubmit(); + this.onSubmit(false); this.setState({ topicInputFoused: false, }); diff --git a/apps/mobile/src/components/AddTopicDialog/index.js b/apps/mobile/src/components/AddTopicDialog/index.js index 6a89d6807..22faeeac1 100644 --- a/apps/mobile/src/components/AddTopicDialog/index.js +++ b/apps/mobile/src/components/AddTopicDialog/index.js @@ -27,6 +27,9 @@ export class AddTopicDialog extends React.Component { if (!this.props.toEdit) { await db.notebooks.notebook(this.props.notebookID).topics.add(this.title); } else { + let topic = this.props.toEdit; + topic.title = this.title; + await db.notebooks.notebook(this.props.notebookID).topics.add(topic); } this.close(); ToastEvent.show('New topic added', 'success');