implement switches page
This commit is contained in:
parent
4c33f41a5d
commit
6094995327
|
@ -77,8 +77,8 @@ pub fn current_front(conn: MainDatabase, tok: paseto::Token) -> Result<Json<Fron
|
|||
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)),
|
||||
})),
|
||||
None => 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)),
|
||||
}))
|
||||
}
|
||||
|
|
|
@ -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" ] ]
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
)
|
|
@ -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)
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue