I have made my first app in Elm, a lookup of Github users.
You type the name of the user into the search bar and the user's username and profile image - which has an anchor linked to their profile - appears below.
Here is my code:
module Main exposing (..)
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
main =
App.beginnerProgram { model = model, view = view, update = update }
-- MODEL
type alias Model =
{ username : String }
model : Model
model =
{ username = "" }
-- UPDATE
type Msg
= Change String
update : Msg -> Model -> Model
update msg model =
case msg of
Change newUsername ->
{ model | username = newUsername }
-- VIEW
view : Model -> Html Msg
view model =
div
[ style
[ ( "display", "flex" )
, ( "justify-content", "center" )
, ( "align-items", "center" )
, ( "flex-direction", "column" )
]
]
[ input
[ placeholder "Search for a Github username"
, onInput Change
, style
[ ( "font-size", "30px" )
, ( "padding", "10px" )
, ( "width", "600px" )
]
]
[]
, div []
[ h5 [] [ text model.username ]
]
, a [ href ("https://github.com/" ++ model.username) ]
[ img
[ src ("https://github.com/" ++ model.username ++ ".png")
, style [ ( "max-width", "200px" ) ]
]
[]
]
]
Here is a demo of it in action: https://alanbuchanan.github.io/GithubUserLookup.html
I used elm-format
to format the code, and based the architecture from the documentation.
- The code returned in the view is messy. How can I tidy my code so that the styles applied do not take up space inside the main
div
of the view, perhaps instead within a variable beforehand? - How else can I optimise this code?