use std::sync::Arc; use topcoat::{ Result, context::{Cx, app_context}, router::{ content::Form, error::{RouterErrorExt, SeeOther, see_other}, module_param, page, path_param, route, }, view::{Child, View, attributes, view}, }; use crate::app::pane::{self, Mobile}; use crate::components::button::{ButtonSize, ButtonVariant, button, button_variants}; use crate::components::dialog::{ dialog, dialog_content, dialog_description, dialog_footer, dialog_header, dialog_title, }; use crate::components::dropdown_menu::{ dropdown_menu, dropdown_menu_content, dropdown_menu_item, dropdown_menu_separator, dropdown_menu_trigger, }; use crate::components::floating::FloatingAlign; use crate::components::input::input; use crate::i18n::{self, Msg, t}; use crate::store::{ListFilter, Store}; use super::{NameForm, clean_name}; module_param!(notebook_id); /// GET /notebooks/{notebook_id}: the notes in one notebook beside its list, with a menu /// to rename or delete the notebook. #[page] async fn notebook(cx: &Cx) -> Result { let store = app_context::>(cx); let id = path_param::(cx).to_owned(); let nb = store .get_notebook(&id) .await .map_err(topcoat::Error::from_anyhow)? .ok_or_not_found()?; let notebooks = store.list_notebooks().await.map_err(topcoat::Error::from_anyhow)?; let page = pane::load_page(cx, store, ListFilter::Notebook(id.clone())) .await .map_err(topcoat::Error::from_anyhow)?; let lang = i18n::lang(cx); let groups = pane::groups(lang, &page.notes, ¬ebooks, &page.previews, None, false); let spec = pane::ListSpec { title: nb.name.clone(), heading: true, count: page.total, notebook_id: id.clone(), groups, empty: t(lang, Msg::EmptyNotebook), more: page.more, }; let rename_action = format!("/notebooks/{id}/rename"); let delete_action = format!("/notebooks/{id}/delete"); let options = t(lang, Msg::NotebookOptions); let rename_label = t(lang, Msg::RenameNotebook); let rename_short = t(lang, Msg::Rename); let delete_label = t(lang, Msg::DeleteNotebook); let delete_title = t(lang, Msg::DeleteNotebookTitle); let delete_body = t(lang, Msg::DeleteNotebookBody); let cancel = t(lang, Msg::Cancel); let name_label = t(lang, Msg::NewNotebookLabel); let actions = view! { dropdown_menu( dropdown_menu_trigger( target: "notebook-menu", attrs: attributes! { class=(button_variants(ButtonVariant::Ghost, ButtonSize::Icon)) aria-label=(options) }, ) dropdown_menu_content( id: "notebook-menu", align: FloatingAlign::End, dropdown_menu_item( close: Some("notebook-menu"), attrs: attributes! { onclick="document.getElementById('rename-notebook').showModal()" }, (rename_label) ) dropdown_menu_separator() dropdown_menu_item( close: Some("notebook-menu"), attrs: attributes! { class="text-destructive" onclick="document.getElementById('delete-notebook').showModal()" }, (delete_label) ) ) ) }; let list = pane::list_pane(cx, lang, spec, Child::new(actions)); let content = Child::new(pane::split( cx, Mobile::List, Child::new(list), Child::new(pane::empty_detail(cx, lang)), )); Ok(view! { (content) dialog( open: false, attrs: attributes! { id="rename-notebook" aria-labelledby="rename-notebook-title" }, dialog_content(
dialog_header( dialog_title(attrs: attributes! { id="rename-notebook-title" }, (rename_label)) ) input(attrs: attributes! { type="text" name="name" value=(nb.name.clone()) maxlength="120" required=(true) autocomplete="off" aria-label=(name_label) class="w-full" }) dialog_footer( button( variant: ButtonVariant::Ghost, attrs: attributes! { type="submit" formmethod="dialog" }, (cancel) ) button( variant: ButtonVariant::Primary, attrs: attributes! { type="submit" }, (rename_short) ) )
) ) dialog( open: false, attrs: attributes! { id="delete-notebook" aria-labelledby="delete-notebook-title" aria-describedby="delete-notebook-body" }, dialog_content(
dialog_header( dialog_title(attrs: attributes! { id="delete-notebook-title" }, (delete_title)) dialog_description(attrs: attributes! { id="delete-notebook-body" }, (delete_body)) ) dialog_footer( button( variant: ButtonVariant::Outline, attrs: attributes! { type="submit" formmethod="dialog" autofocus=(true) }, (cancel) ) button( variant: ButtonVariant::Destructive, attrs: attributes! { type="submit" }, (delete_label) ) )
) ) }) } /// POST /notebooks/{notebook_id}/rename #[route(POST "./rename")] async fn rename_notebook(cx: &Cx, Form(form): Form) -> Result { let store = app_context::>(cx); let id = path_param::(cx).to_owned(); store.get_notebook(&id).await.map_err(topcoat::Error::from_anyhow)?.ok_or_not_found()?; let name = clean_name(&form.name)?; store .rename_notebook(&id, &name) .await .map_err(topcoat::Error::from_anyhow)?; Ok(see_other(format!("/notebooks/{id}"))) } /// POST /notebooks/{notebook_id}/delete: its notes become unfiled. #[route(POST "./delete")] async fn delete_notebook(cx: &Cx) -> Result { let store = app_context::>(cx); let id = path_param::(cx).to_owned(); store.get_notebook(&id).await.map_err(topcoat::Error::from_anyhow)?.ok_or_not_found()?; store .delete_notebook(&id) .await .map_err(topcoat::Error::from_anyhow)?; Ok(see_other("/?toast=notebook_deleted")) }