Fix links, change sort, add Utils

This commit is contained in:
3wc 2021-04-24 15:36:56 +02:00
parent c5cb5ee8c2
commit d6d420ae87
3 changed files with 64 additions and 10 deletions

View File

@ -63,7 +63,7 @@ init url =
default_image : String default_image : String
-- FIXME: change to absolute URL, if this works? -- FIXME: change to absolute URL, if this works?
default_image = "http://localhost:8000/logo.png" default_image = "/logo.png"
-- UPDATE -- UPDATE
@ -146,9 +146,11 @@ body model =
div [ class "pt-3" ] div [ class "pt-3" ]
[ case model.status of [ case model.status of
Failure -> Failure ->
div [ ] div []
[ text "I could not load a random cat for some reason. " [ div [ class "alert alert-danger" ]
, button [ onClick MorePlease ] [ text "Try Again!" ] [ p [] [ text "Unable to load app data" ]
, button [ class "btn btn-danger", onClick MorePlease ] [ text "Try Again!" ]
]
] ]
Loading -> Loading ->
@ -242,7 +244,7 @@ loadApp =
-- fetch app JSON and README in parallel -- fetch app JSON and README in parallel
Http.get Http.get
-- FIXME: change to absolute URL, if this works? -- FIXME: change to absolute URL, if this works?
{ url = "http://localhost:8000/abra-apps-list.json" { url = "/abra-apps-list.json"
, expect = Http.expectJson GotApps appListDecoder } , expect = Http.expectJson GotApps appListDecoder }
@ -259,7 +261,7 @@ loadREADME app =
text "" text ""
in in
Http.get Http.get
-- FIXME add branch name to apps.json, load actual README -- FIXME use live Gitea link
{ url = "http://localhost:1234/coop-cloud/" ++ app.slug ++ "/raw/branch/" ++ app.default_branch ++ "/README.md" { url = "http://localhost:1234/coop-cloud/" ++ app.slug ++ "/raw/branch/" ++ app.default_branch ++ "/README.md"
, expect = Http.expectString GotText } , expect = Http.expectString GotText }

View File

@ -9,6 +9,7 @@ import Spa.Generated.Route as Route
import Spa.Document exposing (Document) import Spa.Document exposing (Document)
import Spa.Page as Page exposing (Page) import Spa.Page as Page exposing (Page)
import Spa.Url as Url exposing (Url) import Spa.Url as Url exposing (Url)
import Util exposing (by, andThen, Direction(..))
page : Page Params Model Msg page : Page Params Model Msg
@ -50,7 +51,7 @@ init { params } =
default_image : String default_image : String
default_image = "http://localhost:8000/logo.png" default_image = "/logo.png"
-- UPDATE -- UPDATE
@ -98,6 +99,20 @@ body model =
[ viewApps model [ viewApps model
] ]
appScore : App -> Int
appScore app =
case app.status of
"1" ->
1
"2" ->
2
"3" ->
3
"4" ->
4
_ ->
5
viewStatusBadge : App -> Html Msg viewStatusBadge : App -> Html Msg
viewStatusBadge app = viewStatusBadge app =
let let
@ -119,7 +134,6 @@ viewStatusBadge app =
"?" "?"
score -> score ->
score score
in in
span [ class ("card-link badge " ++ status_class) ] span [ class ("card-link badge " ++ status_class) ]
[ text ("Score: " ++ status_score) ] [ text ("Score: " ++ status_score) ]
@ -178,6 +192,7 @@ viewApp app =
] ]
] ]
viewApps : Model -> Html Msg viewApps : Model -> Html Msg
viewApps model = viewApps model =
case model of case model of
@ -193,7 +208,10 @@ viewApps model =
Success apps -> Success apps ->
div [] div []
[ div [ class "row" ] [ div [ class "row" ]
(List.map viewApp (List.sortBy (String.toLower << .name) apps)) (List.map viewApp (apps |> List.sortWith
(by appScore ASC
|> andThen .name ASC))
)
] ]
-- HTTP -- HTTP
@ -202,7 +220,7 @@ viewApps model =
loadApps : Cmd Msg loadApps : Cmd Msg
loadApps = loadApps =
Http.get Http.get
{ url = "http://localhost:8000/abra-apps-list.json" { url = "/abra-apps-list.json"
, expect = Http.expectJson GotApps appListDecoder , expect = Http.expectJson GotApps appListDecoder
} }

34
src/Util.elm Normal file
View File

@ -0,0 +1,34 @@
module Util exposing (by, andThen, Direction(..))
type Direction
= ASC
| DESC
by : (a -> comparable) -> Direction -> (a -> a -> Order)
by toCmp direction a b =
case ( compare (toCmp a) (toCmp b), direction ) of
( LT, ASC ) ->
LT
( LT, DESC ) ->
GT
( GT, ASC ) ->
GT
( GT, DESC ) ->
LT
( EQ, _ ) ->
EQ
andThen : (a -> comparable) -> Direction -> (a -> a -> Order) -> (a -> a -> Order)
andThen toCmp direction primary a b =
case primary a b of
EQ ->
by toCmp direction a b
ineq ->
ineq