Files
notesnook/apps/mobile/app/components/dialogs/add-topic/index.js

186 lines
5.0 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
import React, { createRef } from "react";
import { View } from "react-native";
import { useMenuStore } from "../../../stores/use-menu-store";
import {
eSubscribeEvent,
eUnSubscribeEvent,
ToastEvent
} from "../../../services/event-manager";
import Navigation from "../../../services/navigation";
import { db } from "../../../common/database";
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;
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 {
if (!this.title || this.title?.trim() === "") {
2021-05-15 12:45:58 +05:00
ToastEvent.show({
heading: "Topic title is required",
type: "error",
context: "local"
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);
}
this.close();
2023-03-16 21:22:21 +05:00
setTimeout(() => {
Navigation.queueRoutesForUpdate("Notebooks", "Notebook", "TopicNotes");
useMenuStore.getState().setMenuPins();
});
} catch (e) {
console.error(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 }) => {
let id = notebookId;
if (id) {
this.notebook = await db.notebooks.notebook(id).data;
}
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;
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
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();
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"
title={this.toEdit ? "Edit topic" : "New topic"}
paragraph={
this.toEdit
? "Edit title of the topic"
: "Add a new topic in " + this.notebook.title
}
padding={12}
2019-12-09 13:17:40 +05:00
/>
2021-07-28 11:53:45 +05:00
<Seperator half />
<View
style={{
2022-04-01 00:57:55 +05:00
paddingHorizontal: 12,
zIndex: 10
2022-01-22 12:57:05 +05:00
}}
>
<Input
fwdRef={this.titleRef}
2022-04-01 00:57:55 +05:00
testID="input-title"
onChangeText={(value) => {
this.title = value;
}}
blurOnSubmit={false}
2021-12-27 18:09:00 +05:00
placeholder="Enter title"
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
positiveTitle={this.toEdit ? "Save" : "Add"}
2021-01-08 13:10:25 +05:00
onPressNegative={() => this.close()}
onPressPositive={() => this.addNewTopic()}
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
);
}
}