more getters

This commit is contained in:
Cadey Ratio 2020-11-16 10:10:57 -05:00
parent 2145fe8b2a
commit b12667977c
8 changed files with 144 additions and 18 deletions

View File

@ -13,7 +13,8 @@ use rusty_ulid::generate_ulid_string;
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
pub struct FrontChange { pub struct FrontChange {
pub id: String, pub id: String,
pub who: String, // models::Member.name pub who: String, // models::Member.name
pub img_url: String, // models::Member.picurl
pub started_at: NaiveDateTime, pub started_at: NaiveDateTime,
pub ended_at: Option<NaiveDateTime>, pub ended_at: Option<NaiveDateTime>,
pub duration: Option<i32>, pub duration: Option<i32>,
@ -46,6 +47,7 @@ pub fn list(
duration: switch.duration(), duration: switch.duration(),
id: switch.id, id: switch.id,
who: member.cmene, who: member.cmene,
img_url: member.picurl,
started_at: switch.started_at, started_at: switch.started_at,
ended_at: switch.ended_at, ended_at: switch.ended_at,
}) })
@ -74,6 +76,7 @@ pub fn current_front(conn: MainDatabase, tok: paseto::Token) -> Result<Json<Fron
duration: switch.duration(), duration: switch.duration(),
id: switch.id, id: switch.id,
who: member.cmene, who: member.cmene,
img_url: member.picurl,
started_at: switch.started_at, started_at: switch.started_at,
ended_at: switch.ended_at, ended_at: switch.ended_at,
})), })),
@ -181,6 +184,7 @@ pub fn get(tok: paseto::Token, conn: MainDatabase, switch_id: String) -> Result<
duration: switch.duration(), duration: switch.duration(),
id: switch.id, id: switch.id,
who: member.cmene, who: member.cmene,
img_url: member.picurl,
started_at: switch.started_at, started_at: switch.started_at,
ended_at: switch.ended_at, ended_at: switch.ended_at,
})) }))

View File

@ -2,5 +2,5 @@
#! nix-shell -i bash -p elmPackages.elm #! nix-shell -i bash -p elmPackages.elm
echo "--------------- rebuilding ------------------" echo "--------------- rebuilding ------------------"
elm make ./src/Main.elm --output elm.js elm make ./src/Main.elm --output elm.js --debug
echo "------------------ done ---------------------" echo "------------------ done ---------------------"

View File

@ -2,10 +2,11 @@ module Main exposing (main)
import Browser exposing (Document, UrlRequest(..)) import Browser exposing (Document, UrlRequest(..))
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Html exposing (Html, a, br, button, div, h1, img, input, p, pre, span, text) import Html exposing (a, p, text)
import Html.Attributes exposing (href, placeholder, src, value) import Html.Attributes exposing (href)
import Html.Events exposing (onClick, onInput) import Html.Events exposing (onClick)
import Http import Http
import Json.Decode
import Layout import Layout
import Mi import Mi
import Mi.Switch import Mi.Switch
@ -14,8 +15,8 @@ import Model exposing (Model, Msg(..), init)
import Page.Index import Page.Index
import Page.Login import Page.Login
import Route exposing (Route(..), routeParser) import Route exposing (Route(..), routeParser)
import Url exposing (Url) import Url
import Url.Parser as UrlParser exposing ((</>)) import Url.Parser as UrlParser
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
@ -28,15 +29,79 @@ update msg model =
( { model | route = UrlParser.parse routeParser url }, Cmd.none ) ( { model | route = UrlParser.parse routeParser url }, Cmd.none )
SubmitToken -> SubmitToken ->
( model
, Cmd.batch
[ Mi.request
"GET"
(Maybe.withDefault "" model.token)
Mi.tokenIntrospectURL
Http.emptyBody
(Mi.expectJson ValidateToken Mi.tokenDecoder)
, Mi.request
"GET"
(Maybe.withDefault "" model.token)
Mi.Switch.frontURL
Http.emptyBody
(Mi.expectJson ValidateFront Mi.Switch.decoder)
, Mi.request
"GET"
(Maybe.withDefault
""
model.token
)
(Mi.Switch.listURL 30 model.switchPage)
Http.emptyBody
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
]
)
FetchSwitch id ->
( model ( model
, Mi.request , Mi.request
"GET" "GET"
(Maybe.withDefault "" model.token) (Maybe.withDefault "" model.token)
Mi.tokenIntrospectURL (Mi.Switch.idURL id)
Http.emptyBody Http.emptyBody
(Mi.expectJson ValidateToken Mi.tokenDecoder) (Mi.expectJson ValidateSwitchByID Mi.Switch.decoder)
) )
FetchSwitches ->
( model
, Mi.request
"GET"
(Maybe.withDefault
""
model.token
)
(Mi.Switch.listURL 30 model.switchPage)
Http.emptyBody
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
)
ValidateSwitchByID result ->
case result of
Ok data ->
( { model | switchByID = Just data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateSwitches result ->
case result of
Ok data ->
( { model | switches = data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateFront result ->
case result of
Ok data ->
( { model | front = Just data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateToken result -> ValidateToken result ->
case result of case result of
Ok data -> Ok data ->
@ -70,8 +135,14 @@ view model =
Login -> Login ->
Page.Login.view model 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" ] ]
_ -> _ ->
Debug.todo "implement routing" Layout.template "Oh noes" [ p [] [ text "todo: implement this 404 page" ] ]
Just why -> Just why ->
Layout.basic Layout.basic

View File

@ -1,7 +1,7 @@
module Mi.Switch exposing (Switch, decoder, frontURL, idURL, listURL, switchURL) module Mi.Switch exposing (Switch, decoder, frontURL, idURL, listURL, switchURL)
import Iso8601 import Iso8601
import Json.Decode exposing (Decoder, field, int, map5, nullable, string) import Json.Decode exposing (Decoder, field, int, map6, nullable, string)
import Time exposing (Posix) import Time exposing (Posix)
import Url.Builder as UB import Url.Builder as UB
@ -12,17 +12,19 @@ type alias Switch =
, started_at : Posix , started_at : Posix
, ended_at : Maybe Posix , ended_at : Maybe Posix
, duration : Maybe Int , duration : Maybe Int
, img_url : String
} }
decoder : Decoder Switch decoder : Decoder Switch
decoder = decoder =
map5 Switch map6 Switch
(field "id" string) (field "id" string)
(field "who" string) (field "who" string)
(field "started_at" Iso8601.decoder) (field "started_at" Iso8601.decoder)
(field "ended_at" (nullable Iso8601.decoder)) (field "ended_at" (nullable Iso8601.decoder))
(field "duration" (nullable int)) (field "duration" (nullable int))
(field "img_url" string)
switchURL : String switchURL : String

View File

@ -1,4 +1,4 @@
module Mi.WebMention exposing (decoder, idURL, listURL) module Mi.WebMention exposing (WebMention, decoder, idURL, listURL)
import Json.Decode exposing (Decoder, field, map3, string) import Json.Decode exposing (Decoder, field, map3, string)
import Url.Builder as UB import Url.Builder as UB

View File

@ -4,6 +4,8 @@ import Browser exposing (UrlRequest(..))
import Browser.Navigation as Nav import Browser.Navigation as Nav
import Http import Http
import Mi import Mi
import Mi.Switch exposing (Switch)
import Mi.WebMention exposing (WebMention)
import Route exposing (Route, routeParser) import Route exposing (Route, routeParser)
import Url exposing (Url) import Url exposing (Url)
import Url.Parser as UrlParser import Url.Parser as UrlParser
@ -15,6 +17,13 @@ type alias Model =
, token : Maybe String , token : Maybe String
, tokenData : Maybe Mi.TokenData , tokenData : Maybe Mi.TokenData
, error : Maybe String , error : Maybe String
, front : Maybe Switch
, switchPage : Int
, switches : List Switch
, webMentionPage : Int
, webMentions : List WebMention
, switchByID : Maybe Switch
, webMentionByID : Maybe WebMention
} }
@ -23,7 +32,12 @@ type Msg
| ClickLink UrlRequest | ClickLink UrlRequest
| UpdateToken String | UpdateToken String
| SubmitToken | SubmitToken
| FetchSwitch String
| FetchSwitches
| ValidateToken (Result Http.Error Mi.TokenData) | ValidateToken (Result Http.Error Mi.TokenData)
| ValidateSwitchByID (Result Http.Error Switch)
| ValidateFront (Result Http.Error Switch)
| ValidateSwitches (Result Http.Error (List Switch))
| ClearError | ClearError
@ -34,6 +48,13 @@ init _ url key =
, token = Nothing , token = Nothing
, tokenData = Nothing , tokenData = Nothing
, error = Nothing , error = Nothing
, front = Nothing
, switchPage = 1
, switches = []
, webMentionPage = 1
, webMentions = []
, switchByID = Nothing
, webMentionByID = Nothing
} }
, Cmd.none , Nav.pushUrl key "/login"
) )

View File

@ -1,20 +1,23 @@
module Page.Index exposing (view) module Page.Index exposing (view)
import Browser exposing (Document) import Browser exposing (Document)
import Html exposing (br, p, span, text) import Html exposing (br, h2, img, p, s, span, text)
import Html.Attributes exposing (height, src, width)
import Iso8601
import Layout exposing (basic, template) import Layout exposing (basic, template)
import Model exposing (Model) import Model exposing (Model)
view : Model -> Document msg view : Model -> Document msg
view { tokenData } = view { tokenData, front } =
case tokenData of case tokenData of
Nothing -> Nothing ->
basic "Login Required" [] basic "Login Required" []
Just data -> Just data ->
template "Mi" template "Mi"
[ p ([ h2 [] [ text "Token Info" ]
, p
[] []
[ span [ span
[] []
@ -28,4 +31,28 @@ view { tokenData } =
, text data.iss , text data.iss
] ]
] ]
] ]
++ (case front of
Just front_data ->
[ h2 [] [ text "Current Front" ]
, span
[]
[ 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 ->
[]
)
)

View File

@ -7,6 +7,7 @@ import Url.Parser.Query as Query
type Route type Route
= Index = Index
| Login | Login
| NotFound
| System | System
| SwitchLog (Maybe Int) | SwitchLog (Maybe Int)
| SwitchID String | SwitchID String