-
Notifications
You must be signed in to change notification settings - Fork 0
/
Auth.elm
129 lines (89 loc) · 2.44 KB
/
Auth.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
module Pages.Auth exposing (Model, Msg(..), TypeUrl(..), Url, authUrlsDecoder, getAuthUrls, init, showAuthUrl, toSession, update, urlDecoder, urlTypeDecoder, view)
import Asset exposing (..)
import Endpoint
import Html exposing (..)
import Html.Attributes exposing (..)
import Http
import Json.Decode as JD
import Page
import Route
import Session exposing (..)
-- Model
type alias Model =
{ session : Session
, urls : List Url
}
type alias Url =
{ url : String
, typeUrl : TypeUrl
}
type TypeUrl
= Google
| Github
init : Session -> ( Model, Cmd Msg )
init session =
( Model session [], getAuthUrls )
-- Update
type Msg
= GotAuthUrls (Result Http.Error (List Url))
update : Msg -> Model -> ( Model, Cmd msg )
update msg model =
case msg of
GotAuthUrls result ->
case result of
Ok urls ->
( { model | urls = urls }, Cmd.none )
Err _ ->
( model, Cmd.none )
-- View
view : Model -> Page.PageStructure Msg
view model =
{ title = "Auth"
, content =
[ a [ Route.href Route.Home ] [ img [ Asset.src Asset.logo, class "center db pt2" ] [] ]
, div [] <| List.map (\url -> showAuthUrl url) model.urls
]
}
getAuthUrls : Cmd Msg
getAuthUrls =
Http.get
{ url = Endpoint.toString Endpoint.authUrls
, expect = Http.expectJson GotAuthUrls authUrlsDecoder
}
authUrlsDecoder : JD.Decoder (List Url)
authUrlsDecoder =
JD.field "data" (JD.list urlDecoder)
urlDecoder : JD.Decoder Url
urlDecoder =
JD.map2 Url
(JD.field "url" JD.string)
(JD.field "type" urlTypeDecoder)
urlTypeDecoder : JD.Decoder TypeUrl
urlTypeDecoder =
JD.string
|> JD.andThen
(\str ->
case str of
"google" ->
JD.succeed Google
"github" ->
JD.succeed Github
_ ->
JD.fail "unkown type url"
)
showAuthUrl : Url -> Html Msg
showAuthUrl url =
let
imgSrc =
case url.typeUrl of
Google ->
Asset.src Asset.signinGoogle
Github ->
Asset.src Asset.signinGithub
in
div [ class "tc pa2" ]
[ a [ href url.url ] [ img [ imgSrc ] [] ]
]
toSession : Model -> Session
toSession model =
model.session