Skip to content

Instantly share code, notes, and snippets.

@karlosmid
Created October 5, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlosmid/25b6ac47320dcfbf64461717fdb96aa4 to your computer and use it in GitHub Desktop.
Save karlosmid/25b6ac47320dcfbf64461717fdb96aa4 to your computer and use it in GitHub Desktop.
Pichare application refactored using type alias
module Picshare exposing (main)
import Browser
import Html exposing (..)
import Html.Events exposing (onClick)
import Html.Attributes exposing (class, src)
-- 1. type alias for our record model
type alias Model =
{ url : String
, caption : String
, liked : Bool
}
-- 2. use alias
main : Program () Model Msg
main =
Browser.sandbox
{ init = initialModel
, view = view
, update = update
}
-- 3. use alias
view : Model -> Html Msg
view model =
div []
[
div [ class "header" ]
[ h1 [] [text "Picshare"] ],
div [ class "content-flow"]
[ viewDetailedPhoto model]
]
type Msg
= Like
| Unlike
-- 4. use alias
viewDetailedPhoto : Model -> Html Msg
viewDetailedPhoto model =
let
buttonClass = --
if model.liked then
"fa-heart"
else
"fa-heart-o"
msg = --
if model.liked then
Unlike
else
Like
in
div [ class "detaile-photo" ]
[
img [ src model.url ] []
, div [ class "photo-info"]
[ div [ class "like-button" ]
[ i --
[ class "fa fa-2x" --
, class buttonClass --
, onClick msg --
]
[]
]
, h2 [ class "caption" ] [ text model.caption]
]
]
update :
-- 5. use alias
Msg
-> Model
-> Model
update msg model =
case msg of
Like ->
{ model | liked = True }
Unlike ->
{ model | liked = False }
baseUrl : String
baseUrl = "https://www.hps.hr/files/data/"
-- 6. use alias
initialModel : Model
initialModel =
{ url = baseUrl ++ "27/kuce.folder/planinarska-kuca-picelj.jpg"
, caption = "Picelj Park Near Zabok"
, liked = False
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment