@@ -6,95 +6,148 @@ import FFI exposing (getBridge)
66import Html exposing (..)
77import Html.Attributes as Attr
88import Html.Events as Ev
9+import Notification
10+import Pages.Loader exposing (loader)
11+import Pages.Login as Login
12+import Pages.Navigation as Navigation
913import Utils exposing (..)
1014
1115
1216type Msg
1317 = RecieveMessage String
14− | Error String
15− | UpdateEmail String
16− | UpdateServer String
17− | UpdatePassword String
18− | Submit
18+ | ShowError String
19+ | CloseNotification
20+ | Submit { email : String, password : String, server : String }
21+ | LoginMsg Login.Msg
1922
2023
2124type alias Model =
2225 { messages : List String
23− , error : Maybe String
24− , server : String
25− , email : String
26− , password : String
26+ , notification : Maybe Notification.Config
27+ , page : PageModel
2728 }
2829
2930
31+type PageModel
32+ = LoginModel Login.Model
33+ | LoadingPage
34+
35+
36+showPage : PageModel -> ( String, List (Html Msg) )
37+showPage page =
38+ case page of
39+ LoginModel model ->
40+ let
41+ p =
42+ Login.page loginCallbacks LoginMsg
43+ in
44+ ( p.title model, p.view model )
45+
46+ LoadingPage ->
47+ ( "", [ loader ] )
48+
49+
3050main : Program () Model Msg
3151main =
3252 Browser.element
3353 { init = \_ -> ( init, Cmd.none )
3454 , view = view
3555 , update = update
36− , subscriptions = \_ -> getBridge Error (always (RecieveMessage "Got it!"))
56+ , subscriptions =
57+ \_ ->
58+ getBridge ShowError
59+ (\msg ->
60+ case msg of
61+ Bridge.Hello ->
62+ RecieveMessage "Got it!"
63+
64+ Bridge.GotEverything everything ->
65+ RecieveMessage everything
66+ )
3767 }
3868
3969
4070init : Model
4171init =
4272 { messages = []
43− , error = Nothing
44− , server = "bitwarden.iko.soy"
45− , email = "mail@iko.soy"
46− , password = ""
73+ , notification = Nothing
74+ , page = LoadingPage
4775 }
4876
4977
5078view : Model -> Html Msg
5179view model =
80+ let
81+ ( title, body ) =
82+ showPage model.page
83+ in
5284 div []
53− (maybeList model.error (text >> List.singleton >> h2 [])
54− ++ [ h1 [] [ text "HELLO" ]
55− , ul [] (model.messages |> List.map (text >> List.singleton >> li []))
56− , h4 [] [ text "Email" ]
57− , input [ Attr.type_ "text", Attr.value model.email, Ev.onInput UpdateEmail ] []
58− , h4 [] [ text "Password" ]
59− , input [ Attr.type_ "text", Attr.value model.password, Ev.onInput UpdatePassword ] []
60− , h4 [] [ text "Server" ]
61− , input [ Attr.type_ "text", Attr.value model.server, Ev.onInput UpdateServer ] []
62− , button [ Ev.onClick Submit ] [ text "Submit" ]
63− , table []
64− [ tbody []
65− (List.repeat 10 (tr [] [ td [] [ text "test" ] ]))
66− ]
67− ]
68− )
85+ [ Navigation.navigation { back = False, title = title }
86+ , main_ []
87+ (maybeList model.notification (Notification.notification CloseNotification)
88+ ++ body
89+ )
90+ ]
6991
7092
7193update : Msg -> Model -> ( Model, Cmd Msg )
7294update msg model =
95+ let
96+ processPage : (pageModel -> PageModel) -> ( Result String pageModel, Cmd Msg ) -> ( Model, Cmd Msg )
97+ processPage liftModel ( resultModel, cmd ) =
98+ case resultModel of
99+ Ok mdl ->
100+ ( { model | page = liftModel mdl }, cmd )
101+
102+ Err err ->
103+ ( { model
104+ | notification =
105+ Just
106+ { title = "An error had occured"
107+ , message = err
108+ , severity = Notification.Error
109+ }
110+ }
111+ , cmd
112+ )
113+ in
73114 case msg of
74115 RecieveMessage x ->
75116 ( { model | messages = x :: model.messages }
76117 , Cmd.none
77118 )
78119
79− Error err ->
80− ( { model | error = Just err }, Cmd.none )
81−
82− UpdateEmail email ->
83− ( { model | email = email }, Cmd.none )
84−
85− UpdateServer server ->
86− ( { model | server = server }, Cmd.none )
120+ ShowError err ->
121+ ( { model
122+ | notification =
123+ Just
124+ { title = "Something went wrong"
125+ , message = err
126+ , severity = Notification.Error
127+ }
128+ }
129+ , Cmd.none
130+ )
87131
88− UpdatePassword password ->
89− ( { model | password = password }, Cmd.none )
132+ CloseNotification ->
133+ ( { model | notification = Nothing }
134+ , Cmd.none
135+ )
90136
91− Submit ->
137+ Submit data ->
92138 ( model
93− , FFI.sendBridge
94− (Bridge.Login
95− { email = model.email
96− , server = model.server
97− , password = model.password
98− }
99− )
139+ , FFI.sendBridge (Bridge.Login data)
100140 )
141+
142+ LoginMsg imsg ->
143+ case model.page of
144+ LoginModel page ->
145+ (Login.page loginCallbacks LoginMsg).update imsg page |> processPage LoginModel
146+
147+ _ ->
148+ ( model, Cmd.none )
149+
150+
151+loginCallbacks : Login.Callbacks Msg
152+loginCallbacks =
153+ { submit = Submit }