Skip to content

Commit

Permalink
Move search queries outside of Search
Browse files Browse the repository at this point in the history
Co-authored-by: Szymon Fiedler <[email protected]>
  • Loading branch information
mostlyobvious and fidel committed Mar 21, 2024
1 parent 100077b commit 9cc6e71
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
42 changes: 37 additions & 5 deletions ruby_event_store-browser/elm/src/Layout.elm
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
port module Layout exposing (Model, Msg, buildModel, subscriptions, update, view, viewIncorrectConfig, viewNotFound)

import Api exposing (SearchStream, getSearchStreams)
import Browser.Events
import Browser.Navigation
import BrowserTime
import Dict
import FeatherIcons
import Flags exposing (Flags)
import Html exposing (..)
import Html.Attributes exposing (class, href, id, list, placeholder, selected, value)
import Html.Events exposing (onClick, onInput, onSubmit)
import Http
import Json.Decode
import LinkedTimezones exposing (mapLinkedTimeZone)
import List.Extra
Expand All @@ -23,6 +26,7 @@ type Msg
| SearchMsg Search.Msg
| KeyPress String Bool Bool
| ToggleDialog
| SearchedStreamsFetched (Result Http.Error (List SearchStream))


type alias Model =
Expand Down Expand Up @@ -55,8 +59,13 @@ buildModel =


goToStream : WrappedModel Model -> String -> Cmd msg
goToStream model stream =
Browser.Navigation.pushUrl model.key (Route.streamUrl model.flags.rootUrl stream)
goToStream { key, flags } stream =
Browser.Navigation.pushUrl key (Route.streamUrl flags.rootUrl stream)


searchStreams : Flags -> String -> Cmd Msg
searchStreams flags stream =
getSearchStreams SearchedStreamsFetched flags stream


update : Msg -> WrappedModel Model -> ( WrappedModel Model, Cmd Msg )
Expand All @@ -65,14 +74,14 @@ update msg model =
SearchMsg searchMsg ->
let
( newSearch, cmd ) =
Search.update searchMsg model.internal.search model.flags (goToStream model)
Search.update searchMsg model.internal.search (goToStream model)
in
case searchMsg of
OnSelect _ ->
( { model | internal = Model newSearch }, toggleDialog searchModalId )

OnQueryChanged _ ->
( model, Cmd.none )
OnQueryChanged streamName ->
( model, searchStreams model.flags streamName )

_ ->
( { model | internal = Model newSearch }, Cmd.map SearchMsg cmd )
Expand Down Expand Up @@ -128,6 +137,29 @@ update msg model =
ToggleDialog ->
( model, toggleDialog searchModalId )

SearchedStreamsFetched (Ok streams) ->
let
streams_ =
"all" :: List.map .streamId streams

searchModel =
model.internal.search

newModel =
{ searchModel | streams = streams_ }
in
( { model | internal = Model newModel }, Cmd.none )

SearchedStreamsFetched (Err _) ->
let
searchModel =
model.internal.search

newModel =
{ searchModel | streams = [] }
in
( { model | internal = Model newModel }, Cmd.none )


view : (Msg -> a) -> WrappedModel Model -> Html a -> Html a
view layoutMsgBuilder model pageView =
Expand Down
23 changes: 3 additions & 20 deletions ruby_event_store-browser/elm/src/Search.elm
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Search exposing (..)

import Api exposing (SearchStream, getSearchStreams)
import FeatherIcons
import Flags exposing (Flags)
import Html exposing (..)
import Html.Attributes exposing (autofocus, class, id, list, placeholder, value)
import Html.Events exposing (onInput, onSubmit)
Expand All @@ -25,7 +23,6 @@ type alias Model =
type Msg
= StreamChanged Stream
| GoToStream Stream
| SearchedStreamsFetched (Result Http.Error (List SearchStream))
| OnSelect Stream
| OnQueryChanged Stream

Expand All @@ -47,18 +44,13 @@ hackWithInternalOnQueryChangedMsg stream =
Task.perform OnQueryChanged (Task.succeed stream)


searchStreams : Flags -> Stream -> Cmd Msg
searchStreams flags stream =
getSearchStreams SearchedStreamsFetched flags stream


isExactStream : String -> List String -> Bool
isExactStream stream streams =
List.any (\s -> s == stream) streams


update : Msg -> Model -> Flags -> (String -> Cmd Msg) -> ( Model, Cmd Msg )
update msg model flags onSubmit =
update : Msg -> Model -> (String -> Cmd Msg) -> ( Model, Cmd Msg )
update msg model onSubmit =
case msg of
StreamChanged stream ->
if isExactStream stream model.streams then
Expand All @@ -71,10 +63,7 @@ update msg model flags onSubmit =

else
( { model | value = stream }
, Cmd.batch
[ searchStreams flags stream
, hackWithInternalOnQueryChangedMsg stream
]
, hackWithInternalOnQueryChangedMsg stream
)

GoToStream stream ->
Expand All @@ -85,12 +74,6 @@ update msg model flags onSubmit =
]
)

SearchedStreamsFetched (Ok streams) ->
( { model | streams = "all" :: List.map .streamId streams }, Cmd.none )

SearchedStreamsFetched (Err _) ->
( { model | streams = [] }, Cmd.none )

OnSelect _ ->
( model, Cmd.none )

Expand Down

0 comments on commit 9cc6e71

Please sign in to comment.