From b12667977cc454db90fb9cb9f5bd30ed1e94b0a4 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Mon, 16 Nov 2020 10:10:57 -0500 Subject: [PATCH] more getters --- backend/src/api/switch.rs | 6 ++- sina/scripts/build-dev.sh | 2 +- sina/src/Main.elm | 87 ++++++++++++++++++++++++++++++++++---- sina/src/Mi/Switch.elm | 6 ++- sina/src/Mi/WebMention.elm | 2 +- sina/src/Model.elm | 23 +++++++++- sina/src/Page/Index.elm | 35 +++++++++++++-- sina/src/Route.elm | 1 + 8 files changed, 144 insertions(+), 18 deletions(-) diff --git a/backend/src/api/switch.rs b/backend/src/api/switch.rs index 33ebf7c..9a2c120 100644 --- a/backend/src/api/switch.rs +++ b/backend/src/api/switch.rs @@ -13,7 +13,8 @@ use rusty_ulid::generate_ulid_string; #[derive(serde::Serialize)] pub struct FrontChange { 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 ended_at: Option, pub duration: Option, @@ -46,6 +47,7 @@ pub fn list( duration: switch.duration(), id: switch.id, who: member.cmene, + img_url: member.picurl, started_at: switch.started_at, ended_at: switch.ended_at, }) @@ -74,6 +76,7 @@ pub fn current_front(conn: MainDatabase, tok: paseto::Token) -> Result Result< duration: switch.duration(), id: switch.id, who: member.cmene, + img_url: member.picurl, started_at: switch.started_at, ended_at: switch.ended_at, })) diff --git a/sina/scripts/build-dev.sh b/sina/scripts/build-dev.sh index 93f3262..177a64f 100755 --- a/sina/scripts/build-dev.sh +++ b/sina/scripts/build-dev.sh @@ -2,5 +2,5 @@ #! nix-shell -i bash -p elmPackages.elm echo "--------------- rebuilding ------------------" -elm make ./src/Main.elm --output elm.js +elm make ./src/Main.elm --output elm.js --debug echo "------------------ done ---------------------" diff --git a/sina/src/Main.elm b/sina/src/Main.elm index 8aa3af5..6f7a98a 100644 --- a/sina/src/Main.elm +++ b/sina/src/Main.elm @@ -2,10 +2,11 @@ module Main exposing (main) import Browser exposing (Document, UrlRequest(..)) import Browser.Navigation as Nav -import Html exposing (Html, a, br, button, div, h1, img, input, p, pre, span, text) -import Html.Attributes exposing (href, placeholder, src, value) -import Html.Events exposing (onClick, onInput) +import Html exposing (a, p, text) +import Html.Attributes exposing (href) +import Html.Events exposing (onClick) import Http +import Json.Decode import Layout import Mi import Mi.Switch @@ -14,8 +15,8 @@ import Model exposing (Model, Msg(..), init) import Page.Index import Page.Login import Route exposing (Route(..), routeParser) -import Url exposing (Url) -import Url.Parser as UrlParser exposing (()) +import Url +import Url.Parser as UrlParser update : Msg -> Model -> ( Model, Cmd Msg ) @@ -28,15 +29,79 @@ update msg model = ( { model | route = UrlParser.parse routeParser url }, Cmd.none ) 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 , Mi.request "GET" (Maybe.withDefault "" model.token) - Mi.tokenIntrospectURL + (Mi.Switch.idURL id) 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 -> case result of Ok data -> @@ -70,8 +135,14 @@ 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" ] ] + _ -> - Debug.todo "implement routing" + Layout.template "Oh noes" [ p [] [ text "todo: implement this 404 page" ] ] Just why -> Layout.basic diff --git a/sina/src/Mi/Switch.elm b/sina/src/Mi/Switch.elm index e6390a4..fa1ffb7 100644 --- a/sina/src/Mi/Switch.elm +++ b/sina/src/Mi/Switch.elm @@ -1,7 +1,7 @@ module Mi.Switch exposing (Switch, decoder, frontURL, idURL, listURL, switchURL) 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 Url.Builder as UB @@ -12,17 +12,19 @@ type alias Switch = , started_at : Posix , ended_at : Maybe Posix , duration : Maybe Int + , img_url : String } decoder : Decoder Switch decoder = - map5 Switch + map6 Switch (field "id" string) (field "who" string) (field "started_at" Iso8601.decoder) (field "ended_at" (nullable Iso8601.decoder)) (field "duration" (nullable int)) + (field "img_url" string) switchURL : String diff --git a/sina/src/Mi/WebMention.elm b/sina/src/Mi/WebMention.elm index 2efed02..c6f8d54 100644 --- a/sina/src/Mi/WebMention.elm +++ b/sina/src/Mi/WebMention.elm @@ -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 Url.Builder as UB diff --git a/sina/src/Model.elm b/sina/src/Model.elm index 3251a7f..e12e2e6 100644 --- a/sina/src/Model.elm +++ b/sina/src/Model.elm @@ -4,6 +4,8 @@ import Browser exposing (UrlRequest(..)) import Browser.Navigation as Nav import Http import Mi +import Mi.Switch exposing (Switch) +import Mi.WebMention exposing (WebMention) import Route exposing (Route, routeParser) import Url exposing (Url) import Url.Parser as UrlParser @@ -15,6 +17,13 @@ type alias Model = , token : Maybe String , tokenData : Maybe Mi.TokenData , 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 | UpdateToken String | SubmitToken + | FetchSwitch String + | FetchSwitches | ValidateToken (Result Http.Error Mi.TokenData) + | ValidateSwitchByID (Result Http.Error Switch) + | ValidateFront (Result Http.Error Switch) + | ValidateSwitches (Result Http.Error (List Switch)) | ClearError @@ -34,6 +48,13 @@ init _ url key = , token = Nothing , tokenData = Nothing , error = Nothing + , front = Nothing + , switchPage = 1 + , switches = [] + , webMentionPage = 1 + , webMentions = [] + , switchByID = Nothing + , webMentionByID = Nothing } - , Cmd.none + , Nav.pushUrl key "/login" ) diff --git a/sina/src/Page/Index.elm b/sina/src/Page/Index.elm index bf2df83..c18c947 100644 --- a/sina/src/Page/Index.elm +++ b/sina/src/Page/Index.elm @@ -1,20 +1,23 @@ module Page.Index exposing (view) 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 Model exposing (Model) view : Model -> Document msg -view { tokenData } = +view { tokenData, front } = case tokenData of Nothing -> basic "Login Required" [] Just data -> template "Mi" - [ p + ([ h2 [] [ text "Token Info" ] + , p [] [ span [] @@ -28,4 +31,28 @@ view { tokenData } = , 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 -> + [] + ) + ) diff --git a/sina/src/Route.elm b/sina/src/Route.elm index 0f16f1f..e33b8fa 100644 --- a/sina/src/Route.elm +++ b/sina/src/Route.elm @@ -7,6 +7,7 @@ import Url.Parser.Query as Query type Route = Index | Login + | NotFound | System | SwitchLog (Maybe Int) | SwitchID String