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)]
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<NaiveDateTime>,
pub duration: Option<i32>,
@ -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<Json<Fron
duration: switch.duration(),
id: switch.id,
who: member.cmene,
img_url: member.picurl,
started_at: switch.started_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(),
id: switch.id,
who: member.cmene,
img_url: member.picurl,
started_at: switch.started_at,
ended_at: switch.ended_at,
}))

View File

@ -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 ---------------------"

View File

@ -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

View File

@ -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

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 Url.Builder as UB

View File

@ -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"
)

View File

@ -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 ->
[]
)
)

View File

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