handle a possible error

This commit is contained in:
ammarahm-ed
2021-05-15 12:45:58 +05:00
parent 56ad5c8374
commit a72026c10b
3 changed files with 72 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
import React, {createRef} from 'react'; import React, {createRef} from 'react';
import { Actions } from '../../provider/Actions'; import {Actions} from '../../provider/Actions';
import { import {
eSubscribeEvent, eSubscribeEvent,
eUnSubscribeEvent, eUnSubscribeEvent,
@@ -13,7 +13,7 @@ import BaseDialog from '../Dialog/base-dialog';
import DialogButtons from '../Dialog/dialog-buttons'; import DialogButtons from '../Dialog/dialog-buttons';
import DialogContainer from '../Dialog/dialog-container'; import DialogContainer from '../Dialog/dialog-container';
import DialogHeader from '../Dialog/dialog-header'; import DialogHeader from '../Dialog/dialog-header';
import { updateEvent } from '../DialogManager/recievers'; import {updateEvent} from '../DialogManager/recievers';
import Input from '../Input'; import Input from '../Input';
import {Toast} from '../Toast'; import {Toast} from '../Toast';
@@ -33,33 +33,35 @@ export class AddTopicDialog extends React.Component {
} }
addNewTopic = async () => { addNewTopic = async () => {
this.setState({loading: true}); try {
if (!this.title || this.title?.trim() === '') { this.setState({loading: true});
ToastEvent.show({ if (!this.title || this.title?.trim() === '') {
heading: 'Topic title is required', ToastEvent.show({
type: 'error', heading: 'Topic title is required',
context: 'local', type: 'error',
}); context: 'local',
});
this.setState({loading: false});
return;
}
if (!this.toEdit) {
await db.notebooks.notebook(this.notebook.id).topics.add(this.title);
} else {
let topic = this.toEdit;
topic.title = this.title;
await db.notebooks.notebook(topic.notebookId).topics.add(topic);
}
this.setState({loading: false}); this.setState({loading: false});
return; this.close();
} Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
if (!this.toEdit) { Navigation.routeNames.Notebook,
await db.notebooks.notebook(this.notebook.id).topics.add(this.title); Navigation.routeNames.NotesPage,
} else { ]);
let topic = this.toEdit; updateEvent({type: Actions.MENU_PINS});
topic.title = this.title; } catch (e) {}
await db.notebooks.notebook(topic.notebookId).topics.add(topic);
}
this.setState({loading: false});
this.close();
Navigation.setRoutesToUpdate([
Navigation.routeNames.Notebooks,
Navigation.routeNames.Notebook,
Navigation.routeNames.NotesPage,
]);
updateEvent({type:Actions.MENU_PINS});
}; };
componentDidMount() { componentDidMount() {
@@ -121,7 +123,7 @@ export class AddTopicDialog extends React.Component {
<Input <Input
fwdRef={this.titleRef} fwdRef={this.titleRef}
onChangeText={(value) => { onChangeText={value => {
this.title = value; this.title = value;
}} }}
blurOnSubmit={false} blurOnSubmit={false}

View File

@@ -1,27 +1,27 @@
import React, { useEffect, useState } from 'react'; import React, {useEffect, useState} from 'react';
import { ContainerBottomButton } from '../../components/Container/ContainerBottomButton'; import {ContainerBottomButton} from '../../components/Container/ContainerBottomButton';
import { ContainerTopSection } from '../../components/Container/ContainerTopSection'; import {ContainerTopSection} from '../../components/Container/ContainerTopSection';
import { Header } from '../../components/Header'; import {Header} from '../../components/Header';
import SelectionHeader from '../../components/SelectionHeader'; import SelectionHeader from '../../components/SelectionHeader';
import SimpleList from '../../components/SimpleList'; import SimpleList from '../../components/SimpleList';
import { import {
eSendEvent, eSendEvent,
eSubscribeEvent, eSubscribeEvent,
eUnSubscribeEvent eUnSubscribeEvent,
} from '../../services/EventManager'; } from '../../services/EventManager';
import Navigation from '../../services/Navigation'; import Navigation from '../../services/Navigation';
import SearchService from '../../services/SearchService'; import SearchService from '../../services/SearchService';
import { InteractionManager } from '../../utils'; import {InteractionManager} from '../../utils';
import { db } from '../../utils/DB'; import {db} from '../../utils/DB';
import { import {
eOnNewTopicAdded, eOnNewTopicAdded,
eOpenAddNotebookDialog, eOpenAddNotebookDialog,
eOpenAddTopicDialog, eOpenAddTopicDialog,
eScrollEvent eScrollEvent,
} from '../../utils/Events'; } from '../../utils/Events';
export const Notebook = ({route, navigation}) => { export const Notebook = ({route, navigation}) => {
const [topics, setTopics] = useState(route.params.notebook.topics); const [topics, setTopics] = useState(route.params.notebook?.topics);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
let params = route.params; let params = route.params;
let pageIsLoaded = false; let pageIsLoaded = false;
@@ -29,34 +29,38 @@ export const Notebook = ({route, navigation}) => {
const runAfterInteractions = (time = 400) => { const runAfterInteractions = (time = 400) => {
InteractionManager.runAfterInteractions(() => { InteractionManager.runAfterInteractions(() => {
let notebook = db.notebooks.notebook(params?.notebook?.id)?.data; try {
if (notebook) { let notebook = db.notebooks.notebook(params?.notebook?.id)?.data;
params.notebook = notebook; if (notebook) {
} params.notebook = notebook;
params.title = params.notebook.title;
setTopics(notebook.topics);
setTimeout(() => {
if (loading) {
setLoading(false);
} }
}, 10); params.title = params.notebook.title;
Navigation.routeNeedsUpdate('Notebook', () => { if (notebook) {
onLoad(); setTopics(notebook.topics);
}); }
eSendEvent(eScrollEvent, {name: params.title, type: 'in'}); setTimeout(() => {
if (params.menu) { if (loading) {
navigation.setOptions({ setLoading(false);
animationEnabled: true, }
gestureEnabled: false, }, 10);
Navigation.routeNeedsUpdate('Notebook', () => {
onLoad();
}); });
} else { eSendEvent(eScrollEvent, {name: params.title, type: 'in'});
navigation.setOptions({ if (params.menu) {
animationEnabled: true, navigation.setOptions({
gestureEnabled: Platform.OS === 'ios', animationEnabled: true,
}); gestureEnabled: false,
} });
updateSearch(); } else {
ranAfterInteractions = false; navigation.setOptions({
animationEnabled: true,
gestureEnabled: Platform.OS === 'ios',
});
}
updateSearch();
ranAfterInteractions = false;
} catch (e) {}
}, time); }, time);
}; };
const onLoad = data => { const onLoad = data => {
@@ -128,9 +132,8 @@ export const Notebook = ({route, navigation}) => {
return ( return (
<> <>
<SelectionHeader screen="Notebook" /> <SelectionHeader screen="Notebook" />
<ContainerTopSection> <ContainerTopSection>
<Header <Header
title={params.title} title={params.title}
isBack={!params.menu} isBack={!params.menu}

View File

@@ -48,7 +48,7 @@ export const Notes = ({route, navigation}) => {
} else { } else {
_notes = db.notebooks _notes = db.notebooks
.notebook(params.notebookId) .notebook(params.notebookId)
.topics.topic(params.id).all; ?.topics.topic(params.id)?.all
} }
if ( if (
(params.type === 'tag' || params.type === 'color') && (params.type === 'tag' || params.type === 'color') &&