From 60949953279608f0bb16eee306d0e4c66a427a63 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 16 Nov 2020 11:26:03 -0500 Subject: [PATCH] implement switches page --- backend/src/api/switch.rs | 8 +-- sina/src/Main.elm | 26 ++++--- sina/src/Model.elm | 3 +- sina/src/Page/Switches.elm | 143 +++++++++++++++++++++++++++++++++++++ sina/src/Route.elm | 15 ++-- 5 files changed, 172 insertions(+), 23 deletions(-) create mode 100644 sina/src/Page/Switches.elm diff --git a/backend/src/api/switch.rs b/backend/src/api/switch.rs index 9a2c120..b1f4c4d 100644 --- a/backend/src/api/switch.rs +++ b/backend/src/api/switch.rs @@ -77,8 +77,8 @@ pub fn current_front(conn: MainDatabase, tok: paseto::Token) -> Result Err(Error::NotFound), } @@ -185,7 +185,7 @@ pub fn get(tok: paseto::Token, conn: MainDatabase, switch_id: String) -> Result< id: switch.id, who: member.cmene, img_url: member.picurl, - started_at: switch.started_at, - ended_at: switch.ended_at, + started_at: switch.started_at.round_subsecs(0), + ended_at: switch.ended_at.map(|time| time.round_subsecs(0)), })) } diff --git a/sina/src/Main.elm b/sina/src/Main.elm index 80dcbeb..2ef9b96 100644 --- a/sina/src/Main.elm +++ b/sina/src/Main.elm @@ -14,6 +14,7 @@ import Mi.WebMention import Model exposing (Model, Msg(..), init) import Page.Index import Page.Login +import Page.Switches import Route exposing (Route(..), routeParser) import Url import Url.Parser as UrlParser @@ -46,7 +47,7 @@ update msg model = , Mi.request "GET" (Maybe.withDefault "" model.token) - (Mi.Switch.listURL 30 model.switchPage) + (Mi.Switch.listURL 40 model.switchPage) Http.emptyBody (Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder)) ] @@ -62,12 +63,22 @@ update msg model = (Mi.expectJson ValidateSwitchByID Mi.Switch.decoder) ) - FetchSwitches -> - ( model + NextSwitchesPage -> + ( { model | switchPage = model.switchPage + 1 } , Mi.request "GET" (Maybe.withDefault "" model.token) - (Mi.Switch.listURL 30 model.switchPage) + (Mi.Switch.listURL 40 <| model.switchPage + 1) + Http.emptyBody + (Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder)) + ) + + PrevSwitchesPage -> + ( { model | switchPage = model.switchPage - 1 } + , Mi.request + "GET" + (Maybe.withDefault "" model.token) + (Mi.Switch.listURL 40 <| model.switchPage - 1) Http.emptyBody (Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder)) ) @@ -129,11 +140,8 @@ view model = Login -> Page.Login.view model - NotFound -> - Layout.template "Oh noes" [ p [] [ text "todo: implement this 404 page" ] ] - - System -> - Layout.template "System Info" [ p [] [ text "TODO(Ashe): implement this page" ] ] + SwitchLog -> + Page.Switches.view model _ -> Layout.template "Oh noes" [ p [] [ text "todo: implement this 404 page" ] ] diff --git a/sina/src/Model.elm b/sina/src/Model.elm index e12e2e6..d4f4519 100644 --- a/sina/src/Model.elm +++ b/sina/src/Model.elm @@ -33,7 +33,8 @@ type Msg | UpdateToken String | SubmitToken | FetchSwitch String - | FetchSwitches + | NextSwitchesPage + | PrevSwitchesPage | ValidateToken (Result Http.Error Mi.TokenData) | ValidateSwitchByID (Result Http.Error Switch) | ValidateFront (Result Http.Error Switch) diff --git a/sina/src/Page/Switches.elm b/sina/src/Page/Switches.elm new file mode 100644 index 0000000..9dbc1bb --- /dev/null +++ b/sina/src/Page/Switches.elm @@ -0,0 +1,143 @@ +module Page.Switches exposing (view) + +import Browser exposing (Document) +import Html exposing (a, br, button, h2, img, p, span, table, td, text, th, tr) +import Html.Attributes exposing (height, href, src, width) +import Html.Events exposing (onClick) +import Iso8601 +import Layout exposing (template) +import Model exposing (Model, Msg(..)) +import Time exposing (Month(..), utc) + + +view : Model -> Document Msg +view { front, switches, switchPage } = + let + frontInfo = + case front of + Just front_data -> + [ text "All times are in UTC." + , h2 [] [ text "Current Front" ] + , p + [] + [ text "Name: " + , text front_data.who + , br [] [] + , text "Started At: " + , text <| Iso8601.fromTime front_data.started_at + , br [] [] + , img [ src front_data.img_url, width 64, height 64 ] [] + ] + ] + + Nothing -> + [] + + heading = + tr + [] + [ th [] [] + , th [] [ text "ID" ] + , th [] [ text "Who" ] + , th [] [ text "Start" ] + , th [] [ text "End" ] + ] + + formatMonth month = + case month of + Jan -> + "Jan" + + Feb -> + "Feb" + + Mar -> + "Mar" + + Apr -> + "Apr" + + May -> + "May" + + Jun -> + "Jun" + + Jul -> + "Jul" + + Aug -> + "Aug" + + Sep -> + "Sep" + + Oct -> + "Oct" + + Nov -> + "Nov" + + Dec -> + "Dec" + + format time = + span + [] + [ text <| String.pad 2 '0' <| String.fromInt <| Time.toHour utc time + , text ":" + , text <| String.pad 2 '0' <| String.fromInt <| Time.toMinute utc time + , text " " + , text <| formatMonth <| Time.toMonth utc time + , text " " + , text <| String.fromInt <| Time.toDay utc time + , text " " + , text <| String.fromInt <| Time.toYear utc time + ] + + rowify data = + let + ended_at = + case data.ended_at of + Nothing -> + span [] [] + + Just time -> + format time + in + tr + [] + [ td [] [ img [ src data.img_url, width 16, height 16 ] [] ] + , td [] [ a [ href <| "/switches/" ++ data.id ] [ text <| String.slice 0 10 data.id ] ] + , td [] [ text data.who ] + , td [] [ format data.started_at ] + , td [] [ ended_at ] + ] + + contents = + List.map rowify switches + + prevButton = + if switchPage > 1 then + button [ onClick PrevSwitchesPage ] [ text "<-" ] + + else + span [] [] + + nextButton = + button [ onClick NextSwitchesPage ] [ text "->" ] + + body = + [ table [] <| [ heading ] ++ contents + , p + [] + [ prevButton + , text <| String.fromInt switchPage + , nextButton + ] + ] + in + Layout.template "Switches" + (frontInfo + ++ body + ) diff --git a/sina/src/Route.elm b/sina/src/Route.elm index e33b8fa..6519e47 100644 --- a/sina/src/Route.elm +++ b/sina/src/Route.elm @@ -1,18 +1,16 @@ -module Route exposing (..) +module Route exposing (Route(..), routeParser) -import Url.Parser exposing ((), (), Parser, int, map, oneOf, s, string) -import Url.Parser.Query as Query +import Url.Parser exposing ((), Parser, map, oneOf, s, string) type Route = Index | Login | NotFound - | System - | SwitchLog (Maybe Int) + | SwitchLog | SwitchID String | MakeSwitch - | WebMentionLog (Maybe Int) + | WebMentionLog | WebMentionID String @@ -21,10 +19,9 @@ routeParser = oneOf [ map Index (s "") , map Login (s "login") - , map System (s "system") - , map SwitchLog (s "switches" Query.int "page") + , map SwitchLog (s "switches") , map SwitchID (s "switches" string) , map MakeSwitch (s "switches" s "log") - , map WebMentionLog (s "webmentions" Query.int "page") + , map WebMentionLog (s "webmentions") , map WebMentionID (s "webmentions" string) ]