logic.rs 3.35 KB
use crate::api::markdown::Markdown;
use mogwai::prelude::*;
use super::state::MarkdownState;

#[derive(Clone)]
pub(super) enum MarkdownLogic {
    Choose(Option<i32>),
    Discard,
    Select,
    Store,
    Toggle,
    Update,
}

pub(super) async fn markdown_logic( mut rx_logic: broadcast::Receiver<MarkdownLogic>
                                  , tx_logic: broadcast::Sender<MarkdownLogic>
                                  , tx_toggle: broadcast::Sender<bool>
                                  , tx_patches: mpmc::Sender<ListPatch<ViewBuilder<Dom>>>
                                  , mut rx_dom: broadcast::Receiver<Dom> )
{
    let dom = rx_dom.next().await.unwrap();
    let mut md = Markdown::new("md-example").await.unwrap();
    let mut state = MarkdownState::new(dom).unwrap();

    while let Some(msg) = rx_logic.next().await {
        match msg {
            MarkdownLogic::Store => {
                let new_md = state.get_md();
                if md.json.content != new_md {
                    md.json.content = new_md;
                    md.save().await.unwrap();
                }
            },
            MarkdownLogic::Update => state.update(),
            MarkdownLogic::Toggle => {
                state.toggle_show_edit();
                tx_toggle.broadcast(state.show_edit).await.unwrap();
            },
            MarkdownLogic::Discard => {
                state.set_md(Some(md.json.content.as_str()));
                state.update();
            },
            MarkdownLogic::Select => {
                state.toggle_show_patches();

                match state.show_patches {
                    true => {
                        let patches = md
                                    . patches().await.unwrap()
                                    . into_iter()
                                    . map(|diff| {
                                        let id = Some(diff.id);
                                        let choose_filter = tx_logic
                                            . sink()
                                            . contra_map(move |_| MarkdownLogic::Choose(id));
                                        builder! {
                                            <li><button on:click=choose_filter
                                                        value=format!("{}", diff.id.to_owned())>
                                            {diff.date_created.to_owned()}
                                            </button></li>
                                        }});
                        let all = vec![builder! {
                            <li><button on:click=tx_logic.sink().contra_map(|_| MarkdownLogic::Choose(None))>
                            "Current"
                            </button></li>
                        }].into_iter().chain(patches);

                        let list_replace = ListPatch::splice(.., all);
                        tx_patches.send(list_replace).await.unwrap();
                    },
                    false => tx_patches.send(ListPatch::drain()).await.unwrap(),
                }
            },
            MarkdownLogic::Choose(id) => {
                md.read(id).await.unwrap();
                state.set_md(Some(md.json.content.as_str()));
                state.update();
                tx_patches.send(ListPatch::drain()).await.unwrap();
                state.reset_show_patches();
            },
        }
    }
}