This repository was archived by the owner on Jul 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPreApp.elm
140 lines (111 loc) · 3.78 KB
/
PreApp.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
module UnisonLocal.PreApp exposing (..)
import Api exposing (ApiBasePath(..), ApiRequest)
import Browser
import Browser.Navigation as Nav
import Env exposing (Flags)
import Html
import Http
import Perspective exposing (Perspective, PerspectiveParams)
import UnisonLocal.App as App
import UnisonLocal.Route as Route exposing (Route)
import Url exposing (Url)
type Model
= Initializing PreEnv
| InitializationError PreEnv Http.Error
| Initialized App.Model
type alias PreEnv =
{ flags : Flags
, route : Route
, navKey : Nav.Key
, perspectiveParams : PerspectiveParams
}
init : Flags -> Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url navKey =
let
route =
Route.fromUrl flags.basePath url
preEnv =
{ flags = flags
, route = route
, navKey = navKey
, perspectiveParams = Route.perspectiveParams route
}
perspectiveToAppInit perspective =
let
env =
Env.init preEnv.flags preEnv.navKey perspective
( app, cmd ) =
App.init env preEnv.route
in
( Initialized app, Cmd.map AppMsg cmd )
fetchPerspective_ =
( Initializing preEnv, Api.perform (ApiBasePath flags.apiBasePath) (fetchPerspective preEnv) )
in
-- If we have a codebase hash we can construct a full perspective,
-- otherwise we have to fetch the hash before being able to start up the
-- app
preEnv.perspectiveParams
|> Perspective.fromParams
|> Maybe.map perspectiveToAppInit
|> Maybe.withDefault fetchPerspective_
fetchPerspective : PreEnv -> ApiRequest Perspective Msg
fetchPerspective preEnv =
Api.codebaseHash |> Api.toRequest (Perspective.decode preEnv.perspectiveParams) (FetchPerspectiveFinished preEnv)
type Msg
= FetchPerspectiveFinished PreEnv (Result Http.Error Perspective)
| AppMsg App.Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
FetchPerspectiveFinished preEnv result ->
case result of
Ok perspective ->
let
env =
Env.init preEnv.flags preEnv.navKey perspective
newRoute =
perspective
|> Perspective.toParams
|> Route.updatePerspectiveParams preEnv.route
( app, cmd ) =
App.init env newRoute
in
( Initialized app, Cmd.map AppMsg cmd )
Err err ->
( InitializationError preEnv err, Cmd.none )
AppMsg appMsg ->
case model of
Initialized a ->
let
( app, cmd ) =
App.update appMsg a
in
( Initialized app, Cmd.map AppMsg cmd )
_ ->
( model, Cmd.none )
subscriptions : Model -> Sub Msg
subscriptions model =
case model of
Initialized app ->
Sub.map AppMsg (App.subscriptions app)
_ ->
Sub.none
view : Model -> Browser.Document Msg
view model =
case model of
Initializing _ ->
{ title = "Loading.."
, body = [ App.viewAppLoading ]
}
InitializationError _ error ->
{ title = "Application Error"
, body = [ App.viewAppError error ]
}
Initialized appModel ->
let
app =
App.view appModel
in
{ title = app.title
, body = List.map (Html.map AppMsg) app.body
}