Skip to content

Commit

Permalink
Refactor the loading and request management
Browse files Browse the repository at this point in the history
resolves #224
  • Loading branch information
turboMaCk committed Nov 19, 2020
1 parent 29f5f02 commit c41aa60
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 93 deletions.
120 changes: 37 additions & 83 deletions src/Main.elm
Expand Up @@ -108,100 +108,54 @@ updateWith toPage toMsg model ( subModel, subCmd ) =
)


submitQuery :
Model
-> ( Model, Cmd Msg )
-> ( Model, Cmd Msg )
submitQuery old ( new, cmd ) =
attemptQuery : ( Model, Cmd Msg ) -> ( Model, Cmd Msg )
attemptQuery (( model, _ ) as pair) =
let
triggerSearch _ newModel msg makeRequest =
if
(newModel.query /= Nothing)
&& (newModel.query /= Just "")
&& List.member newModel.channel Search.channels
then
( new
, Cmd.batch
[ cmd
, makeRequest
new.elasticsearch
newModel.channel
(Maybe.withDefault "" newModel.query)
newModel.from
newModel.size
newModel.sort
|> Cmd.map msg
]
)

else
( new, cmd )
-- We intentially throw away Cmd
-- because we don't want to perform any effects
-- in this cases where route itself doesn't change
noEffects =
Tuple.mapSecond (always Cmd.none)

submitQuery msg makeRequest searchModel =
Tuple.mapSecond
(\cmd ->
Cmd.batch
[ cmd
, Cmd.map msg <| makeRequest
model.elasticsearch
searchModel.channel
(Maybe.withDefault "" searchModel.query)
searchModel.from
searchModel.size
searchModel.sort
]
) pair
in
case ( old.page, new.page ) of
( Packages oldModel, Packages newModel ) ->
triggerSearch oldModel newModel PackagesMsg Page.Packages.makeRequest
case model.page of
Packages searchModel ->
if Search.shouldLoad searchModel then
submitQuery PackagesMsg Page.Packages.makeRequest searchModel

( NotFound, Packages newModel ) ->
triggerSearch newModel newModel PackagesMsg Page.Packages.makeRequest
else
noEffects pair

( Options oldModel, Options newModel ) ->
triggerSearch oldModel newModel OptionsMsg Page.Options.makeRequest
Options searchModel ->
if Search.shouldLoad searchModel then
submitQuery OptionsMsg Page.Options.makeRequest searchModel

( NotFound, Options newModel ) ->
triggerSearch newModel newModel OptionsMsg Page.Options.makeRequest
else
noEffects pair

( _, _ ) ->
( new, cmd )
_ ->
pair


changeRouteTo :
Model
-> Url.Url
-> ( Model, Cmd Msg )
changeRouteTo currentModel url =
let
attempteQuery ( newModel, cmd ) =
let
-- We intentially throw away Cmd
-- because we don't want to perform any effects
-- in this cases where route itself doesn't change
noEffects =
( newModel, Cmd.none )
in
case ( currentModel.route, newModel.route ) of
( Route.Packages arg1, Route.Packages arg2 ) ->
if
(arg1.channel /= arg2.channel)
|| (arg1.query /= arg2.query)
|| (arg1.from /= arg2.from)
|| (arg1.size /= arg2.size)
|| (arg1.sort /= arg2.sort)
then
submitQuery newModel ( newModel, cmd )

else
noEffects

( Route.Options arg1, Route.Options arg2 ) ->
if
(arg1.channel /= arg2.channel)
|| (arg1.query /= arg2.query)
|| (arg1.from /= arg2.from)
|| (arg1.size /= arg2.size)
|| (arg1.sort /= arg2.sort)
then
submitQuery newModel ( newModel, cmd )

else
noEffects

( a, b ) ->
if a /= b then
submitQuery newModel ( newModel, cmd )

else
noEffects
in
case Route.fromUrl url of
Nothing ->
( { currentModel | page = NotFound }
Expand Down Expand Up @@ -234,7 +188,7 @@ changeRouteTo currentModel url =
in
Page.Packages.init searchArgs modelPage
|> updateWith Packages PackagesMsg model
|> attempteQuery
|> attemptQuery

Route.Options searchArgs ->
let
Expand All @@ -248,7 +202,7 @@ changeRouteTo currentModel url =
in
Page.Options.init searchArgs modelPage
|> updateWith Options OptionsMsg model
|> attempteQuery
|> attemptQuery


update : Msg -> Model -> ( Model, Cmd Msg )
Expand Down
28 changes: 18 additions & 10 deletions src/Search.elm
Expand Up @@ -12,6 +12,7 @@ module Search exposing
, init
, makeRequest
, makeRequestBody
, shouldLoad
, update
, view
)
Expand Down Expand Up @@ -152,6 +153,20 @@ init args model =
)


shouldLoad : Model a -> Bool
shouldLoad model =
model.result == RemoteData.Loading


ensureLoading : Model a -> Model a
ensureLoading model =
if model.query /= Nothing && model.query /= Just "" && List.member model.channel channels then
{ model | result = RemoteData.Loading }

else
model



-- ---------------------------
-- UPDATE
Expand Down Expand Up @@ -191,14 +206,9 @@ update toRoute navKey msg model =
ChannelChange channel ->
{ model
| channel = channel
, result =
if model.query == Nothing || model.query == Just "" then
RemoteData.NotAsked

else
RemoteData.Loading
, from = 0
}
|> ensureLoading
|> pushUrl toRoute navKey

QueryInput query ->
Expand All @@ -207,10 +217,8 @@ update toRoute navKey msg model =
)

QueryInputSubmit ->
{ model
| result = RemoteData.Loading
, from = 0
}
{ model | from = 0 }
|> ensureLoading
|> pushUrl toRoute navKey

QueryResponse result ->
Expand Down

0 comments on commit c41aa60

Please sign in to comment.