From 2bb07e6f947673acc30519e4866901bb6ebf3486 Mon Sep 17 00:00:00 2001 From: Szymon Fiedler Date: Fri, 22 Mar 2024 13:42:52 +0100 Subject: [PATCH] =?UTF-8?q?Improved=20non=E2=80=93matching=20stream=20name?= =?UTF-8?q?s=20clearing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of wiping out the data fetched via http call, we'll filter out streams not matching the pattern withing the input field. There's one glitch to fix because of (text "") expanding the form height. --- ruby_event_store-browser/elm/src/Search.elm | 18 ++++++++-- .../elm/tests/SearchTest.elm | 33 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 ruby_event_store-browser/elm/tests/SearchTest.elm diff --git a/ruby_event_store-browser/elm/src/Search.elm b/ruby_event_store-browser/elm/src/Search.elm index 87fa5ac6af..1d901feb73 100644 --- a/ruby_event_store-browser/elm/src/Search.elm +++ b/ruby_event_store-browser/elm/src/Search.elm @@ -44,7 +44,7 @@ update msg model = ) else - ( { model | value = stream, streams = [] }, Cmd.none ) + ( { model | value = stream }, Cmd.none ) GoToStream stream -> ( { model | value = emptyStreamName } @@ -80,12 +80,15 @@ view model = viewStreamList : Model a -> Html Msg -viewStreamList model = +viewStreamList { value, streams } = div [] [ ul [ class "mt-4 overflow-auto space-y-2 w-full" ] - (List.map (\stream -> viewStreamListItem stream) model.streams) + (streams + |> filterStreams value + |> List.map viewStreamListItem + ) ] @@ -123,3 +126,12 @@ hasAtLeastThreeChars stream = streamsPresent : Model a -> Bool streamsPresent { streams } = not <| List.isEmpty streams + + +filterStreams : Stream -> List Stream -> List Stream +filterStreams stream streams = + if String.isEmpty stream then + [] + + else + List.filter (String.contains stream) streams diff --git a/ruby_event_store-browser/elm/tests/SearchTest.elm b/ruby_event_store-browser/elm/tests/SearchTest.elm new file mode 100644 index 0000000000..9d15d97d3a --- /dev/null +++ b/ruby_event_store-browser/elm/tests/SearchTest.elm @@ -0,0 +1,33 @@ +module SearchTest exposing (suite) + +import Expect +import Search exposing (Stream, filterStreams) +import Test exposing (..) + + +suite : Test +suite = + describe "Suite" + [ describe "filter streams" <| + [ test "filterStreams starting with" <| + \_ -> + Expect.equal + [ "DummyStream$78", "DummyStream$79" ] + (filterStreams "Dum" [ "DummyStream$78", "DummyStream$79" ]) + , test "filterStreams empty" <| + \_ -> + Expect.equal + [] + (filterStreams "" [ "DummyStream$78", "DummyStream$79" ]) + , test "filterStreams non matching" <| + \_ -> + Expect.equal + [] + (filterStreams "foo" [ "DummyStream$78", "DummyStream$79" ]) + , test "filterStreams ending with" <| + \_ -> + Expect.equal + [ "DummyStream$78" ] + (filterStreams "78" [ "DummyStream$78", "DummyStream$79" ]) + ] + ]