/*
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 .
*/
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";
import { BaseDialogProps, DialogManager } from "../common/dialog-manager";
type AddNotebookDialogProps = BaseDialogProps & {
parentId?: string;
edit?: boolean;
notebook?: Notebook;
};
export const AddNotebookDialog = DialogManager.register(
function AddNotebookDialog(props: AddNotebookDialogProps) {
const { notebook, onClose, parentId } = props;
const title = useRef(notebook?.title || "");
const description = useRef(notebook?.description || "");
const onSubmit = useCallback(async () => {
if (!title.current.trim())
return showToast("error", "Notebook title cannot be empty.");
const id = await db.notebooks.add({
id: props.notebook?.id,
title: title.current,
description: description.current
});
if (!id) return;
if (parentId) {
await db.relations.add(
{ type: "notebook", id: parentId },
{ type: "notebook", id }
);
}
await notebookStore.refresh();
await noteStore.refresh();
await appStore.refreshNavItems();
showToast(
"success",
props.edit
? "Notebook edited successfully!"
: "Notebook created successfully"
);
onClose(true);
}, [props.notebook?.id, props.edit, onClose, parentId]);
return (
);
}
);
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 });
}
};