Use Maybe.withDefault some more. Do not show anything before finishing loading data.

This commit is contained in:
Vidir Valberg Gudmundsson 2017-07-23 18:30:27 +02:00
parent c237d4ecc7
commit 974694bd5f
8 changed files with 86 additions and 86 deletions

View file

@ -60,13 +60,10 @@ eventDecoder =
dateDecoder : Decoder Date dateDecoder : Decoder Date
dateDecoder = dateDecoder =
let let
unpacked x = unpacked isoString =
case Date.Extra.fromIsoString x of isoString
Just value -> |> Date.Extra.fromIsoString
value |> Maybe.withDefault (Date.Extra.fromParts 1970 Jan 1 0 0 0 0)
Nothing ->
Date.Extra.fromParts 1970 Jan 1 0 0 0 0
in in
Json.Decode.map unpacked string Json.Decode.map unpacked string
@ -108,7 +105,7 @@ eventTypeDecoder =
|> required "light_text" bool |> required "light_text" bool
initDataDecoder : Decoder (Flags -> Filter -> Location -> Route -> Model) initDataDecoder : Decoder (Flags -> Filter -> Location -> Route -> Bool -> Model)
initDataDecoder = initDataDecoder =
decode Model decode Model
|> required "days" (list dayDecoder) |> required "days" (list dayDecoder)

View file

@ -12,7 +12,6 @@ import Views exposing (view)
-- External modules -- External modules
import Html.Lazy exposing (lazy)
import WebSocket exposing (listen) import WebSocket exposing (listen)
import Navigation exposing (Location) import Navigation exposing (Location)
@ -22,7 +21,7 @@ main =
Navigation.programWithFlags Navigation.programWithFlags
OnLocationChange OnLocationChange
{ init = init { init = init
, view = lazy view , view = view
, update = update , update = update
, subscriptions = subscriptions , subscriptions = subscriptions
} }
@ -32,13 +31,13 @@ init : Flags -> Location -> ( Model, Cmd Msg )
init flags location = init flags location =
let let
currentRoute = currentRoute =
parseLocation (Debug.log "location" location) parseLocation location
emptyFilter = emptyFilter =
Filter [] [] [] Filter [] [] []
model = model =
Model [] [] [] [] [] flags emptyFilter location currentRoute Model [] [] [] [] [] flags emptyFilter location currentRoute False
in in
model ! [ sendInitMessage flags.camp_slug flags.websocket_server ] model ! [ sendInitMessage flags.camp_slug flags.websocket_server ]

View file

@ -28,6 +28,7 @@ type alias Model =
, filter : Filter , filter : Filter
, location : Location , location : Location
, route : Route , route : Route
, dataLoaded : Bool
} }

View file

@ -41,9 +41,5 @@ matchers =
parseLocation : Location -> Route parseLocation : Location -> Route
parseLocation location = parseLocation location =
case parseHash matchers location of parseHash matchers location
Just route -> |> Maybe.withDefault NotFoundRoute
route
Nothing ->
NotFoundRoute

View file

@ -34,7 +34,7 @@ update msg model =
"init" -> "init" ->
case Json.Decode.decodeString initDataDecoder str of case Json.Decode.decodeString initDataDecoder str of
Ok m -> Ok m ->
m model.flags model.filter model.location model.route m model.flags model.filter model.location model.route True
Err error -> Err error ->
model model

View file

@ -23,31 +23,34 @@ import Date.Extra
view : Model -> Html Msg view : Model -> Html Msg
view model = view model =
div [] case model.dataLoaded of
[ dayPicker model True ->
, hr [] [] div []
, case model.route of [ dayPicker model
OverviewRoute -> , hr [] []
scheduleOverviewView model , case model.route of
OverviewRoute ->
scheduleOverviewView model
OverviewFilteredRoute _ -> OverviewFilteredRoute _ ->
scheduleOverviewView model scheduleOverviewView model
DayRoute dayIso -> DayRoute dayIso ->
let let
day = day =
case (List.head (List.filter (\x -> (Date.Extra.toFormattedString "y-MM-dd" x.date) == dayIso) model.days)) of model.days
Just day -> |> List.filter (\x -> (Date.Extra.toFormattedString "y-MM-dd" x.date) == dayIso)
day |> List.head
|> Maybe.withDefault (Day "" (Date.Extra.fromParts 1970 Jan 1 0 0 0 0) "")
in
dayView day model
Nothing -> EventRoute eventSlug ->
Day "" (Date.Extra.fromParts 1970 Jan 1 0 0 0 0) "" eventDetailView eventSlug model
in
dayView day model
EventRoute eventSlug -> NotFoundRoute ->
eventDetailView eventSlug model div [] [ text "Not found!" ]
]
NotFoundRoute -> False ->
div [] [ text "Not found!" ] h4 [] [ text "Loading schedule..." ]
]

View file

@ -146,24 +146,19 @@ renderGroup offset group =
List.map findLefts sortedGroup List.map findLefts sortedGroup
numberInGroup = numberInGroup =
case List.maximum (List.map (\( _, left ) -> left) lefts) of lefts
Just num -> |> List.map (\( _, left ) -> left)
num |> List.maximum
|> Maybe.withDefault 1
Nothing ->
1
fixedLefts = fixedLefts =
if numberInGroup == 0 then if numberInGroup == 0 then
List.map List.map
(\( instance, x ) -> (\( instance, x ) ->
( instance ( instance
, case List.Extra.elemIndex ( instance, x ) lefts of , lefts
Just index -> |> List.Extra.elemIndex ( instance, x )
index |> Maybe.withDefault 0
Nothing ->
0
) )
) )
lefts lefts
@ -171,12 +166,10 @@ renderGroup offset group =
lefts lefts
fixedNumberInGroup = fixedNumberInGroup =
case List.maximum (List.map (\( _, left ) -> left) fixedLefts) of fixedLefts
Just num -> |> List.map (\( _, left ) -> left)
num |> List.maximum
|> Maybe.withDefault 1
Nothing ->
1
in in
div div
[ style [ style

View file

@ -19,35 +19,46 @@ eventDetailView : EventSlug -> Model -> Html Msg
eventDetailView eventSlug model = eventDetailView eventSlug model =
let let
event = event =
case List.head (List.filter (\e -> e.slug == eventSlug) model.events) of model.events
Just event -> |> List.filter (\e -> e.slug == eventSlug)
event |> List.head
Nothing ->
{ title = "", slug = "", abstract = "", speakers = [], videoRecording = False, videoUrl = "" }
in in
div [ class "row" ] case event of
[ div [ class "col-sm-9" ] Just event ->
[ a [ onClick BackInHistory, classList [ ( "btn", True ), ( "btn-default", True ) ] ] div [ class "row" ]
[ i [ classList [ ( "fa", True ), ( "fa-chevron-left", True ) ] ] [] [ div [ class "col-sm-9" ]
, text " Back" [ a [ onClick BackInHistory, classList [ ( "btn", True ), ( "btn-default", True ) ] ]
[ i [ classList [ ( "fa", True ), ( "fa-chevron-left", True ) ] ] []
, text " Back"
]
, h4 [] [ text event.title ]
, p [] [ Markdown.toHtml [] event.abstract ]
, hr [] []
, eventInstancesList eventSlug model.eventInstances
]
, div
[ classList
[ ( "col-sm-3", True )
, ( "schedule-sidebar", True )
, ( "sticky", True )
]
]
[ videoRecordingSidebar event
, speakerSidebar event.speakers
]
] ]
, h4 [] [ text event.title ]
, p [] [ Markdown.toHtml [] event.abstract ] Nothing ->
, hr [] [] div [ class "row" ]
, eventInstancesList eventSlug model.eventInstances [ text
] (case model.dataLoaded of
, div True ->
[ classList "Event not found."
[ ( "col-sm-3", True )
, ( "schedule-sidebar", True ) False ->
, ( "sticky", True ) "Loading..."
)
] ]
]
[ videoRecordingSidebar event
, speakerSidebar event.speakers
]
]
videoRecordingSidebar : Event -> Html Msg videoRecordingSidebar : Event -> Html Msg