-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlbionTradeRoutes.elm
143 lines (107 loc) · 2.87 KB
/
AlbionTradeRoutes.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module AlbionRecipes exposing (..)
import Http
import Html exposing (..)
import Html.Events exposing (onClick, on)
import Html.Attributes exposing (class)
import AlbionMarketParser
exposing
( itemListDecoder
, SimpleItems
, SimpleItem
)
itemListUrl : String
itemListUrl =
"https://albion-market.com/api/v1/items/"
albionMarketPrefix : String
albionMarketPrefix =
"https://albion-market.com/api/v1/"
initialCmd : Cmd Msg
initialCmd =
itemListDecoder
|> Http.get itemListUrl
|> Http.send LoadItems
type alias Model =
{ items : Maybe (List SimpleItem)
, loadingError : Maybe Http.Error
}
initialModel : Model
initialModel =
{ items = Nothing
, loadingError = Nothing
}
type Msg
= LoadItems (Result Http.Error SimpleItems)
| FetchItemData String
| Noop
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
LoadItems (Ok items) ->
( { model | items = Just items.items }, Cmd.none )
LoadItems (Err error) ->
( { model | loadingError = Just error }, Cmd.none )
FetchItemData id ->
( model, Cmd.none )
Noop ->
( model, Cmd.none )
main : Program Never Model Msg
main =
Html.program
{ init = ( initialModel, initialCmd )
, view = viewOrError
, update = update
, subscriptions = (\_ -> Sub.none)
}
viewOrError : Model -> Html Msg
viewOrError model =
case model.loadingError of
Nothing ->
view model
Just error ->
div [ class "error-message" ]
[ h1 [] [ text "Albion Recipes" ]
, p [] [ printError error ]
]
printError : Http.Error -> Html Msg
printError error =
case error of
Http.BadUrl string ->
text ("Bad URL:" ++ string)
Http.Timeout ->
text "Timeout"
Http.NetworkError ->
text "Network Error"
Http.BadStatus _ ->
text "Bad Status"
Http.BadPayload message _ ->
"Bad Payload: " ++ message |> text
view : Model -> Html Msg
view model =
let
itemList =
case model.items of
Nothing ->
text "Loading..."
Just items ->
allItems items
in
div [ class "content" ]
[ h1 [] [ text "Albion Market" ]
, div []
[ itemList
]
]
allItems : List SimpleItem -> Html Msg
allItems items =
List.map
printSimpleItem
items
|> div [ class "column" ]
printSimpleItem : SimpleItem -> Html Msg
printSimpleItem item =
let
tierString =
toString item.tier
in
div [ onClick (FetchItemData item.id) ]
[ text (item.name ++ " -- T" ++ tierString) ]