Skip to content

Commit

Permalink
refactor selectbox
Browse files Browse the repository at this point in the history
  • Loading branch information
myrho committed Oct 22, 2024
1 parent 1b40fbc commit 7502815
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 63 deletions.
18 changes: 1 addition & 17 deletions src/Effect.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Effect exposing (and, n, perform)
module Effect exposing (perform)

--import Plugin.Effect

Expand All @@ -22,22 +22,6 @@ import Route
import Task


n : m -> ( m, List eff )
n m =
( m, [] )


and : (m -> ( m, List eff )) -> ( m, List eff ) -> ( m, List eff )
and update ( m, eff ) =
let
( m2, eff2 ) =
update m
in
( m2
, eff ++ eff2
)


perform : Plugins -> Nav.Key -> Maybe String -> String -> Effect -> Cmd Msg
perform plugins key statusbarToken apiKey effect =
case effect of
Expand Down
57 changes: 34 additions & 23 deletions src/Update.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Browser.Dom
import Config.Update exposing (Config)
import DateFormat
import Dict exposing (Dict)
import Effect exposing (n)
import Effect.Api
import Effect.Graph as Graph
import Effect.Locale as Locale
Expand Down Expand Up @@ -62,6 +61,7 @@ import Update.Pathfinder as Pathfinder
import Update.Search as Search
import Update.Statusbar as Statusbar
import Url exposing (Url)
import Util exposing (n)
import Util.ThemedSelectBox as TSelectBox
import Util.ThemedSelectBoxes as TSelectBoxes
import View.Locale as Locale
Expand Down Expand Up @@ -271,23 +271,7 @@ update plugins uc msg model =
|> n

UserSwitchesLocale loc ->
let
locale =
Locale.switch loc model.config.locale

newModel =
{ model
| config =
model.config
|> s_locale locale
}
in
( newModel
, [ Locale.getTranslationEffect loc
|> LocaleEffect
, SaveUserSettingsEffect (Model.userSettingsFromMainModel newModel)
]
)
switchLocale loc model

UserClickedLightmode ->
let
Expand Down Expand Up @@ -1129,18 +1113,45 @@ update plugins uc msg model =

SelectBoxMsg sb subMsg ->
let
newModel =
{ model | selectBoxes = model.selectBoxes |> TSelectBoxes.update sb subMsg }
( selectBoxes, outMsg ) =
model.selectBoxes
|> TSelectBoxes.update sb subMsg

( TSelectBoxes.SupportedLanguages, TSelectBox.Select x ) =
( sb, subMsg )
newModel =
{ model | selectBoxes = selectBoxes }
in
update plugins uc (UserSwitchesLocale x) newModel
case ( sb, outMsg ) of
( TSelectBoxes.SupportedLanguages, Just (TSelectBox.Selected x) ) ->
switchLocale x newModel

_ ->
n newModel

NotificationMsg ms ->
n { model | notifications = Notification.update ms model.notifications }


switchLocale : String -> Model key -> ( Model key, List Effect )
switchLocale loc model =
let
locale =
Locale.switch loc model.config.locale

newModel =
{ model
| config =
model.config
|> s_locale locale
}
in
( newModel
, [ Locale.getTranslationEffect loc
|> LocaleEffect
, SaveUserSettingsEffect (Model.userSettingsFromMainModel newModel)
]
)


updateByPluginOutMsg : Plugins -> Config -> List Plugin.OutMsg -> ( Model key, List Effect ) -> ( Model key, List Effect )
updateByPluginOutMsg plugins uc outMsgs ( mo, effects ) =
let
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Graph.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Decode.Graph045 as Graph045
import Decode.Graph050 as Graph050
import Decode.Graph100 as Graph100
import Dict exposing (Dict)
import Effect exposing (n)
import Effect.Api exposing (Effect(..), getAddressEgonet, getEntityEgonet)
import Effect.Graph exposing (Effect(..))
import Encode.Graph as Encode
Expand Down Expand Up @@ -75,6 +74,7 @@ import Update.Graph.Search as Search
import Update.Graph.Table as Table
import Update.Graph.Tag as Tag
import Update.Graph.Transform as Transform
import Util exposing (n)
import Util.Data as Data
import Util.Graph
import Util.Graph.History as History
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Graph/Browser.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Update.Graph.Browser exposing (filterTable, hideTable, infiniteScroll, lo
import Api.Data
import Config.Graph as Graph
import Dict
import Effect exposing (n)
import Util exposing (n)
import Effect.Api exposing (Effect(..))
import Effect.Graph exposing (Effect(..))
import Init.Graph.Table.AddressNeighborsTable as AddressNeighborsTable
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Graph/Search.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ module Update.Graph.Search exposing (Config, selectCategory, selectCriterion, se

import Api.Data
import Api.Request.Entities
import Effect exposing (n)
import Effect.Api
import Effect.Graph exposing (Effect(..))
import Init.Graph.Search exposing (initCriterion)
import Model.Graph.Id as Id
import Model.Graph.Search exposing (..)
import Msg.Graph exposing (Msg(..))
import Util exposing (n)


type alias Config =
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Locale.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Api.Data
import DateFormat.Language
import DateFormat.Relative
import Dict
import Effect exposing (n)
import Effect.Locale exposing (Effect)
import Languages.German
import Locale.English
Expand All @@ -14,6 +13,7 @@ import Model.Locale exposing (..)
import Msg.Locale exposing (Msg(..))
import Numeral
import Time
import Util exposing (n)


duration : Float
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Pathfinder.elm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Config.Update as Update
import Css.Pathfinder exposing (searchBoxMinWidth)
import Decode.Pathfinder1
import Dict exposing (Dict)
import Effect exposing (and, n)
import Effect.Api as Api exposing (Effect(..))
import Effect.Pathfinder as Pathfinder exposing (Effect(..))
import Hovercard
Expand Down Expand Up @@ -71,6 +70,7 @@ import Update.Pathfinder.TxDetails as TxDetails
import Update.Pathfinder.WorkflowNextTxByTime as WorkflowNextTxByTime
import Update.Pathfinder.WorkflowNextUtxoTx as WorkflowNextUtxoTx
import Update.Search as Search
import Util exposing (and, n)
import Util.Annotations as Annotations
import Util.Data as Data exposing (timestampToPosix)
import Util.Pathfinder.History as History
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Pathfinder/AddressDetails.elm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Update.Pathfinder.AddressDetails exposing (showTransactionsTable, update)
import Basics.Extra exposing (flip)
import Config.DateRangePicker exposing (datePickerSettings)
import Config.Update as Update
import Effect exposing (n)
import Util exposing (n)
import Effect.Api as Api
import Effect.Pathfinder exposing (Effect(..))
import Init.DateRangePicker as DateRangePicker
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Pathfinder/TxDetails.elm
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Update.Pathfinder.TxDetails exposing (update)

import Effect exposing (n)
import Effect.Pathfinder exposing (Effect)
import Model.Pathfinder.TxDetails exposing (Model)
import Msg.Pathfinder exposing (IoDirection(..), TxDetailsMsg(..))
import RecordSetter exposing (s_state)
import Util exposing (n)


update : TxDetailsMsg -> Model -> ( Model, List Effect )
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Pathfinder/WorkflowNextUtxoTx.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Update.Pathfinder.WorkflowNextUtxoTx exposing (loadReferencedTx, update)

import Api.Data
import Effect exposing (n)
import Effect.Api as Api
import Effect.Pathfinder exposing (Effect(..))
import Init.Pathfinder.Id as Id
Expand All @@ -17,6 +16,7 @@ import Set
import Task
import Tuple exposing (pair)
import Update.Pathfinder.Network as Network
import Util exposing (n)


update : WorkflowNextTxContext -> WorkflowNextUtxoTxMsg -> Model -> ( Model, List Effect )
Expand Down
2 changes: 1 addition & 1 deletion src/Update/Search.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module Update.Search exposing (clear, filterByPrefix, maybeTriggerSearch, update

import Api.Data
import Autocomplete
import Effect exposing (n)
import Effect.Search exposing (Effect(..))
import Init.Search exposing (init)
import Model.Search exposing (..)
import Msg.Search exposing (Msg(..))
import Tuple exposing (pair)
import Util exposing (n)


currencyToResult : String -> Api.Data.SearchResult -> ( String, Int ) -> List ResultLine
Expand Down
17 changes: 17 additions & 0 deletions src/Util.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Util exposing (and, n)


n : m -> ( m, List eff )
n m =
( m, [] )


and : (m -> ( m, List eff )) -> ( m, List eff ) -> ( m, List eff )
and update ( m, eff ) =
let
( m2, eff2 ) =
update m
in
( m2
, eff ++ eff2
)
61 changes: 52 additions & 9 deletions src/Util/ThemedSelectBox.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
module Util.ThemedSelectBox exposing (Model, Msg(..), SelectOption, close, empty, fromList, init, mapLabel, select, update, view)
module Util.ThemedSelectBox exposing
( Model
, Msg(..)
, OutMsg(..)
, SelectOption
, close
, empty
, fromList
, init
, mapLabel
, update
, view
)

import Css
import Html.Styled exposing (Html, div)
Expand All @@ -18,6 +30,12 @@ type alias SelectOption =

type Msg
= Select String
| Open
| Close


type OutMsg
= Selected String


type Model
Expand Down Expand Up @@ -61,16 +79,31 @@ close (SelectBox m) =
{ m | open = False } |> SelectBox


select : String -> Model -> Model
select _ (SelectBox m) =
{ m | open = not m.open } |> SelectBox
open : Model -> Model
open (SelectBox m) =
{ m | open = True }
|> SelectBox


select : Model -> Model
select (SelectBox m) =
{ m | open = not m.open }
|> SelectBox

update : Msg -> Model -> Model
update msg m =

update : Msg -> Model -> ( Model, Maybe OutMsg )
update msg model =
case msg of
Select x ->
select x m
( select model
, Selected x |> Just
)

Open ->
( open model, Nothing )

Close ->
( close model, Nothing )


view : Model -> String -> Html Msg
Expand Down Expand Up @@ -132,7 +165,12 @@ view (SelectBox sBox) selected =
]
in
Sc.dropDownOpenWithInstances
Sc.dropDownOpenAttributes
(Sc.dropDownOpenAttributes
|> Rs.s_dropDownOpen
[ Util.View.onClickWithStop Close
, css [ Css.cursor Css.pointer ]
]
)
(Sc.dropDownOpenInstances
|> Rs.s_dropDownListNormal (Just dropDownList)
|> Rs.s_text (Just selectedRow)
Expand All @@ -145,7 +183,12 @@ view (SelectBox sBox) selected =

else
Sc.dropDownClosedWithInstances
Sc.dropDownClosedAttributes
(Sc.dropDownClosedAttributes
|> Rs.s_dropDownClosed
[ Util.View.onClickWithStop Open
, css [ Css.cursor Css.pointer ]
]
)
(Sc.dropDownClosedInstances
|> Rs.s_text (Just selectedRow)
)
Expand Down
12 changes: 7 additions & 5 deletions src/Util/ThemedSelectBoxes.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Util.ThemedSelectBoxes exposing (Model, SelectBoxesAvailable(..), closeAll, get, init, update)

import Dict exposing (Dict)
import Tuple
import Tuple exposing (mapFirst)
import Util.ThemedSelectBox as TSb


Expand Down Expand Up @@ -32,12 +32,14 @@ init lst =
}


update : SelectBoxesAvailable -> TSb.Msg -> Model -> Model
update : SelectBoxesAvailable -> TSb.Msg -> Model -> ( Model, Maybe TSb.OutMsg )
update sb msg m =
get sb m
|> Maybe.map (TSb.update msg)
|> Maybe.map (set sb m)
|> Maybe.withDefault m
|> Maybe.map
(TSb.update msg
>> mapFirst (set sb m)
)
|> Maybe.withDefault ( m, Nothing )


get : SelectBoxesAvailable -> Model -> Maybe TSb.Model
Expand Down

0 comments on commit 7502815

Please sign in to comment.