//! The trash: trashed notes with Restore and Delete forever, and Empty trash. use std::sync::Arc; use topcoat::{ Result, context::{Cx, app_context}, router::{ error::{SeeOther, see_other}, page, route, }, view::{View, attributes, view}, }; use crate::app::pane; use crate::app::shell::now_millis; use crate::components::button::{ButtonSize, ButtonVariant, button}; use crate::components::dialog::{ dialog, dialog_content, dialog_description, dialog_footer, dialog_header, dialog_title, }; use crate::hub::Hub; use crate::i18n::{self, Msg, forever_count, notes_count, relative_time, t}; use crate::store::{ListFilter, Store}; mod note_id; /// GET /trash: trashed notes, most recently trashed first. #[page] async fn trash(cx: &Cx) -> Result { let store = app_context::>(cx); let page = pane::load_page(cx, store, ListFilter::Trash) .await .map_err(topcoat::Error::from_anyhow)?; let lang = i18n::lang(cx); let now = now_millis(); // The trash's total, not the rows on this page: it decides the empty state and whether // "Empty trash" shows. let count = page.total; let previews = page.previews; let more = page.more; let title = t(lang, Msg::Trash); let show_more = t(lang, Msg::ShowMore); let count_text = notes_count(lang, count); let retention = t(lang, Msg::TrashRetention); let empty_label = t(lang, Msg::EmptyTrashAction); let empty_title = t(lang, Msg::ConfirmEmptyTrashTitle); let empty_body = forever_count(lang, count); let cancel = t(lang, Msg::Cancel); let restore_label = t(lang, Msg::Restore); let forever_label = t(lang, Msg::DeleteForever); let forever_title = t(lang, Msg::ConfirmForeverTitle); let cannot_undo = t(lang, Msg::CannotUndo); let trashed_word = t(lang, Msg::Trashed); Ok(view! {

(title)

(count_text)

(retention)

if count > 0 { button( variant: ButtonVariant::Destructive, size: ButtonSize::Sm, attrs: attributes! { type="button" onclick="document.getElementById('empty-trash').showModal()" }, (empty_label) ) }
if count == 0 {

(t(lang, Msg::EmptyTrash))

} else {
    for note in page.notes {
  • (crate::app::shell::title_of(lang, ¬e.title))

    (previews.get(¬e.id).cloned().unwrap_or_default())

    (format!("{trashed_word} ยท {}", relative_time(lang, note.trashed_at.unwrap_or(note.updated_at), now)))

    button( variant: ButtonVariant::Outline, size: ButtonSize::Sm, attrs: attributes! { type="submit" }, (restore_label) )
    button( variant: ButtonVariant::Destructive, size: ButtonSize::Sm, attrs: attributes! { type="button" onclick=(format!("document.getElementById('forever-{}').showModal()", note.id)) }, (forever_label) ) dialog( open: false, attrs: attributes! { id=(format!("forever-{}", note.id)) aria-labelledby=(format!("forever-title-{}", note.id)) }, dialog_content(
    dialog_header( dialog_title(attrs: attributes! { id=(format!("forever-title-{}", note.id)) }, (forever_title)) dialog_description((cannot_undo)) ) dialog_footer( button( variant: ButtonVariant::Outline, attrs: attributes! { type="submit" formmethod="dialog" autofocus=(true) }, (cancel) ) button( variant: ButtonVariant::Destructive, attrs: attributes! { type="submit" }, (forever_label) ) )
    ) )
  • }
if let Some(href) = more { (show_more) } } if count > 0 { dialog( open: false, attrs: attributes! { id="empty-trash" aria-labelledby="empty-trash-title" aria-describedby="empty-trash-body" }, dialog_content(
dialog_header( dialog_title(attrs: attributes! { id="empty-trash-title" }, (empty_title)) dialog_description(attrs: attributes! { id="empty-trash-body" }, (empty_body)) ) dialog_footer( button( variant: ButtonVariant::Outline, attrs: attributes! { type="submit" formmethod="dialog" autofocus=(true) }, (cancel) ) button( variant: ButtonVariant::Destructive, attrs: attributes! { type="submit" }, (empty_label) ) )
) ) }
}) } /// POST /trash/empty: deletes every trashed note for good. #[route(POST "./empty")] async fn empty_trash(cx: &Cx) -> Result { let store = app_context::>(cx); let hub = app_context::>(cx); let ids = store.empty_trash().await.map_err(topcoat::Error::from_anyhow)?; for id in &ids { hub.forget(id); } Ok(see_other("/trash")) }