summaryrefslogtreecommitdiff
path: root/elm
diff options
context:
space:
mode:
Diffstat (limited to 'elm')
-rw-r--r--elm/Lib/RDate.elm68
-rw-r--r--elm/UList/Opt.elm13
2 files changed, 78 insertions, 3 deletions
diff --git a/elm/Lib/RDate.elm b/elm/Lib/RDate.elm
new file mode 100644
index 00000000..7af8f89f
--- /dev/null
+++ b/elm/Lib/RDate.elm
@@ -0,0 +1,68 @@
+-- Utility module and UI widget for handling release dates.
+--
+-- Release dates are integers with the following format: 0 or yyyymmdd
+-- Special values
+-- 0 -> unknown
+-- 99999999 -> TBA
+-- yyyy9999 -> year known, month & day unknown
+-- yyyymm99 -> year & month known, day unknown
+module Lib.RDate exposing (..)
+
+import Html exposing (..)
+import Html.Attributes exposing (..)
+import Html.Events exposing (..)
+import Date
+
+
+type alias RDate = Int
+
+type alias RDateComp =
+ { y : Int
+ , m : Int
+ , d : Int
+ }
+
+
+expand : RDate -> RDateComp
+expand r =
+ { y = r // 10000
+ , m = modBy 100 (r // 100)
+ , d = modBy 100 r
+ }
+
+
+compact : RDateComp -> RDate
+compact r = r.y * 10000 + r.m * 100 + r.d
+
+
+fromDate : Date.Date -> RDateComp
+fromDate d =
+ { y = Date.year d
+ , m = Date.monthNumber d
+ , d = Date.day d
+ }
+
+
+normalize : RDateComp -> RDateComp
+normalize r =
+ if r.y == 0 then { y = 0, m = 0, d = 0 }
+ else if r.y == 9999 then { y = 9999, m = 99, d = 99 }
+ else if r.m == 99 then { y = r.y, m = 99, d = 99 }
+ else r
+
+
+format : RDateComp -> String
+format date =
+ case (date.y, date.m, date.d) of
+ ( 0, _, _) -> "unknown"
+ (9999, _, _) -> "TBA"
+ ( y, 99, 99) -> String.fromInt y
+ ( y, m, 99) -> String.fromInt y ++ "-" ++ (String.padLeft 2 '0' <| String.fromInt m)
+ ( y, m, d) -> String.fromInt y ++ "-" ++ (String.padLeft 2 '0' <| String.fromInt m) ++ "-" ++ (String.padLeft 2 '0' <| String.fromInt d)
+
+
+display : Date.Date -> RDate -> Html msg
+display today d =
+ let fmt = expand d |> format
+ future = d > (fromDate today |> compact)
+ in if future then b [ class "future" ] [ text fmt ] else text fmt
diff --git a/elm/UList/Opt.elm b/elm/UList/Opt.elm
index 916e723f..cfd84ec6 100644
--- a/elm/UList/Opt.elm
+++ b/elm/UList/Opt.elm
@@ -6,9 +6,11 @@ import Html.Events exposing (..)
import Task
import Process
import Browser
+import Date
import Lib.Util exposing (..)
import Lib.Html exposing (..)
import Lib.Api as Api
+import Lib.RDate as RDate
import Gen.Types as T
import Gen.Api as GApi
import Gen.UListVNOpt as GVO
@@ -17,7 +19,7 @@ import Gen.UListDel as GDE
main : Program GVO.Recv Model Msg
main = Browser.element
- { init = \f -> (init f, Cmd.none)
+ { init = \f -> (init f, Date.today |> Task.perform Today)
, subscriptions = always Sub.none
, view = view
, update = update
@@ -28,6 +30,7 @@ port ulistNotesChanged : String -> Cmd msg
type alias Model =
{ flags : GVO.Recv
+ , today : Date.Date
, del : Bool
, delState : Api.State
, notes : String
@@ -38,6 +41,7 @@ type alias Model =
init : GVO.Recv -> Model
init f =
{ flags = f
+ , today = Date.fromOrdinalDate 2100 1
, del = False
, delState = Api.Normal
, notes = f.notes
@@ -46,7 +50,8 @@ init f =
}
type Msg
- = Del Bool
+ = Today Date.Date
+ | Del Bool
| Delete
| Deleted GApi.Response
| Notes String
@@ -57,6 +62,8 @@ type Msg
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
+ Today d -> ({ model | today = d }, Cmd.none)
+
Del b -> ({ model | del = b }, Cmd.none)
Delete -> ({ model | delState = Api.Loading }, Api.post "/u/ulist/del.json" (GDE.encode { uid = model.flags.uid, vid = model.flags.vid }) Deleted)
Deleted GApi.Success -> (model, ulistVNDeleted True)
@@ -106,7 +113,7 @@ view model =
then td [ class "tco1" ] [ a [ href "#" ] [ text "remove" ] ]
else text ""
, td [ class "tco2" ] [ text <| Maybe.withDefault "status" <| lookup r.status T.rlistStatus ]
- , td [ class "tco3" ] [ text "2018-11-10" ]
+ , td [ class "tco3" ] [ RDate.display model.today r.released ]
, td [ class "tco4" ] <| List.map langIcon r.lang ++ [ releaseTypeIcon r.rtype ]
, td [ class "tco5" ] [ a [ href ("/r"++String.fromInt r.id), title r.original ] [ text r.title ] ]
]