2022-01-22 12:57:05 +05:00
|
|
|
import React, { createRef } from 'react';
|
|
|
|
|
import { Keyboard, LayoutAnimation, UIManager, View } from 'react-native';
|
|
|
|
|
import { Transition, Transitioning, TransitioningView } from 'react-native-reanimated';
|
2022-04-24 05:59:14 +05:00
|
|
|
import { useMenuStore } from '../../../stores/use-menu-store';
|
2022-02-28 23:25:18 +05:00
|
|
|
import { eSubscribeEvent, eUnSubscribeEvent, ToastEvent } from '../../../services/event-manager';
|
|
|
|
|
import Navigation from '../../../services/navigation';
|
2022-08-16 16:48:10 +05:00
|
|
|
import { db } from '../../../common/database';
|
2022-02-28 15:32:55 +05:00
|
|
|
import { eCloseAddTopicDialog, eOpenAddTopicDialog } from '../../../utils/events';
|
|
|
|
|
import { sleep } from '../../../utils/time';
|
|
|
|
|
import BaseDialog from '../../dialog/base-dialog';
|
|
|
|
|
import DialogButtons from '../../dialog/dialog-buttons';
|
|
|
|
|
import DialogContainer from '../../dialog/dialog-container';
|
|
|
|
|
import DialogHeader from '../../dialog/dialog-header';
|
|
|
|
|
import Input from '../../ui/input';
|
|
|
|
|
import Seperator from '../../ui/seperator';
|
|
|
|
|
import { Toast } from '../../toast';
|
2019-12-09 13:17:40 +05:00
|
|
|
|
2020-01-18 18:14:17 +05:00
|
|
|
export class AddTopicDialog extends React.Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
this.state = {
|
|
|
|
|
visible: false,
|
|
|
|
|
titleFocused: false,
|
2021-07-28 11:53:45 +05:00
|
|
|
loading: false
|
2020-01-18 18:14:17 +05:00
|
|
|
};
|
|
|
|
|
|
2022-01-08 00:35:52 +05:00
|
|
|
this.ref = createRef();
|
2020-01-18 18:14:17 +05:00
|
|
|
this.title;
|
2020-03-02 12:12:23 +05:00
|
|
|
this.titleRef = createRef();
|
2020-12-07 11:10:20 +05:00
|
|
|
this.notebook = null;
|
2021-02-20 14:50:27 +05:00
|
|
|
this.toEdit = null;
|
2020-01-18 18:14:17 +05:00
|
|
|
}
|
2020-01-08 22:47:49 +05:00
|
|
|
|
2020-01-18 18:14:17 +05:00
|
|
|
addNewTopic = async () => {
|
2021-05-15 12:45:58 +05:00
|
|
|
try {
|
2022-01-22 12:57:05 +05:00
|
|
|
this.setState({ loading: true });
|
2021-05-15 12:45:58 +05:00
|
|
|
if (!this.title || this.title?.trim() === '') {
|
|
|
|
|
ToastEvent.show({
|
|
|
|
|
heading: 'Topic title is required',
|
|
|
|
|
type: 'error',
|
2021-07-28 11:53:45 +05:00
|
|
|
context: 'local'
|
2021-05-15 12:45:58 +05:00
|
|
|
});
|
2022-01-22 12:57:05 +05:00
|
|
|
this.setState({ loading: false });
|
2021-05-15 12:45:58 +05:00
|
|
|
return;
|
|
|
|
|
}
|
2020-01-18 18:14:17 +05:00
|
|
|
|
2021-05-15 12:45:58 +05:00
|
|
|
if (!this.toEdit) {
|
|
|
|
|
await db.notebooks.notebook(this.notebook.id).topics.add(this.title);
|
|
|
|
|
} else {
|
|
|
|
|
let topic = this.toEdit;
|
|
|
|
|
topic.title = this.title;
|
2020-09-27 13:05:26 +05:00
|
|
|
|
2021-05-15 12:45:58 +05:00
|
|
|
await db.notebooks.notebook(topic.notebookId).topics.add(topic);
|
|
|
|
|
}
|
2022-01-22 12:57:05 +05:00
|
|
|
this.setState({ loading: false });
|
2021-05-15 12:45:58 +05:00
|
|
|
this.close();
|
2022-04-24 05:59:14 +05:00
|
|
|
Navigation.queueRoutesForUpdate('Notebooks', 'Notebook', 'TopicNotes');
|
2021-06-05 21:10:20 +05:00
|
|
|
useMenuStore.getState().setMenuPins();
|
2021-05-15 12:45:58 +05:00
|
|
|
} catch (e) {}
|
2019-12-09 13:17:40 +05:00
|
|
|
};
|
|
|
|
|
|
2020-12-17 10:31:35 +05:00
|
|
|
componentDidMount() {
|
|
|
|
|
eSubscribeEvent(eOpenAddTopicDialog, this.open);
|
|
|
|
|
eSubscribeEvent(eCloseAddTopicDialog, this.close);
|
|
|
|
|
}
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
eUnSubscribeEvent(eOpenAddTopicDialog, this.open);
|
|
|
|
|
eUnSubscribeEvent(eCloseAddTopicDialog, this.close);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-22 12:57:05 +05:00
|
|
|
open = async ({ notebookId, toEdit }) => {
|
2021-02-20 14:50:27 +05:00
|
|
|
let id = notebookId;
|
|
|
|
|
if (id) {
|
|
|
|
|
this.notebook = await db.notebooks.notebook(id).data;
|
2021-01-08 13:14:37 +05:00
|
|
|
}
|
2021-02-20 14:50:27 +05:00
|
|
|
this.toEdit = toEdit;
|
|
|
|
|
|
|
|
|
|
if (this.toEdit) {
|
|
|
|
|
this.title = this.toEdit.title;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 18:14:17 +05:00
|
|
|
this.setState({
|
2021-07-28 11:53:45 +05:00
|
|
|
visible: true
|
2020-01-18 18:14:17 +05:00
|
|
|
});
|
2020-12-17 10:31:35 +05:00
|
|
|
};
|
2020-12-07 11:00:10 +05:00
|
|
|
close = () => {
|
2020-01-18 18:14:17 +05:00
|
|
|
this.title = null;
|
2021-01-08 13:10:25 +05:00
|
|
|
this.notebook = null;
|
2021-02-20 14:50:27 +05:00
|
|
|
this.toEdit = null;
|
2020-01-18 18:14:17 +05:00
|
|
|
this.setState({
|
2021-07-28 11:53:45 +05:00
|
|
|
visible: false
|
2020-01-18 18:14:17 +05:00
|
|
|
});
|
2020-12-07 11:10:20 +05:00
|
|
|
};
|
2020-01-18 18:14:17 +05:00
|
|
|
|
|
|
|
|
render() {
|
2022-01-22 12:57:05 +05:00
|
|
|
const { visible } = this.state;
|
2020-11-23 12:32:33 +05:00
|
|
|
if (!visible) return null;
|
2020-01-18 18:14:17 +05:00
|
|
|
return (
|
2020-09-27 13:05:26 +05:00
|
|
|
<BaseDialog
|
2021-02-20 14:50:27 +05:00
|
|
|
onShow={async () => {
|
2022-01-07 23:42:58 +05:00
|
|
|
if (this.toEdit) {
|
|
|
|
|
this.titleRef.current?.setNativeProps({
|
|
|
|
|
text: this.toEdit.title
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-01-08 00:35:52 +05:00
|
|
|
|
|
|
|
|
this.ref.current?.animateNextTransition();
|
2021-02-20 14:50:27 +05:00
|
|
|
await sleep(300);
|
2020-03-02 12:12:23 +05:00
|
|
|
this.titleRef.current?.focus();
|
|
|
|
|
}}
|
2022-01-08 00:35:52 +05:00
|
|
|
bounce={false}
|
2020-11-26 12:17:37 +05:00
|
|
|
statusBarTranslucent={false}
|
2020-11-23 12:32:33 +05:00
|
|
|
visible={true}
|
2022-01-22 12:57:05 +05:00
|
|
|
onRequestClose={this.close}
|
|
|
|
|
>
|
2020-12-15 14:11:45 +05:00
|
|
|
<DialogContainer>
|
2020-09-27 13:05:26 +05:00
|
|
|
<DialogHeader
|
|
|
|
|
icon="book-outline"
|
2021-12-27 18:09:00 +05:00
|
|
|
title={this.toEdit ? 'Edit topic' : 'New topic'}
|
2021-02-20 14:50:27 +05:00
|
|
|
paragraph={
|
2022-01-22 12:57:05 +05:00
|
|
|
this.toEdit ? 'Edit title of the topic' : 'Add a new topic in ' + this.notebook.title
|
2021-02-20 14:50:27 +05:00
|
|
|
}
|
2021-11-08 15:06:46 +05:00
|
|
|
padding={12}
|
2019-12-09 13:17:40 +05:00
|
|
|
/>
|
2021-07-28 11:53:45 +05:00
|
|
|
<Seperator half />
|
2021-11-08 15:06:46 +05:00
|
|
|
<View
|
|
|
|
|
style={{
|
2022-04-01 00:57:55 +05:00
|
|
|
paddingHorizontal: 12,
|
|
|
|
|
zIndex: 10
|
2022-01-22 12:57:05 +05:00
|
|
|
}}
|
|
|
|
|
>
|
2021-11-08 15:06:46 +05:00
|
|
|
<Input
|
|
|
|
|
fwdRef={this.titleRef}
|
2022-04-01 00:57:55 +05:00
|
|
|
testID="input-title"
|
2021-11-08 15:06:46 +05:00
|
|
|
onChangeText={value => {
|
|
|
|
|
this.title = value;
|
|
|
|
|
}}
|
|
|
|
|
blurOnSubmit={false}
|
2021-12-27 18:09:00 +05:00
|
|
|
placeholder="Enter title"
|
2021-11-08 15:06:46 +05:00
|
|
|
onSubmit={() => this.addNewTopic()}
|
|
|
|
|
returnKeyLabel="Done"
|
|
|
|
|
returnKeyType="done"
|
|
|
|
|
/>
|
|
|
|
|
</View>
|
2020-01-18 18:14:17 +05:00
|
|
|
|
2020-09-27 13:05:26 +05:00
|
|
|
<DialogButtons
|
2021-12-27 18:09:00 +05:00
|
|
|
positiveTitle={this.toEdit ? 'Save' : 'Add'}
|
2021-01-08 13:10:25 +05:00
|
|
|
onPressNegative={() => this.close()}
|
2021-01-08 13:14:37 +05:00
|
|
|
onPressPositive={() => this.addNewTopic()}
|
2021-02-09 16:41:34 +05:00
|
|
|
loading={this.state.loading}
|
2020-09-27 13:05:26 +05:00
|
|
|
/>
|
2020-12-15 14:11:45 +05:00
|
|
|
</DialogContainer>
|
2020-03-17 10:46:21 +05:00
|
|
|
<Toast context="local" />
|
2020-09-27 13:05:26 +05:00
|
|
|
</BaseDialog>
|
2020-01-18 18:14:17 +05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|