yagcdn/frontend/src/Main.elm
Valentin Brandl 49f02750b4
Improvements
2019-07-28 21:42:49 +02:00

103 lines
2.3 KiB
Elm

module Main exposing (Model, Msg(..), init, main, update, view)
import Browser
import Data exposing (Url, hostname, toHost, toUrl)
import Html exposing (Html, br, div, h1, input, table, td, text, tr)
import Html.Attributes exposing (disabled, placeholder, style, value)
import Html.Events exposing (onInput)
import Parse exposing (parseUrl)
import Ribbon exposing (ribbon)
type Msg
= UrlChange String
type alias Model =
{ url : String
, parsed : Maybe Url
}
init : Model
init =
{ url = ""
, parsed = Nothing
}
update : Msg -> Model -> Model
update msg state =
case msg of
UrlChange newUrl ->
{ state | url = newUrl, parsed = parseUrl newUrl }
renderUrl : Url -> Html msg
renderUrl { prov, user, repo, file } =
div myStyle
[ table myStyle
[ tr myStyle
[ td myStyle [ text "host" ]
, td myStyle [ text (toHost prov) ]
]
, tr []
[ td myStyle [ text "user" ]
, td myStyle [ text user ]
]
, tr myStyle
[ td myStyle [ text "repo" ]
, td myStyle [ text repo ]
]
, tr myStyle
[ td myStyle [ text "file" ]
, td myStyle [ text file ]
]
]
]
renderMUrl : Maybe Url -> Html msg
renderMUrl mUrl =
mUrl
|> Maybe.map renderUrl
|> Maybe.withDefault (div myStyle [ text "Parse Error" ])
displayMUrl : Maybe Url -> String
displayMUrl mUrl =
mUrl
|> Maybe.map toUrl
|> Maybe.withDefault (hostname ++ "<service>/<user>/<repo>/<gitref>/<file>")
myStyle : List (Html.Attribute msg)
myStyle =
[ style "width" "100%" ]
myStyle2 : List (Html.Attribute msg) -> List (Html.Attribute msg)
myStyle2 =
List.append myStyle
view : Model -> Html Msg
view state =
div myStyle
[ h1 []
[ text "Gitache" ]
, ribbon
, input (myStyle2 [ placeholder "URL to parse", value state.url, onInput UrlChange ])
[]
, text
"Parsed URL: "
, br [] []
, renderMUrl state.parsed
, input (myStyle2 [ disabled True, value (displayMUrl state.parsed) ]) []
]
main : Program () Model Msg
main =
Browser.sandbox { init = init, update = update, view = view }