recipes.coopcloud.tech/src/Pages/Top.elm

246 lines
6.3 KiB
Elm
Raw Normal View History

2021-04-17 21:42:28 +00:00
module Pages.Top exposing (Model, Msg, Params, page)
2021-04-19 23:31:11 +00:00
import Html exposing (Html, button, div, h2, h5, img, text, ul, li, a, p, span, i)
2021-04-18 10:47:50 +00:00
import Html.Attributes exposing (src, style, class, alt, href)
2021-04-17 21:42:28 +00:00
import Html.Events exposing (onClick)
import Http
import Maybe exposing (withDefault)
2021-04-17 21:42:28 +00:00
import Json.Decode as Decode
import Json.Decode.Extra as Decode exposing (andMap)
2021-04-19 23:31:11 +00:00
import Spa.Generated.Route as Route
2021-04-17 21:42:28 +00:00
import Spa.Document exposing (Document)
import Spa.Page as Page exposing (Page)
import Spa.Url as Url exposing (Url)
2021-04-24 13:36:56 +00:00
import Util exposing (by, andThen, Direction(..))
2021-04-17 21:42:28 +00:00
page : Page Params Model Msg
page =
Page.element
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
-- INIT
type alias Params =
()
type alias App =
{ name : String
, category : String
, repository : Maybe String
, versions : Maybe (List String)
2021-04-18 10:47:50 +00:00
, icon : Maybe String
2021-04-25 13:37:32 +00:00
, status : Int
, slug : String
, default_branch : String
, website : Maybe String
, description : Maybe String
2021-04-17 21:42:28 +00:00
}
type Model
= Failure
| Loading
| Success (List App)
init : Url Params -> ( Model, Cmd Msg )
init { params } =
( Loading, loadApps )
2021-04-18 10:47:50 +00:00
default_image : String
2021-04-24 13:36:56 +00:00
default_image = "/logo.png"
2021-04-18 10:47:50 +00:00
2021-04-17 21:42:28 +00:00
-- UPDATE
type Msg
= MorePlease
| GotApps (Result Http.Error (List App))
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
MorePlease ->
( Loading, loadApps )
GotApps result ->
case result of
Ok apps ->
( Success apps, Cmd.none )
Err _ ->
( Failure, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Document Msg
view model =
2021-04-18 10:47:50 +00:00
{ title = "abra apps"
2021-04-17 21:42:28 +00:00
, body = [ body model ]
}
body : Model -> Html Msg
body model =
2021-04-18 10:47:50 +00:00
div [ class "pt-3" ]
2021-04-17 21:42:28 +00:00
[ viewApps model
]
2021-04-19 23:31:11 +00:00
viewStatusBadge : App -> Html Msg
viewStatusBadge app =
2021-04-18 10:47:50 +00:00
let
status_class =
case app.status of
2021-04-25 13:37:32 +00:00
1 ->
2021-04-18 10:47:50 +00:00
"badge-success"
2021-04-25 13:37:32 +00:00
2 ->
2021-04-18 10:47:50 +00:00
"badge-info"
2021-04-25 13:37:32 +00:00
3 ->
2021-04-18 10:47:50 +00:00
"badge-warning"
2021-04-25 13:37:32 +00:00
4 ->
2021-04-18 10:47:50 +00:00
"badge-danger"
_ ->
"badge-dark"
in
2021-04-25 13:25:25 +00:00
span [ class ("card-link badge " ++ status_class) ]
2021-04-25 13:37:32 +00:00
[ text ("Score: " ++ String.fromInt app.status) ]
2021-04-18 10:47:50 +00:00
2021-04-19 23:31:11 +00:00
viewApp : App -> Html Msg
viewApp app =
2021-04-18 10:47:50 +00:00
let
icon_url =
case app.icon of
Just "" ->
default_image
Just i ->
i
Nothing ->
default_image
2021-04-25 13:25:25 +00:00
repository_link =
2021-04-18 10:47:50 +00:00
case app.repository of
2021-04-19 23:31:11 +00:00
Just link ->
2021-04-25 13:25:25 +00:00
a [ class "card-link", href link ]
[
i [ class "fab fa-git-alt" ] []
, text "code"
2021-04-19 23:31:11 +00:00
]
2021-04-18 10:47:50 +00:00
Nothing ->
text ""
2021-04-25 13:25:25 +00:00
website_link =
case app.website of
Just link ->
case link of
"" ->
text ""
_ ->
2021-04-25 13:25:25 +00:00
a [ class "card-link", href link ]
[ i [ class "fas fa-home" ] []
, text "homepage" ]
Nothing ->
text ""
app_href = Route.toString <| Route.App_String { app = app.slug }
2021-04-18 10:47:50 +00:00
in
2021-04-25 13:25:25 +00:00
div [ class "col-md-4 mb-3 col-sm-12" ]
2021-04-18 10:47:50 +00:00
[ div [ class "card" ]
[ img [ class "card-img-top", src icon_url, alt ("icon for " ++ app.name) ] []
, div [ class "card-body" ]
2021-04-25 13:25:25 +00:00
[ h5 [ class "card-title" ]
[ a [ href app_href ] [ text app.name ] ]
, p [] [ text (withDefault "" app.description) ]
2021-04-23 17:06:06 +00:00
, repository_link
, website_link
2021-04-25 13:25:25 +00:00
, a [ class "card-link", href app_href ]
[ i [ class "fas fa-book" ] []
2021-04-23 17:06:06 +00:00
, text "docs" ]
]
2021-04-18 10:47:50 +00:00
, div [ class "card-footer" ]
[ span [ class "card-link badge badge-secondary" ] [ text app.category ]
2021-04-19 23:31:11 +00:00
, viewStatusBadge app
2021-04-17 21:42:28 +00:00
]
]
]
2021-04-24 13:36:56 +00:00
2021-04-17 21:42:28 +00:00
viewApps : Model -> Html Msg
viewApps model =
case model of
Failure ->
2021-04-24 14:12:56 +00:00
div []
[ div [ class "alert alert-danger" ]
[ p [] [ text "Unable to load app data" ]
, button [ class "btn btn-danger", onClick MorePlease ] [ text "Try Again!" ]
]
2021-04-17 21:42:28 +00:00
]
Loading ->
text "Loading..."
Success apps ->
div []
[ div [ class "row" ]
2021-04-25 13:25:25 +00:00
(List.map viewApp (apps |> List.sortWith
(by .status ASC
2021-04-25 16:43:09 +00:00
|> andThen (String.toLower << .name) ASC))
2021-04-24 13:36:56 +00:00
)
2021-04-17 21:42:28 +00:00
]
-- HTTP
loadApps : Cmd Msg
loadApps =
Http.get
2021-04-25 13:25:25 +00:00
{ url = "/abra-apps.json"
2021-04-17 21:42:28 +00:00
, expect = Http.expectJson GotApps appListDecoder
}
2021-04-18 10:47:50 +00:00
featuresDecoder =
(Decode.oneOf
2021-04-25 13:37:32 +00:00
[ Decode.at [ "status" ] Decode.int
, Decode.succeed 5
2021-04-18 10:47:50 +00:00
]
)
2021-04-17 21:42:28 +00:00
appDecoder : Decode.Decoder App
appDecoder =
Decode.succeed App
|> andMap (Decode.field "name" Decode.string)
|> andMap (Decode.field "category" Decode.string)
|> andMap (Decode.maybe (Decode.field "repository" Decode.string))
|> andMap (Decode.succeed Nothing)
|> andMap (Decode.maybe (Decode.field "icon" Decode.string))
|> andMap (Decode.at [ "features" ] featuresDecoder)
|> andMap (Decode.succeed "")
|> andMap (Decode.field "default_branch" Decode.string)
|> andMap (Decode.maybe (Decode.field "website" Decode.string))
|> andMap (Decode.maybe (Decode.field "description" Decode.string))
2021-04-17 21:42:28 +00:00
2021-04-25 13:25:25 +00:00
2021-04-17 21:42:28 +00:00
appListDecoder : Decode.Decoder (List App)
appListDecoder =
2021-04-25 13:25:25 +00:00
Decode.keyValuePairs appDecoder
|> Decode.map buildApp
buildApp : List (String, App) -> (List App)
buildApp apps =
List.map (\(slug, app) -> { app | slug = slug}) apps