Fix CSS and continuous state in frontend

This commit is contained in:
Michael Soukup 2021-05-10 01:30:26 +02:00
parent e338f76535
commit 281522f227
2 changed files with 63 additions and 22 deletions

View File

@ -6,18 +6,23 @@
<title>{{ title }}</title> <title>{{ title }}</title>
<script src="/static/main.js"></script> <script src="/static/main.js"></script>
<style> <style>
* { body {
padding: 0; text-align:center;
margin: 0;
} }
#main { #container {
display: grid; display: flex;
height: 100%; flex-direction: column;
margin: auto auto;
height: 95vh;
width: 90vh;
} }
img { #container div {
align-self: center;
}
#container img {
align-self: center;
object-fit: cover;
max-width: 100%; max-width: 100%;
max-height: 100vh;
margin: auto;
} }
</style> </style>
</head> </head>
@ -32,7 +37,9 @@
</ul> </ul>
{% endif %} {% endif %}
{% endwith %} {% endwith %}
<div id="main"></div> <div id="container">
<div id="main"></div>
</div>
<script> <script>
var app = Elm.Main.init({ var app = Elm.Main.init({
node: document.getElementById('main') node: document.getElementById('main')

View File

@ -2,8 +2,7 @@ module Main exposing (main)
import Browser import Browser
import Html exposing (..) import Html exposing (..)
import Html.Attributes exposing (..) import Html.Attributes exposing (src)
import Html.Events exposing (..)
import Http import Http
import Time import Time
import Json.Decode exposing (Decoder, map3, field, string, float) import Json.Decode exposing (Decoder, map3, field, string, float)
@ -31,13 +30,12 @@ type alias Surprise =
type Model type Model
= Failure = Failure
| Loading
| Success Surprise | Success Surprise
init : () -> (Model, Cmd Msg) init : () -> (Model, Cmd Msg)
init _ = init _ =
(Loading, getSurprise) (Success { diff = 0.0, surprise = "", asset = "/static/loading.svg" }, getSurprise)
-- UPDATE -- UPDATE
@ -51,7 +49,7 @@ update : Msg -> Model -> (Model, Cmd Msg)
update msg model = update msg model =
case msg of case msg of
FetchSurprise _ -> FetchSurprise _ ->
(Loading, getSurprise) (model, getSurprise)
GotSurprise result -> GotSurprise result ->
case result of case result of
@ -66,7 +64,7 @@ update msg model =
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions model = subscriptions model =
Time.every 1000 FetchSurprise Time.every 980 FetchSurprise
-- VIEW -- VIEW
@ -77,14 +75,50 @@ view model =
Failure -> Failure ->
div [] [ text "Failed to fetch from server." ] div [] [ text "Failed to fetch from server." ]
Loading ->
img [ src "/static/loading.svg" ] []
Success surprise -> Success surprise ->
div [] div []
[ div [] [ text (String.fromInt (round surprise.diff)) ] ( (viewDiff surprise.diff)
, img [ src surprise.asset ] [] ++ [ img [ src surprise.asset ] [] ]
] )
viewDiff: Float -> List (Html Msg)
viewDiff diff =
diff
|> round
|> max 0
|> secondsToIntervals
|> List.filter (\(i, _) -> i /= 0)
|> List.map formatInterval
|> List.map (\s -> h2 [] [ text s ])
formatInterval: (Int, String) -> String
formatInterval (n, unit) =
let
s =
(String.fromInt n) ++ " " ++ unit
in
if n > 1 then
s ++ "s"
else
s
secondsToIntervals: Int -> List (Int, String)
secondsToIntervals diff =
let
(days, daysRem) =
(diff // 86400, modBy 86400 diff)
(hours, hoursRem) =
(daysRem // 3600, modBy 3600 daysRem)
(minutes, seconds) =
(hoursRem // 60, modBy 60 hoursRem)
in
[ (days, "day")
, (hours, "hour")
, (minutes, "minute")
, (seconds, "second")
]
-- HTTP -- HTTP