teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

コードをhackellからelmに変更

2020/09/29 10:01

投稿

best_engineer
best_engineer

スコア42

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,7 @@
3
3
  また、Modelの状態を監視しているソースなのですが、Cmdの意味やMorePleaseを押した時の挙動がよく分かっておりません。
4
4
  ご存知の方がいればお聞きしたいです。
5
5
 
6
- ```Hasckell
6
+ ```elm
7
7
  module Main exposing (main)
8
8
 
9
9
  import Browser

2

コードを追加

2020/09/29 10:01

投稿

best_engineer
best_engineer

スコア42

title CHANGED
File without changes
body CHANGED
@@ -4,6 +4,34 @@
4
4
  ご存知の方がいればお聞きしたいです。
5
5
 
6
6
  ```Hasckell
7
+ module Main exposing (main)
8
+
9
+ import Browser
10
+ import Html exposing (..)
11
+ import Html.Attributes exposing (..)
12
+ import Html.Events exposing (..)
13
+ import Http
14
+ import Json.Decode exposing (Decoder, field, string)
15
+
16
+
17
+
18
+ -- MAIN
19
+
20
+
21
+ main : Program () Model Msg
22
+ main =
23
+ Browser.document
24
+ { init = init
25
+ , update = update
26
+ , subscriptions = subscriptions
27
+ , view = viewDocument
28
+ }
29
+
30
+
31
+
32
+ -- MODEL
33
+
34
+
7
35
  type Model
8
36
  = Failure
9
37
  | Loading
@@ -14,6 +42,17 @@
14
42
  init _ =
15
43
  ( Loading, getRandomCatGif )
16
44
 
45
+
46
+
47
+ -- UPDATE
48
+
49
+
50
+ type Msg
51
+ = MorePlease
52
+ | GotGif (Result Http.Error String)
53
+
54
+
55
+ update : Msg -> Model -> ( Model, Cmd Msg )
17
56
  update msg model =
18
57
  case msg of
19
58
  MorePlease ->
@@ -27,6 +66,33 @@
27
66
  Err _ ->
28
67
  ( Failure, Cmd.none )
29
68
 
69
+
70
+
71
+ -- SUBSCRIPTIONS
72
+
73
+
74
+ subscriptions : Model -> Sub Msg
75
+ subscriptions model =
76
+ Sub.none
77
+
78
+
79
+
80
+ -- VIEW
81
+
82
+
83
+ viewDocument : Model -> Browser.Document Msg
84
+ viewDocument model =
85
+ { title = "Some cats", body = [ view model ] }
86
+
87
+
88
+ view : Model -> Html Msg
89
+ view model =
90
+ div []
91
+ [ h2 [] [ text "Random Cats" ]
92
+ , viewGif model
93
+ ]
94
+
95
+
30
96
  viewGif : Model -> Html Msg
31
97
  viewGif model =
32
98
  case model of
@@ -57,4 +123,10 @@
57
123
  , expect = Http.expectJson GotGif gifDecoder
58
124
  }
59
125
 
126
+
127
+ gifDecoder : Decoder String
128
+ gifDecoder =
129
+ field "data" (field "image_url" string)
130
+
131
+
60
132
  ```

1

タグを外す

2020/09/29 10:00

投稿

best_engineer
best_engineer

スコア42

title CHANGED
File without changes
body CHANGED
File without changes