diff options
author | Yorhel <git@yorhel.nl> | 2019-07-25 14:30:04 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2019-07-25 14:36:21 +0200 |
commit | f296495a912ce759df11c43e78b4552788bdbff2 (patch) | |
tree | 0c10802de65fb7c8475722e12234bff5eb980628 /elm3/RelEdit/Producers.elm | |
parent | 0f3cfeb85caec6424bcbea47142eefbf8011636b (diff) |
Merge the v3 branch into separate namespace + fix Docker stuff (again)
I was getting tired of having to keep two branches up-to-date with the
latest developments, so decided to throw v3 into the same branch - just
different files (...which will get mostly rewritten again soon). The two
versions aren't very different in terms of dependencies, build system
and support code, so they can now properly share files. Added a section
to the README to avoid confusion.
This merge also makes it easier to quickly switch between the different
versions, which is handy for development. It's even possible to run both
at the same time, but my scripts use the same port so that needs a
workaround.
And it's amazing how often I break the Docker scripts.
Diffstat (limited to 'elm3/RelEdit/Producers.elm')
-rw-r--r-- | elm3/RelEdit/Producers.elm | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/elm3/RelEdit/Producers.elm b/elm3/RelEdit/Producers.elm new file mode 100644 index 00000000..e546e223 --- /dev/null +++ b/elm3/RelEdit/Producers.elm @@ -0,0 +1,93 @@ +module RelEdit.Producers exposing (Model, Msg, init, update, view) + +import Html exposing (..) +import Html.Attributes exposing (..) +import Html.Events exposing (..) +import Lib.Html exposing (..) +import Lib.Autocomplete as A +import Lib.Gen exposing (RelEditProducers) +import Lib.Util exposing (..) +import Lib.Api exposing (Producer) + + +type alias Model = + { producers : List RelEditProducers + , search : A.Model Producer + , duplicates : Bool + } + + +init : List RelEditProducers -> Model +init l = + { producers = l + , search = A.init + , duplicates = False + } + + +type Msg + = Del Int + | SetRole Int String + | Search (A.Msg Producer) + + +searchConfig : A.Config Msg Producer +searchConfig = { wrap = Search, id = "add-producer", source = A.producerSource } + + +validate : Model -> Model +validate model = { model | duplicates = hasDuplicates <| List.map .pid model.producers } + + +update : Msg -> Model -> (Model, Cmd Msg) +update msg model = + case msg of + Del i -> (validate { model | producers = delidx i model.producers }, Cmd.none) + SetRole i s -> (validate { model | producers = modidx i (\e -> { e | developer = s == "d" || s == "b", publisher = s == "p" || s == "b" }) model.producers } + , Cmd.none ) + + Search m -> + let (nm, c, res) = A.update searchConfig m model.search + in case res of + Nothing -> ({ model | search = nm }, c) + Just r -> + let nrow = { pid = r.id, name = r.name, developer = False, publisher = True } + in (validate { model | search = A.clear nm, producers = model.producers ++ [nrow] }, c) + + + +view : Model -> Html Msg +view model = + let + role e = + case (e.developer, e.publisher) of + (True, False) -> "d" + (False, True) -> "p" + _ -> "b" + + roles = + [ ("d", "Developer") + , ("p", "Publisher") + , ("b", "Both") + ] + + entry n e = editListRow "" + [ editListField 1 "col-form-label single-line" + [ a [href <| "/p" ++ String.fromInt e.pid, title e.name, target "_blank" ] [text e.name ] ] + , editListField 1 "" + [ inputSelect [onInput (SetRole n)] (role e) roles ] + , editListField 0 "" [ removeButton (Del n) ] + ] + + in cardRow "Producers" Nothing + <| editList (List.indexedMap entry model.producers) + ++ formGroups ( + (if model.duplicates + then [ [ div [ class "invalid-feedback" ] + [ text "The producers list contains duplicates." ] ] ] + else [] + ) ++ + [ label [for "add-producer"] [text "Add producer"] + :: A.view searchConfig model.search [placeholder "Producer", style "max-width" "400px"] + ] + ) |