intendednull / yewdux

Ergonomic state management for Yew applications
https://intendednull.github.io/yewdux/
Apache License 2.0
319 stars 31 forks source link

Feature Suggestion: history/undo/redo #53

Closed wainwrightmark closed 1 year ago

wainwrightmark commented 1 year ago

Hi. I pattern I use quite often is a history store that enables undo/redo functionality for a particular store.

It's quite a common pattern and can be implemented generically so I wonder if it would be worth creating a macro to automatically generate it.

usage would look something like this

#[derive(Default, Clone, PartialEq, Eq, Debug)]
#[store(history)]
struct CounterStore {
    count: i32,
}

#[function_component]
fn Controls() -> Html {
    let (state, dispatch) = use_store::<CounterStoreHistoryState>();

    let on_undo_click = dispatch.apply_callback(|_| CounterStoreHistoryMessage::Undo);
    let on_redo_click = dispatch.apply_callback(|_| CounterStoreHistoryMessage::Redo);

    html!(
        <div>
            <button onclick={on_undo_click} disabled={undo_disabled}>{"Undo"}</button>
            <button onclick={on_redo_click} disabled={redo_disabled}>{"Redo"}</button>
        </div>
    )
}

The CounterStoreHistoryState and CounterStoreHistoryMessage structs are generated by the macro.

Happy to do a PR if you think this is a good idea - I've already written the code a couple of times, just need to write a macro to generate it.

intendednull commented 1 year ago

Sure, I can see a place for this. Maybe add a crate to the workspace called yewdux-utils or something. That can be the home for this kind of stuff

intendednull commented 1 year ago

Released!