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

307 lines
8.5 KiB
Elm
Raw Normal View History

2021-04-19 23:31:11 +00:00
module Pages.App_String exposing (Model, Msg, Params, page)
2021-04-19 23:50:06 +00:00
import Regex
2021-04-23 17:06:06 +00:00
import Html exposing (Html, button, div, h2, h5, img, text, ul, li, a, p, span, i)
2021-04-19 23:31:11 +00:00
import Html.Attributes exposing (src, style, class, alt, href)
import Html.Events exposing (onClick)
import Http
import Markdown
import Json.Decode as Decode
import Json.Decode.Extra as Decode exposing (andMap)
2021-04-19 23:31:11 +00:00
import Spa.Document exposing (Document)
import Spa.Generated.Route as Route
2021-04-19 23:31:11 +00:00
import Spa.Page as Page exposing (Page)
import Spa.Url as Url exposing (Url)
2021-04-19 23:50:06 +00:00
-- INIT
2021-04-19 23:31:11 +00:00
page : Page Params Model Msg
page =
Page.element
{ init = init
, update = update
, view = view
, subscriptions = subscriptions
}
type alias Params =
{ app : String
}
type alias App =
{ name : String
, category : String
, repository : Maybe String
, versions : Maybe (List String)
, icon : Maybe String
, status : String
, slug : String
, default_branch : String
, website : Maybe String
2021-04-19 23:31:11 +00:00
}
type alias Model =
{ url : Url Params
, status : Status
, readme : String
}
type Status
= Failure
| Loading
| Success App
init : Url Params -> ( Model, Cmd Msg )
init url =
( { url = url, status = Loading, readme = "" }, loadApp )
2021-04-19 23:31:11 +00:00
default_image : String
2021-04-19 23:50:06 +00:00
-- FIXME: change to absolute URL, if this works?
2021-04-24 13:36:56 +00:00
default_image = "/logo.png"
2021-04-19 23:31:11 +00:00
-- UPDATE
type Msg
= MorePlease
| GotApps (Result Http.Error (List App))
| GotText (Result Http.Error String)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
MorePlease ->
( { model | status = Loading }, loadApp )
2021-04-19 23:31:11 +00:00
GotApps result ->
case result of
Ok apps ->
let
2021-04-19 23:50:06 +00:00
-- TODO better way of getting a single app?
apps_filtered = List.filter (\app -> app.slug == model.url.params.app) apps
2021-04-19 23:31:11 +00:00
in
case List.head apps_filtered of
Nothing ->
( { model | status = Failure }, Cmd.none )
2021-04-19 23:31:11 +00:00
Just item ->
( { model | status = Success (item) }, loadREADME item)
2021-04-19 23:31:11 +00:00
2021-04-25 13:25:25 +00:00
Err err ->
let
_ =
Debug.log "Something failed" err
in
( { model | status = Failure } , Cmd.none )
2021-04-19 23:31:11 +00:00
GotText result ->
case result of
Ok content ->
2021-04-19 23:50:06 +00:00
-- update model.content with the loaded README
let
-- remove HTML comments
pattern = "<!--.*-->"
maybeRegex = Regex.fromString pattern
regex = Maybe.withDefault Regex.never maybeRegex
in
( { model | readme = Regex.replace regex (\_ -> "") content }, Cmd.none )
2021-04-19 23:31:11 +00:00
2021-04-25 13:25:25 +00:00
Err err ->
let
_ =
Debug.log "Something failed" err
in
( { model | status = Failure } , Cmd.none )
2021-04-19 23:31:11 +00:00
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Document Msg
view model =
2021-04-23 17:06:06 +00:00
{ title = title model
2021-04-19 23:31:11 +00:00
, body = [ body model ]
}
2021-04-23 17:06:06 +00:00
title : Model -> String
title model =
case model.status of
Loading ->
"loading - abra apps"
Failure ->
"error - abra apps"
Success app ->
app.name ++ " - abra apps"
2021-04-19 23:31:11 +00:00
body : Model -> Html Msg
body model =
div [ class "pt-3" ]
[ case model.status of
Failure ->
2021-04-24 13:36: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-19 23:31:11 +00:00
]
Loading ->
text "Loading..."
Success app ->
div []
[ div [ class "row" ]
[ viewApp app model.readme ]
]
]
viewStatusBadge : App -> Html Msg
viewStatusBadge app =
let
status_class =
case app.status of
"1" ->
"badge-success"
"2" ->
"badge-info"
"3" ->
"badge-warning"
"4" ->
"badge-danger"
_ ->
"badge-dark"
in
span [ class ("card-link badge " ++ status_class) ]
[ text ("Score: " ++ app.status) ]
viewApp : App -> String -> Html Msg
viewApp app readme =
let
icon_url =
case app.icon of
Just "" ->
default_image
Just i ->
i
Nothing ->
default_image
repository_link =
case app.repository of
2021-04-23 17:06:06 +00:00
Just link ->
a [ class "card-link", href link ]
[ i [ class "fab fa-git-alt" ] []
, text "code" ]
2021-04-19 23:31:11 +00:00
Nothing ->
text ""
website_link =
case app.website of
Just link ->
case link of
"" ->
text ""
_ ->
a [ class "card-link", href link ]
[ i [ class "fas fa-home" ] []
, text "homepage" ]
Nothing ->
text ""
2021-04-19 23:31:11 +00:00
in
div [ class "col-md-6 col-sm-10 mb-3 offset-md-3 offset-sm-1" ]
[ div [ class "card" ]
[ div [ class "card-header" ]
[ a
[ class "btn btn-sm border border-secondary card-link"
, href (Route.toString Route.Top)]
[ text " back" ]
, span [ class "card-link badge badge-secondary" ] [ text app.category ]
2021-04-19 23:31:11 +00:00
, viewStatusBadge app
, repository_link
, website_link
2021-04-19 23:31:11 +00:00
]
, img [ class "card-img-top", src icon_url, alt ("icon for " ++ app.name) ] []
, div [ class "card-body" ]
2021-04-19 23:50:06 +00:00
-- render Markdown with no special options
2021-04-19 23:31:11 +00:00
[ div [] (Markdown.toHtml Nothing readme)
]
, div [ class "card-footer" ]
[]
]
]
-- HTTP
loadApp : Cmd Msg
loadApp =
Http.get
2021-04-25 13:25:25 +00:00
{ url = "/abra-apps.json"
, expect = Http.expectJson GotApps appListDecoder }
loadREADME : App -> Cmd Msg
loadREADME app =
let
repository_link =
case app.repository of
Just link ->
a [ class "card-link", href link ]
[ i [ class "fab fa-git-alt" ] []
, text "code" ]
Nothing ->
text ""
in
2021-04-25 13:25:25 +00:00
Http.get
-- FIXME use live Gitea link
{ url = "https://cors-container.herokuapp.com/https://git.autonomic.zone/coop-cloud/" ++ app.slug ++ "/raw/branch/" ++ app.default_branch ++ "/README.md"
, expect = Http.expectString GotText }
2021-04-19 23:31:11 +00:00
2021-04-25 13:25:25 +00:00
featuresDecoder : Decode.Decoder String
2021-04-19 23:31:11 +00:00
featuresDecoder =
2021-04-19 23:50:06 +00:00
-- get features.status if it's there
2021-04-19 23:31:11 +00:00
(Decode.oneOf
[ Decode.at [ "status" ] Decode.string
, Decode.succeed ""
]
)
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)
2021-04-25 13:25:25 +00:00
|> andMap (Decode.succeed "")
|> andMap (Decode.field "default_branch" Decode.string)
|> andMap (Decode.maybe (Decode.field "website" Decode.string))
2021-04-19 23:31:11 +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