mi-v1/frontend/src/Pages/Switch.elm

265 lines
7.8 KiB
Elm
Raw Normal View History

2020-01-18 19:39:48 +00:00
module Pages.Switch exposing (Model, Msg, page)
import Element exposing (..)
import Element.Font as Font
import Element.Input as Input
import Generated.Params as Params
import Global
2020-01-18 20:18:11 +00:00
import Html
import Html.Attributes
2020-01-18 19:39:48 +00:00
import Http
import Iso8601
import Json.Decode
import Mi
import Mi.SwitchData
import Spa.Page
import Utils.Spa exposing (Page, PageContext)
page : Page Params.Switch Model Msg model msg appMsg
page =
Spa.Page.component
{ title = always "Switch Data"
, init = init
, update = update
, subscriptions = always subscriptions
, view = always view
}
-- INIT
type alias Model =
{ currentFront : Maybe Mi.SwitchData.Switch
, frontData : Maybe (List Mi.SwitchData.Switch)
, error : Maybe String
, amount : Int
, page : Int
}
init : PageContext -> Params.Switch -> ( Model, Cmd Msg, Cmd Global.Msg )
init { global } _ =
let
model =
{ currentFront = Nothing
, frontData = Nothing
, error = Nothing
, amount = 40
, page = 0
}
in
( model
, Cmd.batch
[ Mi.request
"GET"
(Maybe.withDefault
""
global.token
)
Mi.SwitchData.frontURL
Http.emptyBody
(Mi.expectJson ValidateCurrentFront Mi.SwitchData.decoder)
, Mi.request
"GET"
(Maybe.withDefault
""
global.token
)
(Mi.SwitchData.listURL model.amount model.page)
Http.emptyBody
(Mi.expectJson ValidateFrontData (Json.Decode.list Mi.SwitchData.decoder))
]
, Cmd.none
)
-- UPDATE
type Msg
= ValidateCurrentFront (Result Http.Error Mi.SwitchData.Switch)
| ValidateFrontData (Result Http.Error (List Mi.SwitchData.Switch))
| NextPage
| PrevPage
update : PageContext -> Msg -> Model -> ( Model, Cmd Msg, Cmd Global.Msg )
update { global } msg model =
case msg of
PrevPage ->
( { model | page = model.page - 1, frontData = Nothing }
, Mi.request
"GET"
(Maybe.withDefault
""
global.token
)
(Mi.SwitchData.listURL model.amount (model.page - 1))
Http.emptyBody
(Mi.expectJson ValidateFrontData (Json.Decode.list Mi.SwitchData.decoder))
, Cmd.none
)
NextPage ->
( { model | page = model.page + 1, frontData = Nothing }
, Mi.request
"GET"
(Maybe.withDefault
""
global.token
)
(Mi.SwitchData.listURL model.amount (model.page + 1))
Http.emptyBody
(Mi.expectJson ValidateFrontData (Json.Decode.list Mi.SwitchData.decoder))
, Cmd.none
)
ValidateCurrentFront result ->
case result of
Ok data ->
( { model | currentFront = Just data }
, Cmd.none
, Cmd.none
)
Err _ ->
( { model | error = Just "can't fetch current front" }
, Cmd.none
, Cmd.none
)
ValidateFrontData result ->
case result of
Ok data ->
( { model | frontData = Just data }
, Cmd.none
, Cmd.none
)
Err _ ->
( { model | error = Just "can't fetch historical front data" }
, Cmd.none
, Cmd.none
)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Element Msg
view model =
let
currentFront =
case model.currentFront of
Just data ->
let
startTime =
Iso8601.fromTime data.started_at
in
column
[]
2020-01-18 20:18:11 +00:00
[ row []
[ html
(Html.img
[ Html.Attributes.src ("/static/img/" ++ String.toLower data.who ++ ".png")
, Html.Attributes.width 64
, Html.Attributes.height 64
]
[]
)
, el [ Font.size 30 ] (text data.who)
]
2020-01-18 19:39:48 +00:00
, text ("started at " ++ startTime)
]
Nothing ->
text "Loading..."
prevButton =
if model.page /= 0 then
Input.button [] { onPress = Just PrevPage, label = text "Prev" }
else
none
nextButton =
Input.button [] { onPress = Just NextPage, label = text "Next" }
tableButtons =
2020-01-18 20:18:11 +00:00
row [] [ prevButton, text "-", nextButton ]
2020-01-18 19:39:48 +00:00
switchDataTable =
case model.frontData of
Nothing ->
case model.currentFront of
Nothing ->
none
Just _ ->
text "Loading..."
Just data ->
column []
[ table []
{ data = data
, columns =
[ { header = text "ID"
, width = fill
, view =
\switch ->
link
[ Font.underline
, Font.color (rgb255 204 75 75)
, mouseOver [ alpha 0.5 ]
]
{ label = text (String.slice 0 10 switch.id)
2020-01-18 20:18:11 +00:00
, url = "/switch/id/" ++ switch.id
2020-01-18 19:39:48 +00:00
}
}
, { header = text "Who"
, width = fill
, view =
\switch -> text switch.who
}
, { header = text "Start"
, width = fill
, view =
\switch -> el [ padding 4 ] (text (Iso8601.fromTime switch.started_at))
}
, { header = text "End"
, width = fill
, view =
\switch ->
case switch.ended_at of
Nothing ->
none
Just time ->
el [ padding 4 ] (text (Iso8601.fromTime time))
}
]
}
, tableButtons
]
in
column
[]
[ currentFront
, el [ padding 15 ] none
, switchDataTable
]