2023-11-09 14:20:30 +05:00
|
|
|
/*
|
|
|
|
|
This file is part of the Notesnook project (https://notesnook.com/)
|
|
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { useRef, useCallback } from "react";
|
|
|
|
|
import Dialog from "../components/dialog";
|
|
|
|
|
import Field from "../components/field";
|
|
|
|
|
import { showToast } from "../utils/toast";
|
|
|
|
|
import { Notebook } from "@notesnook/core";
|
|
|
|
|
import { store as noteStore } from "../stores/note-store";
|
|
|
|
|
import { store as notebookStore } from "../stores/notebook-store";
|
|
|
|
|
import { store as appStore } from "../stores/app-store";
|
|
|
|
|
import { db } from "../common/db";
|
2024-07-16 15:04:28 +05:00
|
|
|
import { BaseDialogProps, DialogManager } from "../common/dialog-manager";
|
2024-09-05 15:49:50 +05:00
|
|
|
import { strings } from "@notesnook/intl";
|
2023-11-09 14:20:30 +05:00
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
type AddNotebookDialogProps = BaseDialogProps<boolean> & {
|
2023-11-09 14:20:30 +05:00
|
|
|
parentId?: string;
|
|
|
|
|
edit?: boolean;
|
|
|
|
|
notebook?: Notebook;
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
export const AddNotebookDialog = DialogManager.register(
|
|
|
|
|
function AddNotebookDialog(props: AddNotebookDialogProps) {
|
|
|
|
|
const { notebook, onClose, parentId } = props;
|
|
|
|
|
const title = useRef<string>(notebook?.title || "");
|
|
|
|
|
const description = useRef<string>(notebook?.description || "");
|
2023-11-09 14:20:30 +05:00
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
const onSubmit = useCallback(async () => {
|
|
|
|
|
if (!title.current.trim())
|
2024-09-06 11:07:43 +05:00
|
|
|
return showToast("error", strings.allFieldsRequired());
|
2023-11-09 14:20:30 +05:00
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
const id = await db.notebooks.add({
|
|
|
|
|
id: props.notebook?.id,
|
|
|
|
|
title: title.current,
|
|
|
|
|
description: description.current
|
|
|
|
|
});
|
2024-09-02 13:43:05 +05:00
|
|
|
if (!id) return;
|
|
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
if (parentId) {
|
|
|
|
|
await db.relations.add(
|
|
|
|
|
{ type: "notebook", id: parentId },
|
|
|
|
|
{ type: "notebook", id }
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-11-09 14:20:30 +05:00
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
await notebookStore.refresh();
|
|
|
|
|
await noteStore.refresh();
|
|
|
|
|
await appStore.refreshNavItems();
|
2023-11-09 14:20:30 +05:00
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
showToast(
|
|
|
|
|
"success",
|
2023-11-09 14:20:30 +05:00
|
|
|
props.edit
|
2024-07-16 15:04:28 +05:00
|
|
|
? "Notebook edited successfully!"
|
|
|
|
|
: "Notebook created successfully"
|
|
|
|
|
);
|
|
|
|
|
onClose(true);
|
|
|
|
|
}, [props.notebook?.id, props.edit, onClose, parentId]);
|
|
|
|
|
return (
|
|
|
|
|
<Dialog
|
|
|
|
|
testId="add-notebook-dialog"
|
|
|
|
|
isOpen={true}
|
|
|
|
|
title={props.edit ? "Edit Notebook" : "Create a Notebook"}
|
|
|
|
|
description={
|
|
|
|
|
props.edit
|
|
|
|
|
? `You are editing "${notebook?.title}".`
|
|
|
|
|
: "Notebooks are the best way to organize your notes."
|
|
|
|
|
}
|
|
|
|
|
onClose={() => onClose(false)}
|
|
|
|
|
positiveButton={{
|
|
|
|
|
text: props.edit ? "Save" : "Create",
|
|
|
|
|
onClick: onSubmit
|
2023-11-09 14:20:30 +05:00
|
|
|
}}
|
2024-07-16 15:04:28 +05:00
|
|
|
negativeButton={{ text: "Cancel", onClick: () => onClose(false) }}
|
|
|
|
|
>
|
|
|
|
|
<Field
|
|
|
|
|
defaultValue={title.current}
|
|
|
|
|
data-test-id="title-input"
|
|
|
|
|
autoFocus
|
|
|
|
|
required
|
2024-09-05 15:49:50 +05:00
|
|
|
label={strings.title()}
|
2024-07-16 15:04:28 +05:00
|
|
|
name="title"
|
|
|
|
|
id="title"
|
|
|
|
|
onChange={(e) => (title.current = e.target.value)}
|
|
|
|
|
onKeyUp={async (e) => {
|
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
|
await onSubmit();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
<Field
|
|
|
|
|
data-test-id="description-input"
|
2024-09-05 15:49:50 +05:00
|
|
|
label={strings.description()}
|
2024-07-16 15:04:28 +05:00
|
|
|
name="description"
|
|
|
|
|
id="description"
|
|
|
|
|
onChange={(e) => (description.current = e.target.value)}
|
|
|
|
|
defaultValue={description.current}
|
2024-09-05 15:49:50 +05:00
|
|
|
helpText={strings.optional()}
|
2024-07-16 15:04:28 +05:00
|
|
|
sx={{ mt: 1 }}
|
|
|
|
|
/>
|
|
|
|
|
</Dialog>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-11-09 14:20:30 +05:00
|
|
|
|
2024-07-16 15:04:28 +05:00
|
|
|
type EditNotebookDialogProps = { notebookId: string };
|
|
|
|
|
export const EditNotebookDialog = {
|
|
|
|
|
show: async (props: EditNotebookDialogProps) => {
|
|
|
|
|
const notebook = await db.notebooks.notebook(props.notebookId);
|
|
|
|
|
if (!notebook) return;
|
|
|
|
|
return AddNotebookDialog.show({ edit: true, notebook });
|
|
|
|
|
}
|
|
|
|
|
};
|