From 974694bd5f3a46fa19ef29c4997fc43595b8913c Mon Sep 17 00:00:00 2001 From: Vidir Valberg Gudmundsson Date: Sun, 23 Jul 2017 18:30:27 +0200 Subject: [PATCH] Use Maybe.withDefault some more. Do not show anything before finishing loading data. --- schedule/src/Decoders.elm | 13 +++--- schedule/src/Main.elm | 7 ++-- schedule/src/Models.elm | 1 + schedule/src/Routing.elm | 8 +--- schedule/src/Update.elm | 2 +- schedule/src/Views.elm | 49 ++++++++++++----------- schedule/src/Views/DayView.elm | 29 ++++++-------- schedule/src/Views/EventDetail.elm | 63 ++++++++++++++++++------------ 8 files changed, 86 insertions(+), 86 deletions(-) diff --git a/schedule/src/Decoders.elm b/schedule/src/Decoders.elm index f0433b49..85b7108e 100644 --- a/schedule/src/Decoders.elm +++ b/schedule/src/Decoders.elm @@ -60,13 +60,10 @@ eventDecoder = dateDecoder : Decoder Date dateDecoder = let - unpacked x = - case Date.Extra.fromIsoString x of - Just value -> - value - - Nothing -> - Date.Extra.fromParts 1970 Jan 1 0 0 0 0 + unpacked isoString = + isoString + |> Date.Extra.fromIsoString + |> Maybe.withDefault (Date.Extra.fromParts 1970 Jan 1 0 0 0 0) in Json.Decode.map unpacked string @@ -108,7 +105,7 @@ eventTypeDecoder = |> required "light_text" bool -initDataDecoder : Decoder (Flags -> Filter -> Location -> Route -> Model) +initDataDecoder : Decoder (Flags -> Filter -> Location -> Route -> Bool -> Model) initDataDecoder = decode Model |> required "days" (list dayDecoder) diff --git a/schedule/src/Main.elm b/schedule/src/Main.elm index 3a8ab0d5..67d7af90 100644 --- a/schedule/src/Main.elm +++ b/schedule/src/Main.elm @@ -12,7 +12,6 @@ import Views exposing (view) -- External modules -import Html.Lazy exposing (lazy) import WebSocket exposing (listen) import Navigation exposing (Location) @@ -22,7 +21,7 @@ main = Navigation.programWithFlags OnLocationChange { init = init - , view = lazy view + , view = view , update = update , subscriptions = subscriptions } @@ -32,13 +31,13 @@ init : Flags -> Location -> ( Model, Cmd Msg ) init flags location = let currentRoute = - parseLocation (Debug.log "location" location) + parseLocation location emptyFilter = Filter [] [] [] model = - Model [] [] [] [] [] flags emptyFilter location currentRoute + Model [] [] [] [] [] flags emptyFilter location currentRoute False in model ! [ sendInitMessage flags.camp_slug flags.websocket_server ] diff --git a/schedule/src/Models.elm b/schedule/src/Models.elm index 233f96f4..b2df690c 100644 --- a/schedule/src/Models.elm +++ b/schedule/src/Models.elm @@ -28,6 +28,7 @@ type alias Model = , filter : Filter , location : Location , route : Route + , dataLoaded : Bool } diff --git a/schedule/src/Routing.elm b/schedule/src/Routing.elm index 17417bf3..0bbe9642 100644 --- a/schedule/src/Routing.elm +++ b/schedule/src/Routing.elm @@ -41,9 +41,5 @@ matchers = parseLocation : Location -> Route parseLocation location = - case parseHash matchers location of - Just route -> - route - - Nothing -> - NotFoundRoute + parseHash matchers location + |> Maybe.withDefault NotFoundRoute diff --git a/schedule/src/Update.elm b/schedule/src/Update.elm index d9bea033..d52293dc 100644 --- a/schedule/src/Update.elm +++ b/schedule/src/Update.elm @@ -34,7 +34,7 @@ update msg model = "init" -> case Json.Decode.decodeString initDataDecoder str of Ok m -> - m model.flags model.filter model.location model.route + m model.flags model.filter model.location model.route True Err error -> model diff --git a/schedule/src/Views.elm b/schedule/src/Views.elm index cff006a1..84d66332 100644 --- a/schedule/src/Views.elm +++ b/schedule/src/Views.elm @@ -23,31 +23,34 @@ import Date.Extra view : Model -> Html Msg view model = - div [] - [ dayPicker model - , hr [] [] - , case model.route of - OverviewRoute -> - scheduleOverviewView model + case model.dataLoaded of + True -> + div [] + [ dayPicker model + , hr [] [] + , case model.route of + OverviewRoute -> + scheduleOverviewView model - OverviewFilteredRoute _ -> - scheduleOverviewView model + OverviewFilteredRoute _ -> + scheduleOverviewView model - DayRoute dayIso -> - let - day = - case (List.head (List.filter (\x -> (Date.Extra.toFormattedString "y-MM-dd" x.date) == dayIso) model.days)) of - Just day -> - day + DayRoute dayIso -> + let + day = + model.days + |> List.filter (\x -> (Date.Extra.toFormattedString "y-MM-dd" x.date) == dayIso) + |> List.head + |> Maybe.withDefault (Day "" (Date.Extra.fromParts 1970 Jan 1 0 0 0 0) "") + in + dayView day model - Nothing -> - Day "" (Date.Extra.fromParts 1970 Jan 1 0 0 0 0) "" - in - dayView day model + EventRoute eventSlug -> + eventDetailView eventSlug model - EventRoute eventSlug -> - eventDetailView eventSlug model + NotFoundRoute -> + div [] [ text "Not found!" ] + ] - NotFoundRoute -> - div [] [ text "Not found!" ] - ] + False -> + h4 [] [ text "Loading schedule..." ] diff --git a/schedule/src/Views/DayView.elm b/schedule/src/Views/DayView.elm index 77d15ec3..b2fc099c 100644 --- a/schedule/src/Views/DayView.elm +++ b/schedule/src/Views/DayView.elm @@ -146,24 +146,19 @@ renderGroup offset group = List.map findLefts sortedGroup numberInGroup = - case List.maximum (List.map (\( _, left ) -> left) lefts) of - Just num -> - num - - Nothing -> - 1 + lefts + |> List.map (\( _, left ) -> left) + |> List.maximum + |> Maybe.withDefault 1 fixedLefts = if numberInGroup == 0 then List.map (\( instance, x ) -> ( instance - , case List.Extra.elemIndex ( instance, x ) lefts of - Just index -> - index - - Nothing -> - 0 + , lefts + |> List.Extra.elemIndex ( instance, x ) + |> Maybe.withDefault 0 ) ) lefts @@ -171,12 +166,10 @@ renderGroup offset group = lefts fixedNumberInGroup = - case List.maximum (List.map (\( _, left ) -> left) fixedLefts) of - Just num -> - num - - Nothing -> - 1 + fixedLefts + |> List.map (\( _, left ) -> left) + |> List.maximum + |> Maybe.withDefault 1 in div [ style diff --git a/schedule/src/Views/EventDetail.elm b/schedule/src/Views/EventDetail.elm index 9f231514..0ddf5e17 100644 --- a/schedule/src/Views/EventDetail.elm +++ b/schedule/src/Views/EventDetail.elm @@ -19,35 +19,46 @@ eventDetailView : EventSlug -> Model -> Html Msg eventDetailView eventSlug model = let event = - case List.head (List.filter (\e -> e.slug == eventSlug) model.events) of - Just event -> - event - - Nothing -> - { title = "", slug = "", abstract = "", speakers = [], videoRecording = False, videoUrl = "" } + model.events + |> List.filter (\e -> e.slug == eventSlug) + |> List.head in - div [ class "row" ] - [ div [ class "col-sm-9" ] - [ a [ onClick BackInHistory, classList [ ( "btn", True ), ( "btn-default", True ) ] ] - [ i [ classList [ ( "fa", True ), ( "fa-chevron-left", True ) ] ] [] - , text " Back" + case event of + Just event -> + div [ class "row" ] + [ div [ class "col-sm-9" ] + [ 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 ] - , hr [] [] - , eventInstancesList eventSlug model.eventInstances - ] - , div - [ classList - [ ( "col-sm-3", True ) - , ( "schedule-sidebar", True ) - , ( "sticky", True ) + + Nothing -> + div [ class "row" ] + [ text + (case model.dataLoaded of + True -> + "Event not found." + + False -> + "Loading..." + ) ] - ] - [ videoRecordingSidebar event - , speakerSidebar event.speakers - ] - ] videoRecordingSidebar : Event -> Html Msg