/
ContentBlockProgramComponent.fs
174 lines (152 loc) · 7.42 KB
/
ContentBlockProgramComponent.fs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
namespace Songhay.Dashboard.Client.Components
open System
open System.Net
open Microsoft.AspNetCore.Components
open Microsoft.JSInterop
open Elmish
open Bolero
open Bolero.Html
open Bolero.Remoting
open Bolero.Remoting.Client
open Bolero.Templating.Client
open Songhay.Modules.Models
open Songhay.Modules.Bolero.BoleroUtility
open Songhay.Modules.Bolero.RemoteHandlerUtility
open Songhay.Player.YouTube
open Songhay.Player.YouTube.Components
open Songhay.Player.YouTube.Models
open Songhay.Player.YouTube.YtUriUtility
open Songhay.Dashboard.Client.ElmishRoutes
open Songhay.Dashboard.Client.Models
open Songhay.Dashboard.Client
open Songhay.Dashboard.Client.Components
type ContentBlockProgramComponent() =
inherit ProgramComponent<DashboardModel, DashboardMessage>()
let update remote (jsRuntime: IJSRuntime) (message: DashboardMessage) (model: DashboardModel) =
match message with
| DashboardMessage.ClearError -> { model with error = None }, Cmd.none
| DashboardMessage.CopyToClipboard data ->
jsRuntime.InvokeVoidAsync("window.navigator.clipboard.writeText", data).AsTask() |> ignore
model, Cmd.none
| DashboardMessage.Error exn -> { model with error = Some exn.Message }, Cmd.none
| DashboardMessage.GetFeeds ->
let success (result: Result<string, HttpStatusCode>) =
let dataGetter = Songhay.Dashboard.ServiceHandlerUtility.toAppData
let feeds = (dataGetter, result) ||> toHandlerOutput None
GotFeeds feeds
let uri = App.AppDataLocation |> Uri
let cmd = Cmd.OfAsync.either remote.getAppData uri success DashboardMessage.Error
{ model with feeds = None }, cmd
| DashboardMessage.GotFeeds feeds -> { model with feeds = feeds }, Cmd.none
| DashboardMessage.SetPage page ->
let m = { model with page = page }
match page with
| StudioFeedsPage -> m , Cmd.ofMsg GetFeeds
| _ -> m, Cmd.none
| DashboardMessage.SetYouTubeFigureId data -> { model with ytFigureVideoId = data }, Cmd.none
| DashboardMessage.SetYouTubeFigureTitle data -> { model with ytFigureTitle = data }, Cmd.none
| DashboardMessage.YouTubeFigureResolutionChange res -> { model with ytFigureThumbRes = res }, Cmd.none
| DashboardMessage.YouTubeMessage ytMsg ->
let ytModel = {
model with ytModel = YouTubeModel.updateModel ytMsg model.ytModel
}
let uriYtSet =
(
YtIndexSonghay |> Identifier.Alphanumeric,
snd ytModel.ytModel.YtSetIndexSelectedDocument
)
||> getPlaylistSetUri
let successYtItems (result: Result<string, HttpStatusCode>) =
let dataGetter = ServiceHandlerUtility.toYtSet
let set = (dataGetter, result) ||> toHandlerOutput None
let ytItemsSuccessMsg = YouTubeMessage.CalledYtSet set
DashboardMessage.YouTubeMessage ytItemsSuccessMsg
let failure ex = ((jsRuntime |> Some), ex) ||> ytMsg.failureMessage |> DashboardMessage.YouTubeMessage
match ytMsg with
| YouTubeMessage.CallYtItems ->
let success (result: Result<string, HttpStatusCode>) =
let dataGetter = ServiceHandlerUtility.toYtItems
let items = (dataGetter, result) ||> toHandlerOutput None
let ytItemsSuccessMsg = YouTubeMessage.CalledYtItems items
DashboardMessage.YouTubeMessage ytItemsSuccessMsg
let uri = YtIndexSonghayTopTen |> Identifier.Alphanumeric |> getPlaylistUri
let cmd = Cmd.OfAsync.either remote.getYtItems uri success failure
ytModel, cmd
| YouTubeMessage.CallYtIndexAndSet ->
let success (result: Result<string, HttpStatusCode>) =
let dataGetter = ServiceHandlerUtility.toPublicationIndexData
let index = (dataGetter, result) ||> toHandlerOutput None
let ytItemsSuccessMsg = YouTubeMessage.CalledYtSetIndex index
DashboardMessage.YouTubeMessage ytItemsSuccessMsg
let uriIdx = YtIndexSonghay |> Identifier.Alphanumeric |> getPlaylistIndexUri
let cmdBatch = Cmd.batch [
Cmd.OfAsync.either remote.getYtSetIndex uriIdx success failure
Cmd.OfAsync.either remote.getYtSet uriYtSet successYtItems failure
]
ytModel, cmdBatch
| YouTubeMessage.CallYtSet _ ->
let cmd = Cmd.OfAsync.either remote.getYtSet uriYtSet successYtItems failure
ytModel, cmd
| YouTubeMessage.OpenYtSetOverlay ->
if ytModel.ytModel.YtSetIndex.IsNone && ytModel.ytModel.YtSet.IsNone then
ytModel, Cmd.ofMsg <| DashboardMessage.YouTubeMessage CallYtIndexAndSet
else
ytModel, Cmd.none
| _ -> ytModel, Cmd.none
let view (_: IJSRuntime) (model: DashboardModel) dispatch =
ContentBlockTemplate()
.StudioLinks(StudioLinksComponent.BComp)
.Error(
cond model.error <| function
| None -> empty()
| Some err ->
ContentBlockTemplate.ErrorNotification()
.Text(err)
.Hide(fun _ -> dispatch DashboardMessage.ClearError)
.Elt()
)
.Content(
cond model.page <| function
| StudioFeedsPage -> PageComponent.BComp <| StudioFeedsElmishComponent.EComp model dispatch
| StudioToolsPage -> PageComponent.BComp StudioToolsComponent.BComp
| YouTubeFigurePage -> PageComponent.BComp <| YouTubeFigureElmishComponent.EComp model dispatch
)
.YouTubeThumbs(
YtThumbsComponent.EComp (Some "songhay tube") model.ytModel (DashboardMessage.YouTubeMessage >> dispatch)
)
.YouTubeThumbsSet(
YtThumbsSetComponent.EComp model.ytModel (DashboardMessage.YouTubeMessage >> dispatch)
)
.Elt()
static member val Id = "content-block" with get
static member PComp =
(
ContentBlockProgramComponent.Id
,
div {
attr.id ContentBlockProgramComponent.Id
newLine; indent 2
comp<ContentBlockProgramComponent>
}
) ||> HtmlDocumentComponent.BComp
[<Inject>]
member val JSRuntime = Unchecked.defaultof<IJSRuntime> with get, set
override this.Program =
let initModel =
{
error = None
feeds = None
page = StudioToolsPage
ytFigureTitle = "“It took every shred of grace and grit I had” - Bozoma Saint John"
ytFigureVideoId = "pkUK5LEZGa8"
ytFigureThumbRes = "maxresdefault"
ytModel = YouTubeModel.initialize
}
let init = (fun _ -> initModel, Cmd.ofMsg (DashboardMessage.YouTubeMessage YouTubeMessage.CallYtItems))
let update = update (this.Remote<DashboardService>()) this.JSRuntime
let view = view this.JSRuntime
Program.mkProgram init update view
|> Program.withRouter router
#if DEBUG
|> Program.withHotReload
#endif